zoukankan      html  css  js  c++  java
  • Spring 依赖注入(注解方式)

     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方法上,但可以出现对应的构造方法。

  • 相关阅读:
    哎,这两天的心情,真是太差了
    昨天跌停价冲进去,今天继续跌停
    好几天没有干正事了,是否已经堕落了?
    继续通宵加班
    Map集合的遍历方式
    List,Set,Map,propertes注入方式
    java异常捕获以及处理
    List、Set、Map、数组互转
    Java并发编程:深入剖析ThreadLocal
    Exceotion与RuntimeException的区别
  • 原文地址:https://www.cnblogs.com/chy18883701161/p/11117797.html
Copyright © 2011-2022 走看看