Spring注入依賴的4個注解
- @Value 注入int、float、String等基本數據類型,只能標注在成員變量、setter方法上。
- @Autowired 按類型自動裝配,可標注在成員變量(官方不推薦)、構造方法、setter方法上。
- @Qualifier 按名稱自動裝配,需要和@Autowired搭配使用,只能標注在成員變量(官方不推薦)、setter方法上。
- @Resource 按名稱或類型自動裝配,需要第三方包 javax.annotation.jar 的支持,只能標注在成員變量、setter方法上。
以上3個注解用於自動裝配其它bean的實例,盡量標注在setter方法上。
復雜類型需要用xml方式注入。
使用spring的注解,需要引入spring-aop.RELEASE.jar。
如果只使用了上面這些依賴注入的注解,需要在xml中啟用注解,還需要配置<bean>:
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config></context:annotation-config> <bean name="b" class="com.chy.bean.B" /> </beans>
如果使用了類注解(@Controller、@Service、@Repository、@Component),直接使用包掃描即可,不必配置<bean>:
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.chy.bean" /> </beans>
包掃描已經包括了啟用注解的功能。
@Value
用於注入基本類型,只能標注在成員變量、setter上,不能標注在構造方法上。
直接標注在成員變量上:(不需要setter方法)
@Component
public class B { @Value("20") private int age;
//...... }
spring會自動將引號中的值轉換為需要的類型。值必須放在引號中。
標注在setter方法上:
public class B { private int age; @Value("20") public void setName(int age) { this.age = age; } //..... }
@Value不能和 參數是該成員變量的構造方法 一同使用。
比如說使用@Value注入了age字段,該類中就不能有A(int age)這個構造方法。
@Autowired
按類型自動裝配,可以標注在成員變量、構造方法、setter方法上,官方不推薦標注在成員變量上。
標注在構造方法上時,可缺省@Autowired,因為使用包掃描時,如果未顯式配置依賴注入,默認使用構造方法的自動裝配(按參數類型)。
@Qualifier
@Component public class A { private B b; @Autowired @Qualifier("b") public void setB(B b) { this.b = b; } public A(B b) { this.b = b; } }
@Qualifier不能單獨用,需要和@Autowired搭配使用。
是按名稱的自動裝配,需要在@Autowired("name")寫上所依賴bean的name。
只能標注在成員變量(官方不推薦)、setter方法上,雖然不能標注在構造方法上,但可以出現對應的構造方法。
@Resource
spring提供了@Resource注解,但並未提供此注解的實現。
@Resource需要第三方包的支持:javax.annotation.jar。
下載地址:http://www.java2s.com/Code/Jar/j/Downloadjavaxannotationapi12sourcesjar.htm
如果使用maven,會自動下載spring依賴的第三方包commons-logging.jar、javax.annotation.jar,無需我們手動下載添加。
@Component public class A { // @Resource(name = "b") @Resource(type = B.class) private B b; public void setB(B b) { this.b = b; } public A(B b) { this.b = b; } }
@Resource可按名稱或按類型自動裝配,可在()中指定。
name的值是String形式,type的值是class形式。
@Component public class A { private B b; @Resource public void setB(B b) { this.b = b; } public A(B b) { this.b = b; } }
未指定規則時,默認先按名稱裝配,找不到滿足要求的bean,再按類型裝配。
@Resource只能標注在成員變量、setter方法上,但可以出現對應的構造方法。