Spring-Bean automatic assembly

1.1 Project Directory​

1.2 Automatic assembly concept

  • Auto-wiring is a way to use spring to satisfy bean dependencies.
  • Spring will look for dependent beans for a bean in the application context.

There are three assembly mechanisms for beans in Spring, namely:

  1. Explicit configuration in xml;
  2. Explicit configuration in java;
  3. Implicit bean discovery mechanism and automatic assembly.

Spring's automatic assembly needs to be implemented from two perspectives, or two operations:

  1. Component scanning: Spring will automatically discover beans created in the application context.
  2. Autowiring: Spring automatically satisfies the dependencies between beans, which is what we call IoC/DI.

Note : The combination of component scanning and automatic assembly exerts great power, so that the display configuration is reduced to a minimum.

1.3 Code example

Create two new entity classes, Student Teacher has a function method.

package cn.guardwhy.domain;

public class Student {
    
    
    public void function(){
    
    
        System.out.println("学生学习!!!");
    }
}
package cn.guardwhy.domain;

public class Teacher {
    
    
    public void function(){
    
    
        System.out.println("老师教导学生学习!!");
    }
}

Create a new user class User

package cn.guardwhy.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    
    
    private Student student;
    private Teacher teacher;
    private String str;
}

Write Spring configuration file

<?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="student" class="cn.guardwhy.domain.Student"/>
    <bean id="teacher" class="cn.guardwhy.domain.Teacher"/>

    <bean id="user" class="cn.guardwhy.domain.User">
        <property name="student" ref="student"/>
        <property name="teacher" ref="teacher"/>
        <property name="str" value="guardwhy"/>
    </bean>
</beans>

Test code

package cn.guardwhy.Test;

import cn.guardwhy.domain.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    
    
    @Test
    public void testMethodAutowire(){
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");
        user.getTeacher().function();
        user.getStudent().function();
    }
}

1.4 byName

autowire byName (autowire by name)
In the process of manually configuring xml, errors such as missing letters and capitalization often occur, and cannot be checked, which reduces the development efficiency.
The use of automatic assembly will avoid these errors and simplify the configuration.

Test code

Modify the bean configuration and add an attribute autowire="byName"

<!--byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid-->
<bean id="user" class="cn.guardwhy.domain.User" autowire="byName">
     <property name="student" ref="student"/>
</bean>

to sum up

When a bean node has the attribute of autowire byName.

  1. It will find all the set method names in its class, such as setStudent, and get the string with the set removed and the first letter lowercase, that is, student.
  2. Go to the spring container to find if there is an object with this string name id.
  3. If there is, take out the injection; if not, report a null pointer exception.

1.5 byType

autowire byType (automatic assembly by type)

Test code

The use of autowire byType first needs to ensure that objects of the same type are unique in the spring container. If it is not unique, a non-unique exception will be reported.

<!--byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!!-->
<bean id="user" class="cn.guardwhy.domain.User" autowire="byType">
     <property name="student" ref="student"/>
</bean>

1.6 Summary

  • When byname, it is necessary to ensure that the id of all beans is unique, and the bean needs to be consistent with the value of the set method of the automatically injected property.
  • When bytype, it is necessary to ensure that the class of all beans is unique, and the bean needs to be consistent with the type of the automatically injected properties.

Guess you like

Origin blog.csdn.net/hxy1625309592/article/details/115026852