Home  >  Article  >  Java  >  Introduction to the simplified usage of Spring framework

Introduction to the simplified usage of Spring framework

不言
不言forward
2018-10-23 15:11:471906browse

This article brings you an introduction to the simplified usage of the Spring framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

As a Spring framework, its main function is to manage a bunch of classes that make the App (application) function. These classes, which are the cornerstone and backbone of the entire App, are called beans.

To manage beans, that is, a bunch of classes that perform business functions, you cannot directly new them, which lacks unified scheduling. Therefore, Spring uses .xml configuration files as a medium and IoC (Inversion of Control) as a tool to transfer these beans to the Spring container for unified management.

Based on this, to throw a bean to the container, at least two parts are required:

The definition of the class corresponding to the bean

Indirectly controlled .xml configuration file

The reason why two parts are needed is easy to understand. First, you must have a definition of the bean itself. Next, you have to tell the Spring container how to accept the bean. This is explained by the .xml file.

For example, the bean we want to manage is called HelloWorld, then its two parts are: applicationContext-src.xml:



    
            
        
      

and HelloWorld.java:

public class HelloWorld {
    private String message;     
    
    public void setMessage(String message) {       
        this.message  = message;    
    }     
    
    public void getMessage() {       
        System.out.println("Your Message : " + message);    
    } 
}

With these two parts, the Spring container can correctly receive the bean named HelloWorld.

Now, if you want to use this bean, of course you cannot touch the HelloWorld bean directly. Instead, you need to get the bean through the Spring container that manages its agent, and then use this bean to serve yourself.

For example, the class named MainApp.java now needs to use the service of the HelloWorld bean. We can do this:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {    
    
    @SuppressWarnings("resource")
    public static void main(String[] args) {       
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-src.xml");        
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");        
        obj.getMessage();    
    }
}

There are two parts here:

First, get the Spring container according to the location of the configuration .xml file, which is the Context here. It can be understood as the most famous spokesperson among several Spring containers.

After having this spokesperson, you can naturally ask the spokesperson for the required beans HelloWorld, so you can get the required beans through the context.getBean() method.

After you get the bean, you can use it directly.

It can be seen that the starting point of the Spring framework is intuitive. It is to act as a proxy for a bunch of functional classes (beans) and unify these beans into their own containers for management. Any other class that needs to use the bean must obtain it through its agent.

A question worth discussing is that the above is quite intuitive and easy to understand, but why are there not many declarations about ApplicationContext in general Java Web development?

The reason is that in normal J2EE development, the declaration of Spring Context is not completed directly in the user code, but is configured in web.xml:


    contextConfigLocation
    /WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml



    org.springframework.web.context.ContextLoaderListener

Here with the help of ContextLoaderListener registers ApplicationContext into our Web App.

Another more important consideration is:

There should be no direct use of ApplicationContext in your code, but the same use of configuration files and IoC to use Context.



The above is the detailed content of Introduction to the simplified usage of Spring framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete