SpringIoc Bean created way

What is the IOC, what is the role of

IOC definition (Inversion of Controle, Inversion of Control), Spring's core function, will be the object of our program to use Spring to manage, including object creation and destruction, and finally via dependency injection approach to achieve our program calls, etc., which can bring many benefits:

  1. Centralized resource management, resource configurable and easy to manage.
  2. Reducing the use of resources dependence between the two sides, that is, we say that the degree of coupling.

Create a test project SpringIoc

  1. Maven configuration:
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  1. Create our own Service
public class UserService {

    private String name;

    public UserService(){
        System.out.println("我是无参构造函数");
    }

    public UserService(String name) {
        this.name = name;
        System.out.println("我是带参构造函数 " );
    }

	public void sayHello(){
        System.out.println("Hello, My name is " + name);
    }
}
  1. Create a Spring configuration file: Spring-csdn.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
       
	<!-- 具体配置后续添加-->
	
</beans>
  1. Creating TestUserService:
public class TestUserService {

    @Test
    public void testIoc(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-csdn.xml");
        UserService userService = (UserService) applicationContext.getBean("com.lot.test.UserService");
        userService.sayHello();
    }
}

Bean is created:

  1. Constructor with no arguments embodiment, arranged in the spring-csdn.xml added in:

    <!-- 无参构造方式创建 -->
    <bean class="com.lot.test.UserService"/>
    

    Execution TestUserService.testIoc (), printed as:

    我是无参构造函数
    Hello, My name is null
    
  2. Embodiment is configured with parameters, arranged in a spring-csdn.xml added in:

        <!--带参构造方式创建-->
        <bean class="com.lot.test.UserService">
            <constructor-arg name="name" value="张井天" />
        </bean>
    

    Execution TestUserService.testIoc (), printed as:

    我是带参构造函数
    Hello, My name is 张井天
    
  3. In concrete work, in order to simplify the configuration tags may be added: the component-scan, this tag in the xml configuration, spring automatically to scan the following base-pack or sub-packets following java file, if there is scanned @Component @ Controller @ Service such as these annotated classes, put these classes registered as a bean

    <context:component-scan base-package="com.lot.test" />
    

    Execution TestUserService.testIoc (), the com.lot.test.UserService modified to userService, printed as:

    我是无参构造函数
    Hello, My name is null
    

SpringIoc create a flow chart Bean

  1. Spring source code is more complex, here are just a few of the bloggers know:

  2. Spring loaded first spring-csdn.xml our configuration file, and then generates the corresponding Bean beanDefinition saved to each CurrentHashMap in schematic sequence is as follows:
    Here Insert Picture Description

  3. When initialization beanDefinitionMap, will continue to instantiate beanDefinition Bean to save the properties singletonObjects DefaultSingletonBeanRegistry in schematic sequence is as follows:

Here Insert Picture Description
4. When I call getBean, if a singleton Bean will be available directly from singletonObjects the (lazy loading will be created), non-singleton bean is re-created according to the information stored beanDefinition, returns to the program.

The above is only superficial understanding of bloggers, we have a better suggestion, please leave a message, bloggers will promptly correct! ! !

Guess you like

Origin blog.csdn.net/zhangyong01245/article/details/93472117