• Spring Annotation(三)


    使用 @Autowired 注释

    Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。来看一下使用 @Autowired 进行成员变量自动注入的代码:


    清单 6. 使用 @Autowired 注释的 Boss.java

    package com.baobaotao; 
    import org.springframework.beans.factory.annotation.Autowired; 
    public class Boss { 
        @Autowired 
        private Car car; 

        @Autowired 
        private Office office; 

        … 
    }

    Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。


    清单 7. 让 @Autowired 注释工作起来

    <?xml version="1.0" encoding="UTF-8" ?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 

        <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> 
        <bean class="org.springframework.beans.factory.
        annotation. AutowiredAnnotationBeanPostProcessor"/> 

        <!-- 移除 boss Bean 的属性注入配置的信息 --> 
        <bean id="boss" class="com.baobaotao.Boss"/> 

        <bean id="office" class="com.baobaotao.Office"> 
            <property name="officeNo" value="001"/> 
        </bean> 
        <bean id="car" class="com.baobaotao.Car" scope="singleton"> 
            <property name="brand" value=" 红旗 CA72"/> 
            <property name="price" value="2000"/> 
        </bean> 
    </beans>

    这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。

    按照上面的配置,Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法(setCar() 和 setOffice())从 Boss 中删除。

    当然,您也可以通过 @Autowired 对方法或构造函数进行标注,来看下面的代码:


    清单 8. 将 @Autowired 注释标注在 Setter 方法上

    package com.baobaotao; 
    public class Boss { 
        private Car car; 
        private Office office; 

        @Autowired 
        public void setCar(Car car) { 
            this.car = car; 
        } 

        @Autowired 
        public void setOffice(Office office) { 
            this.office = office; 
        } 

        … 
    }

    这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:


    清单 9. 将 @Autowired 注释标注在构造函数上

    package com.baobaotao; public class Boss { 
        private Car car; 
        private Office office; 

        @Autowired 
        public Boss(Car car ,Office office){ 
            this.car = car; 
            this.office = office ; 
        } 
        … 
    }

    由于 Boss() 构造函数有两个入参,分别是 car 和 office@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。

  • 相关阅读:
    DateTime.Compare how to check if a date is less than 30 days old?
    疫情防控记录
    vulnerable process has been exploited and is running
    判断NBIOT的网络信号
    FortiGate 防火墙常用命令
    常见软路由 网卡 和 处理器 对比
    telnet ssh 默认程序关联
    地磁,干扰及校准,椭球拟合
    FortiGate SDWAN 多链路负载均衡
    All In One Runtimes 3.9.2.1 Unattended
  • 原文地址:https://www.cnblogs.com/yujy/p/2795199.html
Copyright © 2020-2023  润新知