This is the 14th day of my participation in the More text Challenge. For more details, see more text Challenge


Related articles

Spring series: Spring series


The scope of the bean

  • In Spring, the objects that make up the bulk of an application and are managed by the Spring IoC container are called beans.
  • Simply put, beans are objects that are initialized, assembled, and managed by the IoC container.
  • Entity class:
public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
}
Copy the code
1.1. Singleton

1) XML:


      
<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.xsd">

    <bean id="person" class="dyj.entity.Person" scope="singleton">
        <property name="like" value="Phishing"/>
        <property name="age" value="23"/>
        <property name="name" value="Ding Da"/>
    </bean>
</beans>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person1 =  Context.getBean("person",Person.class);
        Person person2 =  Context.getBean("person",Person.class); System.out.println(person1.hashCode()); System.out.println(person2.hashCode()); System.out.println(person1==person2); }}Copy the code

③ The execution result is as follows:

(4) conclusion:

  • When scope is singleton, there is one and only one instance in the Spring container.
  • Whether you use the instance or not, it is created when the container starts.
1.2. Prototype

1) XML:


      
<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.xsd">

    <bean id="person" class="dyj.entity.Person" scope="prototype">
        <property name="like" value="Phishing"/>
        <property name="age" value="23"/>
        <property name="name" value="Ding Da"/>
    </bean>
</beans>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person1 =  Context.getBean("person",Person.class);
        Person person2 =  Context.getBean("person",Person.class); System.out.println(person1.hashCode()); System.out.println(person2.hashCode()); System.out.println(person1==person2); }}Copy the code

③ Execution result:

(4) conclusion:

  • When Scope is prototype, one bean corresponds to multiple object instances!
  • It is not instantiated when we start the container, but only when we use it!
1.3. Request (Just understand)
  • When a bean is scoped as Request, it means that each bean definition corresponds to one instance in an HTTP Request. That is, each HTTP request has its own bean instance, which is created based on a bean definition. This scope is only valid in the context of the Web-based Spring ApplicationContext. Consider the following bean definition:
<bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
Copy the code
  • For each HTTP request, the Spring container creates a new loginAction Bean instance based on the loginAction Bean definition, which is available only within the current HTTP request. Thus, the internal state of the established instance can be confidently changed as needed, and instances created in other requests based on the loginAction Bean definition will not see these request-specific state changes. When processing the request is complete, the request scoped bean instance will be destroyed.
1.4 Session (just know)
  • When a bean is scoped as Session, it means that each bean definition corresponds to one instance in an HTTP Session. This scope is only valid in the context of the Web-based Spring ApplicationContext. Consider the following bean definition:
 <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
Copy the code
  • For an HTTP Session, the Spring container creates a new Instance of the userPreferences Bean based on the userPreferences Bean definition, The userPreferences bean is valid only for the current HTTP Session. As with the Request scope, you can safely change the internal state of the created instance as needed, and instances created in other HTTP sessions based on userPreferences will not see these changes in state specific to a particular HTTP Session. When an HTTP Session is finally discarded, beans in the scope of that HTTP Session are also discarded.

2. Automatic assembly of beans

1.0. Environment construction

1) 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.xsd">

    <bean id="car" class="dyj.entity.Car"/>
    <bean id="house" class="dyj.entity.House"/>

    <bean id="person" class="dyj.entity.Person">
        <property name="car" ref="car"/>
        <property name="house" ref="house"/>
        <property name="like" value="Phishing"/>
        <property name="age" value="23"/>
        <property name="name" value="Ding Da"/>
    </bean>
</beans>
Copy the code

② Entity class:

public class Car {
    public void getName(a){
        System.out.println("I'm a car!); }}Copy the code
public class House {
    public void getName(a){
        System.out.println("I'm a house!); }}Copy the code
public class Person {    
    private Car car;     
    private House house; 
    private String name; 
    private int age;     
    private String like; 
    private String high; 
}
Copy the code

③ Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person =  Context.getBean("person",Person.class); person.getCar().getName(); person.getHouse().getName(); }}Copy the code

④ Execution result:

(5) conclusion:

  • The setup is complete.
  • The Bean can be injected normally.
1.1 byName: automatic assembly byName

① Modify XML:


      
<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.xsd">

    <bean id="car" class="dyj.entity.Car"/>
    <bean id="house" class="dyj.entity.House"/>

    <bean id="person" class="dyj.entity.Person" autowire="byName">
        <property name="like" value="Phishing"/>
        <property name="age" value="23"/>
        <property name="name" value="Ding Da"/>
    </bean>
</beans>
Copy the code

② Execution result:(3) conclusion:

  • Proof can be made by autowire=”byName” to automate assembly byName!
  • If the bean id=”car” value is the same as the property name in the entity class!
1.2. ByType: automatic assembly byType

① Modify XML:


      
<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.xsd">

    <bean class="dyj.entity.Car"/>
    <bean class="dyj.entity.House"/>

    <bean id="person" class="dyj.entity.Person" autowire="byType">
        <property name="like" value="Phishing"/>
        <property name="age" value="23"/>
        <property name="name" value="Ding Da"/>
    </bean>
</beans>
Copy the code

② Execution result:

(3) conclusion:

  • The id of the bean has nothing to do with it.
  • The same bean can only have one, otherwise error!
1.3 contructor: the constructor is automatically assembled, ref: manually created by default
  • It has been explained before, please refer to it!

How does Spring create objects through IOC?


I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah