Spring thorough understanding of the container and application context

Once you have Spring, dependency injection by the way, our business code do not have to manage the life cycle of the associated object. Business code only need to follow the business process itself, walked and walked, wherever, subject to the need for additional help, give Spring said, I want to object - so Spring is very attentive to your objects.

It sounds simple, it is not difficult to use, but if this is the only ism, is pretty free and easy, without any brain fee. . . However, you do not really care, Spring is where the object is to yours?

If you want to learn Spring deeper, not just make use of with, then you should think carefully about the issue on appeal, otherwise, this blog you see a shovel ah. . .

You can think about this: Spring if it wants to be responsible for application management to create so many objects, like Apple to produce so many phone (objects), as there must be a special place to engage the object. Apple mobile phone production plant in a place called, such as Foxconn, but in software development for the local Spring we will not engage in an object called a factory, and called the container.

Yes, the concept of the container in java you are most familiar than the Tomcat, and it is the Servlet running a web container, but in order to achieve Spring dependency injection features, you can not do without the production of container object - if there is no container management is responsible for creating an object, your code should only call the objects, Spring nowhere to give you ah. In fact, the container is the core Spring framework to achieve the function, the container does not just help us create an object so simple, it is responsible for the management of objects in the entire life cycle - creation, assembly, destroyed.

A term you hear most often about the Spring of this container is the IOC container. The so-called IOC, is called inversion of control programming ideas, the Internet has summarized very easy to understand, and I do not casually explained. In a word, my application was no longer bother dependencies between objects to create and manage objects, and let IOC container to do the job right, that is to say, I object creation, to have control over the management of Spring container, which is the reverse of controlling rights, so the container can be called Spring IOC container. Though, I want to clarify one thing: not to say that only container called Spring IOC container, container-based framework IOC there are many, not Spring-specific.

Well, finally the concept of Spring container set forth about it, but what use is it the egg? Light container you actually can not do anything! Do you think that science fiction container, like treasure bag in front of the jingle cats, it will give you what you want to what? In fact, nothing inside the container, the container decide what to put inside the object is our own decision dependencies between objects, but also our own, just give us a container to provide a managed object space only. So, how do we need to put our container to container object management on behalf of it? This involves the application of a Spring context.

What is the context of the application of it, you can simply understood achievement will you need Spring to help you manage objects into a container so one kind. . One kind. . amount. . A container objects - yes, that is a Spring application context container abstract representation; and say on our common ApplicationContext is essentially a high-level interface to maintain collaborative relationships between Bean and the definition of the object. Amount, does not sound very abstract mouthful? Then you reread it. . .

Here, we must be clear, the core of Spring is a container, and the container is not unique, the framework itself provides an implementation of a number of containers, roughly divided into two types: one is not commonly used BeanFactory, which is the simplest container only provide basic DI function; Another is to inherit application context derived after BeanFactory, which is an abstract interface ApplicationContext we mentioned above, it can provide a more enterprise-level services, such as parsing the configuration text messages and so on, which is the most common context instance object scenarios.

With context object, we will be able to register the object of the need to manage the Spring container. For context abstract interface, Spring also provides us with a variety of types of containers to achieve, for us to choose between different scenarios:

  • ①  AnnotationConfigApplicationContext: loaded from the one or more configuration class java-based context definition, the method is applicable to java annotation;

  • ②  ClassPathXmlApplicationContext: load context definitions from the one or more xml configuration file in the class path, xml configuration suitable manner;

  • ③  FileSystemXmlApplicationContext: load context definitions from the one or more xml configuration file in the file system, that is loaded in the xml configuration file system drive;

  • ④  AnnotationConfigWebApplicationContext: specifically for web application prepared for annotation mode;

  • ⑤  XmlWebApplicationContext: loading the web application from one or more context definition xml configuration file for xml configuration.

With the above understanding, the problem is very easy. You just objects you need for your IOC container xml-based management worth mentioning, java notes or, in short, you want to manage objects (Spring we all call it asked bean), collaborative relationship between the bean configured, then using the application context object loaded into our Spring container, which can provide management services to the object you want for your program. Below, or paste it simple application example of the application context:

Let's xml configuration by way of configuration bean and establish collaborative relationships between bean:

<?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.2.xsd">
    <bean id="man" class="spring.chapter1.domain.Man">
        <constructor-arg ref="qqCar" />
    </bean>
    <bean  id="qqCar" class="spring.chapter1.domain.QQCar"/>
</beans>

Then load the configuration through the IOC container application context to enable Spring managed objects for us, we need to be using an object when it is ok then get bean from the container:

public class Test {
    public static void main(String[] args) {
        //加载项目中的spring配置文件到容器
//        ApplicationContext context = new ClassPathXmlApplicationContext("resouces/applicationContext.xml");
        //加载系统盘中的配置文件到容器
        ApplicationContext context = new FileSystemXmlApplicationContext("E:/Spring/applicationContext.xml");
        //从容器中获取对象实例
        Man man = context.getBean(Man.class);
        man.driveCar();
    }
}

The above test, I will profile applicationContext.xml were placed under the project and any system drive, I only need to use the appropriate context object to load the configuration file, the end result is exactly the same. Of course, now more and more projects use java annotations, notes so essential ways:

//同xml一样描述bean以及bean之间的依赖关系
@Configuration
public class ManConfig {
    @Bean
    public Man man() {
        return new Man(car());
    }
    @Bean
    public Car car() {
        return new QQCar();
    }
}

 

public class Test {
    public static void main(String[] args) {
        //从java注解的配置中加载配置到容器
        ApplicationContext context = new AnnotationConfigApplicationContext(ManConfig.class);
        //从容器中获取对象实例
        Man man = context.getBean(Man.class);
        man.driveCar();
    }
}

Since then, Spring containers and application contexts even explained almost, specific skill points in the next blog post will slowly for everyone to offer.

Guess you like

Origin blog.csdn.net/Dome_/article/details/91494010