Home > Java > javaTutorial > body text

Introduction to the simplified usage of Spring framework

不言
Release: 2018-10-23 15:11:47
forward
2029 people have browsed it

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:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"     
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="helloWorld" class="com.terence.HelloWorld">        
        <property name="message" value="Hello Terence&#39;s World!" />
    </bean>  

</beans>
Copy after login

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);    
    } 
}
Copy after login

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();    
    }
}
Copy after login

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:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!