I heard that wechat search “Java fish” will change strong oh!

This article is in Java Server, which contains my complete series of Java articles, can be read for study or interview

The content of this chapter is based on the official documentation: docs.spring. IO /spring-fram…

The Spring version is 5.2.9

(1) Scope of Bean

There are six scopes for beans in Spring

(1.1) Singleton singleton scope

By default, spring beans use a singleton scope, meaning that only one bean object is created globally, and all beans retrieved in the program are the same. You can also explicitly configure the scope:

<bean id="user" class="com.javayz.pojo.User" scope="singleton">
Copy the code

Prototype scope

The prototype pattern is that a new object is generated each time a Bean object is got from the container

<bean id="user" class="com.javayz.pojo.User" scope="prototype">
Copy the code

(1.3) Four other scopes used in Web development

Request, Session, Application, and Websocket can only be used in Web development, respectively:

Request: The bean uses one in a request.

Session: Use one bean in the same session.

The application: bean is used in a single ServletContext.

Websocket: Beans are used in a single websocket.

(2) Automatic assembly of beans

In the previous article, all beans were assigned manually. In addition to manual assignment, beans also support implicit auto-assembly

Let’s start with a simple test example:

2.1. Example Building

Two entity classes:

public class Teacher {
    public void teach(a){
        System.out.println("Class"); }}Copy the code
public class Student {
    private String name;
    private Teacher teacher;
    // omit the get and set methods
}
Copy the code

bean.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 https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="teacher" class="com.javayz.pojo.Teacher"/>
    <bean id="student" class="com.javayz.pojo.Student">
        <property name="name" value="javayz"/>
        <property name="teacher" ref="teacher"/>
    </bean>
</beans>
Copy the code

Testing:

@org.junit.Test
public void test(a){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Student student = (Student) context.getBean("student");
    student.getTeacher().teach();
}
Copy the code

This is a manual assembly method. In bean.xml, we manually assemble the bean with the id teacher to the teacher variable. Now we use autowiring:

2.2 Autowire automatic assembly

<bean id="teacher" class="com.javayz.pojo.Teacher"/>
<bean id="student" class="com.javayz.pojo.Student" autowire="byName">
    <property name="name" value="javayz"/>
</bean>
Copy the code

Instead of writing the teacher attribute to the bean, we use Autowire =”byName” and the Spring container will automatically help us find the bean based on its name. In addition to byName, there are several other ways:

ByName: Automatically looks up the bean in the context with the same value name as the bean after your set method;

ByType: automatically searches the context to find the bean of the same object type as its own. ByType is used to ensure that the bean is globally unique.

(3) Use annotations to achieve automatic assembly

Using annotations requires us to configure enable:


      
<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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>
Copy the code

Again, this time declare only one bean in the XML file:


      
<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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <bean id="teacher" class="com.javayz.pojo.Teacher"/>
    <bean id="student" class="com.javayz.pojo.Student"/>
</beans>
Copy the code

Inject the Bean in the entity class with the @AutoWired annotation

public class Student {
    private String name;
    @Autowired
    private Teacher teacher;
    // omit the get and set methods
}
Copy the code

The end result is the same as manual injection. If you define multiple beans for the same class in XML, you can specify a bean using @qualifier () :

public class Student {
    private String name;
    @Autowired
    @Qualifier(value = "teacher2")
    private Teacher teacher;
}
Copy the code

In addition to using @AutoWired, you can also assemble beans using @Resocure annotations

public class Student {
    private String name;
    @Resource
    private Teacher teacher;
}
Copy the code

The difference between @autowired and @Resource is:

@autoWired is implemented by ByName. If no object is found, a null pointer exception is reported.

@resource is implemented by Byname. If the name cannot be found, ByType is implemented. Otherwise, an error is reported.