IOC operation Bean Management (annotation based)

1. What is annotation

  1. Annotation is a code special tag, format: @ annotation name (attribute name = attribute value, attribute name = attribute value...)
  2. Use annotations, which act on classes, properties, and methods
  3. The purpose of using annotations is to simplify xml configuration

2. Spring provides annotations for creating objects in Bean management

  1. @Component
  2. @Service
  3. @Controller
  4. @Repository

The above four annotation functions are the same and can be used to create bean instances
The different names are just for the convenience of distinguishing each code layer in the code.

3. Object creation based on annotation

Step 1: introduce dependencies (Spring-aop-5.2.6.RELEASE.jar)
Step 2 start component scanning

  1. If multiple packages are scanned, they are separated by commas
  2. Scan package upper directory
    <context:component-scan base-package="com.clearlast"></context:component-scan>
    

The third step is to create a class and add object annotations on the class

  1. In the annotation, the value attribute value can be omitted,
    The default value is the class name, which is lowercase
    UserService – userService

    @Component(value = "userService") //Equivalent to < bean id = "userservice" class = ".." / >
    public class UserService {
    	 public void add() {
    	 System.out.println("add.......");
     	 } 
    }
    

4. Enable component scanning details configuration

Use default filters = "false" means that you do not use the default filter now, but configure the filter yourself
Context: include filter, which sets what content to scan
Context: Exclude Filter: sets which contents are not scanned

  1. If you want to configure the scanned content yourself, first set use default filters to false,
    Generally, use default filters = "false" and context: include filter are used together.

  2. If you just want to set some contents not to be scanned, you can directly configure context: exclude filter, because they are scanned by default.

  3. give an example:

    3.1 set what content needs to be scanned

    <context:component-scan base-package="com.clearlast" use-default-filters="false">
     	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    

    3.2 set which contents do not need to be scanned

    <context:component-scan base-package="com.atguigu">
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    

5. Attribute injection based on annotation

  1. @Autowired: automatically assemble according to the attribute type

  2. @Resource: can be injected according to type or name
    @Resource(name = "userDaoImpl1")

  3. @Value: inject common type attribute
    @Value(value = "abc")

  4. @Qualifier: inject the @ qualifier annotation according to the name,
    Use with @ Autowired above
    @Qualifier(value = "userDaoImpl1")

    Example:

    @Autowired //Injection according to type
    @Qualifier(value = "userDaoImpl1") //Injection by name
    private UserDao userDao;
    

6. Fully annotated development

  1. @Configuration (represents that this class is a configuration class)

  2. @ComponentScan (basePackages = {"..."}) (packages to scan)

  3. Create a configuration class to replace the xml configuration file

    @Configuration //As a configuration class, replace the xml configuration file
    @ComponentScan(basePackages = {"com.clearlast"})
    public class SpringConfig {
    }
    
  4. Writing test classes

    @Test
    public class test(){
    	ApplicationContext context = new AnnotationConfigApplicationContext("SpringConfig.class");
    	context.getbean("xxx");
    }
    

Keywords: Spring

Added by leesiulung on Mon, 27 Dec 2021 01:27:06 +0200