Detailed explanation of Spring annotation configuration implementation process

Support for configuration annotations:

After spring 4, if you want to use the annotation form, you must introduce the aop package

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aop</artifactId>
	<version>5.2.8.RELEASE</version>
</dependency>

Import context constraints and add annotation support:

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

Configure to scan the annotations under the specified package

<!--指定注解扫描包-->
<context:component-scan base-package="com.lf.pojo"/>

Common annotation description

Automatic assembly annotation

  • @Autowired: Automatic assembly, its role is to eliminate the getter/setter method in the Java code and the property attribute in the bean. Does it eliminate the need for getter programs? If private properties need to be provided externally, they should be retained
  • @Autowired is automatically transferred by type and does not support id matching
  • Need to import spring-aop package
  • If @Autowired cannot uniquely autowire the attributes, you need to pass @Resource(value="xxx")
  • @Qualifier: If there is more than one matching bean in the container, the name of the bean can be qualified through the @Qualifier annotation
  • @Autowired is automatically assembled according to the type byType, if @Qualifier is added, it can be automatically assembled according to the byName method
  • @Qualifier cannot be used alone
  • @Resource: Automatic assembly, by name and type. Very similar to @Autowired annotation
  • A summary of the similarities and differences between @Autowired and @Resource:
  • Both @Autowired and @Resource can be used to assemble beans. Can be written on the field, or written on the setter method
  • @Autowired is assembled by type by default (belonging to the spring specification). By default, the dependent object must exist. If you want to allow a null value, you can set its required attribute to false, such as: * @Autowired(required=false), if you want Use name assembly can be combined with @Qualifier annotations
  • @Resource is assembled by name by default, and the name can be specified by the name attribute. If the name attribute is not specified, when the annotation is located on the field, it will search by name by the field name by default; if the annotation is written on the setter method, the attribute name will be used for assembly by default. When no bean matching the name is found, the assembly is performed according to the type. But it should be noted that: once the name attribute is specified, it will only be assembled according to the name.
    They have the same function and are injected into the object by annotation, but the execution order is different. @Autowired first byType, @Resource first byName

Implementation of Bean

@Component: Its role is to achieve bean injection. @Component annotations can be placed on the top of the class, but @Component is not recommended to use
Spring provides more detailed annotation forms: @Repository, @Service, @Controller , they correspond respectively Storage layer Bean, business layer Bean, and presentation layer Bean. Therefore it is recommended to use them instead of @Component

Property injection
Use annotations to inject properties

You can add @value("value") directly to the direct name without providing the set method

@Component("user")
// 相当于配置文件中
public class User {
@Value("Java")
// 相当于配置文件中 
public String name;
}

If a set method is provided, add @value("value") to the set method

@Component("user") 
public class User {
  public String name;
  @Value("Java")  
  public void setName(String name) {    
    this.name = name;  
  } 
}

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113400433