zoukankan      html  css  js  c++  java
  • @Autowired注解的使用

    使用 @Autowired 注释的 Boss.java  

    1.package com.baobaotao;    

    import org.springframework.beans.factory.annotation.Autowired;    

      public class Boss {    

         @Autowired   

        private Car car;    

        @Autowired   

        private Office office;    

    此处省略了getter和setter方法。

    在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法

    2.在applicatonContext.xml中 把原来 引用的<porpery >标签也去掉。

    <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->    

      事先在Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。  

        <bean class="org.springframework.beans.factory.annotation.    

            AutowiredAnnotationBeanPostProcessor"/>    

       <!-- 移除 boss Bean 的属性注入配置的信息<property> -->     

        <bean id="boss" class="com.baobaotao.Boss"/>    

         此处省略了<property>配置信息。

        <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,并注入到对应的地方中去。  

     

  • 相关阅读:
    HTML DOM 教程Part5 [DOM 冷门对象] 摘录自W3C School
    Javascript DOM 的节点操作示例
    CSS 教程Part2 [背景、文本、字体](摘录自 W3C School)
    Stream Part.1
    Thread.Join() 方法
    CSS 教程Part3 [列表、表格、轮廓](摘录自 W3C School)
    使用委托异步调用方法让程序并行运行
    JSON 数据格式
    C# WinForm 程序中获取本机IP地址
    微软示例代码 for ManualResetEvent
  • 原文地址:https://www.cnblogs.com/panzai/p/11728296.html
Copyright © 2011-2022 走看看