SpringIOC (+ realization principle)

 

 principle

Creation process

When initializing a Spring container, Spring parses the spring configuration file (the applicationContext.xml), from top to bottom resolve to <bean id = "", class = "">, the tag according to the class attribute specifies the class full path name to find the class and create objects of this class through reflection, and objects created credited to the built-in Map container management, which is stored in the map key is the id value of the tag, the value of the object is created .

Get Object Process

When the object is acquired from the container by getBean (id) method, to find out whether a matching key values ​​from the built-Map according id, returns an object if there if no exception is thrown.

Tip : By default, many times get a bean object with id obtained is the same object (the default object is created in the case of a single example, if the label may need more examples set multiple cases). If receiving a plurality of type <bean> tag set id different, each id will have a built-in key to the Map, which is different from the value of the class object is created. Under the same <beans> tag does not allow a plurality of the same id <label> if the configuration error.

 

achieve

First, create a lib directory need to rely on imported packages and buildpath: These packages can also be spring official website to download other remote repository can be downloaded.

Tip : We are here just getting started importing this way, the back does not need to import so, the development of direct maven to automatically import)

 

 

After introducing dependencies create a Person class in the specified package (using order to create an object)

package com.test.spring;

public class Person {
    public void say(String str) {
        System.out.println(str);
    }
}

Then create a profile applicationContext.xml spring in the src directory. Implemented in this configuration file IOC Inversion of Control, also in charge of the configuration file to create an object, we do not need to create an object by the new keyword.

<?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="person" class="com.test.spring.Person"></bean>
</beans>

Here we are in the <bean id = "", class = ""> which set the type of information we need to create an object, class which fill our need to create the name of the class, spring container to find this class is loaded through this class name, and then objects created by id to be set id, when we used to find objects by the strength of the id. id can customize example: person1, person2 ,. . All OK, class name in lowercase general use.

Note : <beans> configuration content not extra spaces, will complain, version needs to be consistent. According to specifications to write.

 

Our configuration file is created Person_test good test after test class:

Package com.test.spring;
 Import org.junit.jupiter.api.Test;
 Import org.springframework.context.ApplicationContext;
 Import org.springframework.context.support.ClassPathXmlApplicationContext; 


public  class Person_Test { 
    @Test 
    / ** 
     * Get SpringIOC Object 
     * <the bean ID = "Person" class = "com.test.spring.person"> 
     * / 
    public  void Test01 () {
         // create Spring container object 
        the ApplicationContext context = new new the ClassPathXmlApplicationContext ( "the applicationContext.xml" );
         // to get the object through our profile beanid surface configuration: id = "person"
        P = the Person (the Person) context.getBean ( "Person" );
         // call the object's methods, and parameters successor 
        p.say ( "SpringIOC the Hello" ); 
        p.say ( "the Spring Inversion of Control" ); 
    } 
}

@Test here to do the necessary testing to be tested using the method of inserting the above and then Junit JUnit to run by right -Run as --- print results are as follows:

 

Guess you like

Origin www.cnblogs.com/jumpkin1122/p/11565282.html