Spring XML配置--使用注解装配(@Atutowired、@Inject、@Resource)

1.装配术语

创建应用对象之间协作关系的行为通常被称为装配

2.使用注解装配

Spring是从Spring2.5开始引入使用注解自动装配的。

Spring容器是默认禁用注解装配的,因此如果要使用Spring的注解装配,你必须启用它。启用方式:使用Spring的context命名空间配置中的<context:annotation-config>元素,配置启用如下:

[html]  view plain  copy
  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:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans  
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context   
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  10.       
  11.     <!--   
  12.         spring默认是禁用注解装配,添加下行代码可启用  
  13.         spring支持几种不同的用于自动装配的注解  
  14.         Spring自带的@Autowired  
  15.         JSR-330的@Inject  
  16.         JSR-250的@Resource   
  17.     -->  
  18.     <context:annotation-config />  
  19. </beans>  

一旦配置完成,我们就可以对代码添加注解,标识Spring应该为属性、方法和构造器进行自动装配

好了,我们可以看到上述的配置的自动装配配置中,spring支持几种不同的用于自动装配的注解,接下来我就详解下使用各个不同注解进行自动装配。

三者的区别,我是网上搜索的,就截图就OK了:


3.   4种类型的自动装配

当涉及自动装配Bean的依赖关系时,Spring有多种处理的方式。因此,Spring提供了4种各具特色的自动装配策略。

a.byName:把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中。如果没有,则不进行装配;

b.byType:把与Bean的属性具有相同类型的其它Bean自动装配到Bean的对应属性中。如果没有,则不进行装配;

c.constructor:把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中;

d.autodetect:首先尝试constructor进行自动装配。如果失败,再尝试使用byType进行自动装配;

4.Spring自带的@Autowired

首先,@Autowired可以对Bean的属性可以进行多种装配方式,比如属性的setter方法、Bean引用的任意方法、构造器以及可以使用@Autowired直接标注属性,并删除setter方法,比如:

注:在执行@Autowired注解时,Spring就会尝试对该方法执行byType自动装配

HelloWord对象有一个helloChina属性的对象,我们要对helloChina进行注解装配!

[java]  view plain  copy
  1. 1.setter方法  
  2. @Autowired  
  3. public void setHelloChina(HelloChina helloChina){  
  4.     this.helloChina=helloChina;  
  5. }  
  6. 2.任意方法  
  7. @Autowired  
  8. public void xx(HelloChina helloChina){  
  9.     this.helloChina=helloChina;  
  10. }  
  11. 3.构造器  
  12. @Autowired  
  13. public HelloWord(HelloChina helloChina){  
  14.     this.helloChina=helloChina;  
  15. }  
  16. 4.删除setter方法  
  17. @Autowired  
  18. private HelloChina helloChina;  

看,@Autowired甚至不会受限于private关键字,那是不是@Autowired注解没有任何限制了呢?

其实并不是这样的,@Autowired注解的确存在两种阻碍我们工作的场景。具体来说,应该中必须只能有一个Bean适合装配到@Autowired注解所标识的属性或参数中。如果没有匹配的Bean,或者存在多个匹配的Bean,@Autowired注解就会遇到一些麻烦(异常:NoSuchBeanDefinitionException)。

这类型麻烦的解决方案如下:

a.可选的自动装配

属性不一定非要装配,null值也即可接受:

[java]  view plain  copy
  1. @Autowired(required=false)  
  2. private HelloChina helloChina;  
表示的是,如果没有找到HelloChina进行装配,则helloChina为null值;

注:required属性可以用于@Autowired注解所使用的任意地方。但用于构造器装配时,只有一个构造器可以将@Autowired的required属性设置为true。其他使用@Autowired注解所标识的构造器只能将required属性设置为false。此外,使用@Autowired标识多个构造器时,Spring就会从所有满足装配条件的构造器中选择入参数最多的那个构造器;

b.限定歧义性的依赖

当Bean的属性有多个适合自动装配的Bean时,那么@Autowired应该使用那个Bean呢?这时@Autowired就不知道了。

这时为了确定,我们需要装配的Bean时,可以配合使用@Qualifier来明确指定名为xxName的Bean。

[java]  view plain  copy
  1. @Autowired  
  2. @Qualifier("xxName")  
  3. private HelloChina helloChina;  
如上所示,@Qualifier注解尝试注入ID为xxName的Bean。
原理,也即是将byType自动装配转换为byName自动装配。

@Qualifier限定器,还可以自个创建,即满足不了我们的需求时,这时就不介绍了,有需要的google去吧;

5.JSR-330的@Inject ( javax.inject.Inject )

maven依赖:

<dependency>
   <groupId>javax.inject</groupId>
   <artifactId>javax.inject</artifactId>
   <version>1</version>
</dependency>

为了统一各种依赖注入框架的编程模型,JCP最近发布了java依赖注入规范,JCP将其称为JSR-330,@Inject注解是JSR-330的核心部件。该注解几乎可以完全替代Spring的@Autowired注解。所以除了使用Spring特定的@Autowired注解,我们可以选择@Inject

和@Autowired一个,@Inject可以用来自动装配属性、方法和构造器;与@Autowired不同的是,@Inject没有required属性。因此@Inject注解注入的依赖关系必须存在,否则报异常。

与@Autowired一样,@Inject有自己限定的方法,即处理限定歧义性的依赖,配置如下:

[java]  view plain  copy
  1. @Autowired  
  2. @Named("xxName")  
  3. private HelloChina helloChina;  
另外,@Inject还可以创建自己的Qualifier,这里就不扯了....

注:实际上,@Named注解就是一个使用@Qualifier注解所标注的注解

附上注解的定义,使用位置

-------------------@Autowired的使用--------------------------------------
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    boolean required() default true;
}

-------------------@Inject的使用--------------------------------------
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Inject {
}

-------------------@Qualifier的使用--------------------------------------
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Qualifier {
    String value() default "";
}

-------------------@Resource的使用--------------------------------------
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
    String name() default "";
    String lookup() default "";
    Class type() default java.lang.Object.class;
    enum AuthenticationType {
            CONTAINER,
            APPLICATION
    }
    AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
    boolean shareable() default true;
    String mappedName() default "";
    String description() default "";
}

-------------------@Named的使用--------------------------------------
@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Named {
    String value() default "";
}


猜你喜欢

转载自blog.csdn.net/qq_31459039/article/details/80109825