A brief description of Spring's IOC configuration and use

Follow up from the previous article: https://blog.csdn.net/hgfhjtff/article/details/108042988 Add link description
1. Assemble the Bean into the Spring container for XML configuration.

<?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
">
    <!-- 使用 id 属性定义 student,其对应的实现类为 org.example.bean.Student -->
    <bean id="student" class="org.example.bean.Student"></bean>
    
    <!-- 使用 name 属性定义 teacher,其对应的实现类为 org.example.bean.Teacher -->
    <bean id="teacher" class="org.example.bean.Teacher"></bean>
</beans>

Attributes in the Bean element (commonly used):
id: is a unique identifier of a Bean, and the configuration and management of the Bean by the Spring container are completed through this attribute.
name: The Spring container can also configure and manage the Bean in the container through this attribute. The name attribute can specify multiple names for the Bean, and each name is separated by a comma or semicolon.
class: This attribute specifies the specific implementation class of the Bean. It must be a complete class name, using the fully qualified name of the class.
Scope: used to set the scope of the Bean instance, its attribute values ​​are singleton (singleton), prototype (prototype). The default value is singleton.
init-method: used to initialize the Bean instance object, called after the instance is finished.
destruction-method: used to destroy Bean instance objects, called when the container is destroyed.
constructor-arg: a child element of the element, you can use this element to pass in construction parameters for instantiation. The index attribute of this element specifies the serial number of the construction parameter (starting from 0), and the type attribute specifies the type of the construction parameter.
property: A child element of the bean element, used to call the Set method in the Bean instance to complete property assignment, thereby completing dependency injection. The name attribute of this element specifies the name of the corresponding attribute in the Bean instance.
ref: sub-element of elements such as property and constructor-arg. The bean attribute in this element is used to specify a reference to a Bean instance in the Bean factory.
value: The child elements of elements such as property and constractor-arg are used to directly specify a constant value.
list: Used to encapsulate dependency injection of List or array types.
set: Dependency injection used to encapsulate Set type attributes.
map: Dependency injection used to encapsulate Map type attributes.
entry: a child element of the map element, used to set a key-value pair. The key attribute specifies the key value of the string type, and the ref or value child element specifies its value.

2. Bean three kinds of instantiation: (!! The entity class is omitted here, the Spring configuration header in XML!!!)
Constructor instantiation:
configuration and call:

    <bean id="student" class="org.example.bean.Student"/>

 	ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("instance.xml");
	Student student = applicationContext.getBean("student", Student.class);

Static method instantiation:

    <bean id="teacher" 
          class="org.example.factory_static.TeacherFactory" 
          factory-method="create" />
          
 	ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("instance.xml");
	Student student = applicationContext.getBean("student", Student.class);

Example factory factory:

    <bean id="managerFactory" class="org.example.factory_instance.ManagerFactory"/>
    <!-- factory-bean属性指定一个实例工厂,factory-method属性确定使用工厂中的哪个方法 -->
    <bean id="manager" factory-bean="managerFactory" factory-method="create" />
          
 	ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("instance.xml");
	Student student = applicationContext.getBean("student", Student.class);

Bean's scope:
singleton singleton
prototype prototype
request Http request
session: In an HTTP Session, the container will return the same instance of the Bean. For different HTTP requests, different instances will be returned, and the scope is only valid in the current HTTP Session.
global session

Bean's life cycle:
1. Instantiate Bean
2. Inject all attributes of Bean
3. Implement BeanNameAware, setBeanName() method passes the id value of the current Bean
4. Implement BeanFactoryAware interface, setBeanFactory() passes the instance reference of the current factory.
5. To implement the ApplicationContextAware interface, Spring calls the setApplicationContext() method to pass in the reference of the current ApplicationContext instance.
6. BeanPostProcessor() is associated with the Bean, and postProcessBeforeInitialzation() initializes the Bean. This is how Spring's AOP is implemented.
7. If the Bean implements the InitializingBean interface, Spring will call the afterPropertiesSet() method.
8. If the initialization method is specified through the init-method attribute in the configuration file, the initialization method is called.
9. If BeanPostProcessor and Bean are associated, Spring will call the interface initialization method postProcessAfterInitialization(). At this point, the Bean can be used by the application system.
10. If the scope of the bean is specified as scope="singleton", then the bean will be placed in the Spring IoC buffer pool, which will trigger Spring's life cycle management of the bean; if the bean is specified in The scope of scope is scope="prototype", then the Bean is handed over to the caller, the caller manages the life cycle of the Bean, and Spring no longer manages the Bean.
11. If the Bean implements the DisposableBean interface, Spring will call the destroy() method to destroy the Bean in Spring; if the destruction method of the Bean is specified in the configuration file through the destroy-method attribute, Spring will call this method to perform the Bean destroy.

Based on Annotation:
@Component
can use this annotation to describe Beans in Spring, but it is a generalized concept that only represents a component (Bean) and can be applied at any level. When using, just mark the annotation on the corresponding class.
@Autowired is
used to annotate the attribute variables of the Bean, the Set method and the constructor of the attribute, and cooperate with the corresponding annotation processor to complete the automatic configuration of the Bean. The assembly is performed according to the type of Bean by default.
@Resource has the
same effect as Autowired. The difference is that @Autowired assembles according to Bean type by default, while @Resource assembles according to Bean instance name by default.
There are two important attributes in @Resource: name and type. Spring resolves the name attribute to the Bean instance name and the type attribute to the Bean instance type.
If the name attribute is specified, the assembly is performed according to the instance name; if the type attribute is specified, the assembly is performed according to the bean type.
If neither is specified, it will be assembled according to the Bean instance name first, if it cannot match, then it will be assembled according to the Bean type; if it fails to match, a NoSuchBeanDefinitionException will be thrown.
@Qualifier
and @Autowired annotations are used together, and the default assembly by Bean type will be modified to assembly by the instance name of the Bean. The instance name of the Bean is specified by the parameter of the @Qualifier annotation.
@Controller
It usually acts on the control layer (Controller layer) and is used to identify the class of the control layer as a Bean in Spring, and its function is the same as @Component.
@Service
usually acts on the business layer (Service layer) and is used to identify the business layer class as a Bean in Spring, and its function is the same as @Component.
@Repository is
used to identify the class of the data access layer (DAO layer) as a Bean in Spring, and its function is the same as @Component.

Automatic assembly:
To use automatic assembly, you need to configure the autowire attribute of the element. The autowire attribute has five values.

 <bean id="userService" 
          class="org.example.wire_annotation.UserService" 
          autowire="byName" />

byName:
Automatic assembly according to the name of the Property. If the name of a Bean is the same as the name of the Property in another Bean, the Bean will be automatically assembled into the Property.
byType:
Automatic assembly according to the property data type (Type). If the data type of a Bean is compatible with the property data type of another Bean, it will be automatically assembled.
constructor:
According to the data type of the parameters of the construction method, automatic assembly of byType mode is performed.
autodetect:
If the default construction method is found, use the constructor mode, otherwise use the byType mode.
no:
By default, auto-wiring is not used. Bean dependencies must be defined by ref elements.

Guess you like

Origin blog.csdn.net/hgfhjtff/article/details/108100713