Spring5(四)——注解配置IOC、DI

一、注解注入

1、注册对象

  @Component:用于注册普通组件bean
  @Service:用于注册业务层bean
  @Controller:用于注册控制层bean
  @Repository:用于注册持久层bean
  上面四个注解功能是一样的,都可以用来创建 bean 实例。看一下源码就知道,他们其实都是Component。业界有个默认的规定,不同的注解用于不同的层,上面已说明了。其实随便用对代码也一样,只是大家默认这样用,就遵守一下。源码:

 1 @Target(ElementType.TYPE)
 2 @Retention(RetentionPolicy.RUNTIME)
 3 @Documented
 4 public @interface Component {
 5    String value() default "";
 6 }
 7 
 8 @Target({ElementType.TYPE})
 9 @Retention(RetentionPolicy.RUNTIME)
10 @Documented
11 @Component
12 public @interface Service {
13    String value() default "";
14 }
15 
16 @Target({ElementType.TYPE})
17 @Retention(RetentionPolicy.RUNTIME)
18 @Documented
19 @Component
20 public @interface Controller {
21    String value() default "";
22 }
23 
24 @Target({ElementType.TYPE})
25 @Retention(RetentionPolicy.RUNTIME)
26 @Documented
27 @Component
28 public @interface Repository {
29    String value() default "";
30 }

  在注解里面 value 属性值可以省略不写,默认值是类名称首字母小写,Person --> person 。

  包扫描的方式,下面三种看情况使用。

 1 <!-- 组件扫描,默认扫描 包下 所有含有注解的类 -->
 2 <context:component-scan base-package="com.lx.spring.day6"/>
 3 
 4 
 5 <!-- use-default-filters="false" : 表示现在不使用默认 filter,自己配置 filter
 6      context:include-filter : 设置扫描哪些内容 -->
 7 <!-- include:包括。也就是只扫描 @Controller 注解的类 -->
 8 <context:component-scan base-package="com.lx.spring.day6" use-default-filters="false">
 9     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
10 </context:component-scan>
11 
12 
13 <!-- context:exclude-filter: 设置哪些内容不进行扫描 -->
14 <!-- exclude:不包括。也就是扫描包下 所有含有注解的类 除开 @Controller 注解的类 -->
15 <context:component-scan base-package="com.lx.spring.day6">
16     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
17 </context:component-scan>

2、注入属性

  @Autowired:根据属性类型进行自动装配

1 @Component 等价于 @Component(value = "person")
2 public class Person {
3     @Autowired
4     private Stu stu;
5 }

  @Qualifier:根据名称进行装配 
  通常这个 @Qualifier 注解的使用,和上面@Autowired一起使用。(推荐使用)

 1 // 1.注册时指定名称
 2 @Component(value = "kkk")
 3 public class Stu {
 4 
 5     public void show() {
 6         System.out.println("---show--Stu--");
 7     }
 8 }
 9 
10 @Component
11 public class Person {
12 
13     @Autowired
14     // 2.使用时指定名称(这里的名称就必须和前面注册时指定的名称一致)
15     @Qualifier(value = "kkk")
16     private Stu stu;
17 
18     public void showP() {
19         System.out.println("---showP--");
20         stu.show();
21     }
22 
23 }

  @Resource:可以根据类型注入,也可以根据名称注入。用法和前面两个一样,但是不推荐使用。因为它是javax包下的。

  import javax.annotation.Resource;

  @Value:注入普通类型属性

1 @Value("abckkk")
2 private String name;

二、完全注解开发

1、用配置类代替xml配置文件

1 @Configuration
2 @ComponentScan(basePackages = {"com.lx.spring.day6"})
3 public class SpringConfig {
4     
5 }

  @Configuration:告诉Spring,这是一个配置类,等价于xml文件。
  @ComponentScan(basePackages = {"com.lx.spring.day6"}):告诉Spring,扫描包,等价于在xml中写:

  <context:component-scan base-package="com.lx.spring.day6"/>

2、加载配置类

 1 public class Main {
 2     public static void main(String[] args) {
 3         //加载配置类
 4         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
 5         Person person = context.getBean(Person.class);
 6 
 7         System.out.println(person);
 8         person.showP();
 9     }
10 }
posted @ 2020-10-19 21:47  Craftsman-L  阅读(93)  评论(0编辑  收藏  举报