Spring assembles bean s based on annotations -- easy to understand

    1. Why use annotations to assemble bean s

          First of all, understand a problem, what is assembly?

          Assembly: filling in configuration and setting specified values for attributes of non simple data types (basic data types and String types are regarded as simple data types) is called assembly, so we know that the word assembly specifically refers to configuring attribute values for non simple data types. Simple data types do not have the concept of assembly.

  In Spring, although XML configuration files can be used to assemble beans, if there are a large number of beans in the application, the XML configuration files will be too bloated, which will bring some difficulties to maintenance and upgrade.

Since JDK 5.0, Java has provided Annotation function. Spring 2.5 also provides comprehensive support for Annotation technology. We can use annotations to configure dependency injection.

Spring does not use annotations to assemble beans by default, so you need to add < context: annotation config / > in the configuration file to enable annotations.

2. Review the xml file to register the bean object

    2.1 create an. xml file and configure the document declaration

<?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-3.0.xsd
  http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--No, autowire attribute-->
<bean id="userMappingImpl" class="----.UserMappingImpl">
<property name="" value=""/>
  <property name="" ref=""></property> 
</bean>
<!--yes autowire attribute-->
<bean id="userMappingImpl" class="----.UserMappingImpl" autowire="byType">
<property name="" value=""/>
</bean>
<!--Summary: autowire="Five different ways"Property can omit this line <!--<property name="" ref=""></property> Code, let spring The container automatically implements the configuration according to the specified way to complete the code injection (that is, the assembly)-->
</beans>

      The autowire attribute has the following parameters

          Respectively, it means to let the spring container inject dependent object values in different ways.

    2.2 creating classes

public class UserMapperImpl{
    private UserMapper userMapper;
    private String id;
     public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
     public void setId(String id){
        this.id=id;
   }
}

  2.3

3. How to assemble bean s with annotations

    3.1 configuration in. xml 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="Give Way spring Package name of container scan"/>
</beans>

 < context:component-scan   Base package = "" / > function: let the container automatically scan the annotations under the package to create beans. Which annotations can create bean objects? (@ Component,@Repository,@Service,@Controller)

  The main notes of Spring are:

@Component: use this annotation to mark on the class and represent a component (Bean),

This annotation is equivalent to the configuration of a single bean object in the following xml file

<bean id="" class=""></bean>

@Repository: used to identify the class on the data access layer (Dao layer) as a bean in spring, and tell the spring container that this is the Dao layer class with the same function as @ Component.

@Service: the class used in the business layer is identified as a bean in spring, and its function is the same as @ Component.

@Controller: used to identify the class on the control layer as a bean in spring, with the same function as @ Component (used in spring MVC class annotation).

    We can remember that @ Component is equivalent to the general terms @ Repository, @ Service and @ Controller. Their functions are the same. For example, we sometimes refer to Zhang San and Li Silong as people.

After decorating a class with annotations in @ Component, @ Repository, @ Service, @ Controller, it indicates which class instance object is registered in the container in spring.

  After doing this, we just know what annotations can be used for instance bean s. These annotations are the ones that really implement assembly:

@Autowired: automatic assembly. It can be used for attributes, Getter methods and construction methods. It is assembled by type by default (autowire="byType").

@Qualify: generally used in conjunction with @ AutoWired annotation, @ qualify (value="id") enables you to inject attributes through id names.

@Resource: works on properties, Getter methods, and constructor methods. It is assembled by name by default (autowire="byName").

@Configuration: configuration annotation. You can create an empty class. The @ configuration annotation is used on the class to identify that the class is used by the configuration class.

@Configuration
 @ComponentScan(value = "package name") < / * replaces the annotation scan of the. xml file*/
public class UserMapperImpl{}

                    It's not easy to create. My favorite friends can connect three times with one button ----- bye!!!

 

 

Tags: Java Spring

Posted on Wed, 01 Sep 2021 16:39:52 -0400 by mark_kccs