1. Introduction to annotations
1. What is a comment
(1) Annotation is a code special mark, format: @Annotation name (attribute name=attribute value, attribute name=attribute value...)
(2) Use annotations, annotations are applied to classes, methods and attributes
(3) Use annotations Purpose: simplify xml configuration
2. Spring provides annotations for creating objects in Bean management
(1) @Component : a universal annotation that can be used to create any object
(2) @Service : generally used in the business logic layer or service layer
(3) @Controller : generally the same as the Web layer
(4) @Repository : Generally used for DAO layer
The above four annotation functions are the same and can be used to create bean instances
- Note that the annotations need to be imported into the aop package shown below
2. How to use annotations? one example
(1) Introduce dependency
(2) Turn on component scanning
- Introduce namespace:
- Turn on component scanning
(3) Create a class and add annotations to the class
-
Add the annotation @Component(value = "userService") to the called class, where value is equivalent to the id attribute in XML
-
To test whether the annotation is successful, note that bean1.xml must be read in, because component scanning is enabled in bean1.xml
4. Turn on the detailed configuration of component scanning
<!--示例1 use-default-filters="false" 表示现在不使用默认filter,自己配置filter context:include-filter,设置扫描哪些内容-->
<context:component-scan base-package="com.atguigu" use-defaultfilters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
<!--示例2 下面配置扫描包所有内容 context:exclude-filter: 设置哪些内容不进行扫描-->
<context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
3. Realize attribute injection based on annotation
(1) @Autowired: Automatic assembly according to attribute type
- Due to automatic assembly, there is no value attribute, but automatic assembly according to the type
- The first step is to create service and dao objects, and add creation object annotations to the service and dao classes
- The second step is to inject the dao object into the service, add dao type attributes to the service class, and use annotations on the attributes
@Autowired will automatically assemble according to the UserDaoImpl type
(2) @Qualifier: inject according to the name
Used with @Autowired above, in the Javax package
(3) @Resource: can be injected according to the type, can be injected according to the name
//@Resource //根据类型进行注入
@Resource(name = "userDaoImpl1") //根据名称进行注入
private UserDao userDao;
(4) @Value injects common type attributes
- Use before normal type attributes
4. Fully Annotated Development
Before, we used XML files to turn on component scanning, but we can use annotations to complete the scanning process, thus completely using XML.
(1) Create a configuration class to replace the xml configuration file
@Configuration //作为配置类,替代XML
@ComponentScan(basePackages = {
"com.LinXiaoDe.spring5"})
public class SpringConfig {
}
(2) Write test classes and use AnnotationConfigApplicationContext(SpringConfig.class)
load configuration classes
to use in SpringBoot to simplify development