1、什么是注解

  1. 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值,…)
  2. 使用注解,注解作用在类上面,方法上面,属性上面
  3. 使用注解目的:简化xml配置

2、Spring针对Bean管理中创建对象提供的注解

  1. @Component //组件
  2. @Service //用于service层
  3. @Controller //用于web层
  4. @Repository //一般用于dao层

四个注解功能是一样的,都可以用来创建bean实例

3、基于注解方式实现对象创建

第一步,引入aop.jar包
image.png
第二步,开启组件扫描

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8. <!--开启组件扫描
  9. 1 如果扫描多个包,多个包使用逗号隔开
  10. 2 扫描包上层目录
  11. -->
  12. <context:component-scan base-package="com.daijunyi"></context:component-scan>
  13. </beans>

第三步创建 UserService类

package com.daijunyi.service;

import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写 UserService--->userService
@Component(value = "userService")//<bean id="userService"/>
public class UserService {

    public void add(){
        System.out.println("add方法");
    }
}

第四步 测试

    @Test
    public void testUserService(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }

4、开启组件扫描细节问题

1、自己指定包下扫描内容

    <!--示例1
        use-default-filters="false"表示现在不使用默认filter,自己配置filter
        context:include-filter,设置扫描那些内容
        -->
    <context:component-scan base-package="com.daijunyi" use-default-filters="false">
        <!--表示只扫描 Component注解的类-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
    </context:component-scan>

2、排除包下某些内容不扫描

<!--示例2
        下面配置扫描包所有内容,但是排除Repository注解
    -->
    <context:component-scan base-package="com.daijunyi">
        <!--表示不扫描 Repository注解的类-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

5、注解属性注入

  1. @AutoWired //根据属性类型自动注入
  2. @Qualifier //根据属性名称进行注入 但是得在@AutoWired注解使用的时候作为一个补充
  3. @Resource//可以根据类型注入,可以根据名称注入

以上三种是针对对象注入

  1. @Value //对基本类型进行注入
     @Value(value = "${file.name}")
     private String name;
    
    可以用来取属性文件中的值,前提是得在xml配置文件中引入了属性值文件。
    <context:property-placeholder location="classpath:data.properties"></context:property-placeholder>
    
    引入外部属性值文件

6、纯注解开发

  1. 创建配置类,替代xml配置文件 ```java package com.daijunyi.config;

import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;

@Configuration // 作为替换xml 配置类 @ComponentScan(basePackages = {“com.daijunyi”}) public class SpringConfig { }


2. 测试配置,就得使用AnnotationConfigApplicationContext来加载配置类
```java
package com.daijunyi.test;

import com.daijunyi.config.SpringConfig;
import com.daijunyi.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {

    @Test
    public void test(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        UserService userService = (UserService) context.getBean("userService");
        userService.userService();

    }
}