1   配置文件的方法

我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。

Boss 拥有 Office 和 Car 类型的两个属性:          清单 3. Boss.java

  1. package com.baobaotao;
  2. public class Boss {
  3. private Car car;
  4. private Office office;
  5. // 省略 get/setter
  6. @Override
  7. public String toString() {
  8. return "car:" + car + "/n" + "office:" + office;
  9. }
  10. }

package com.baobaotao;

public class Boss {
private Car car;
private Office office;

// 省略 get/setter

@Override
public String toString() {
return "car:" + car + "/n" + "office:" + office;
}
}        System.out.println必须实现toString方法    我们在 Spring 容器中将 Office 和 Car 声明为 Bean,并注入到 Boss Bean 中:下面是使用传统 XML 完成这个工作的配置文件 beans.xml:          清单 4. beans.xml 将以上三个类配置成 Bean

  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="boss" class="com.baobaotao.Boss">
  7. <property name="car" ref="car"/>
  8. <property name="office" ref="office" />
  9. </bean>
  10. <bean id="office" class="com.baobaotao.Office">
  11. <property name="officeNo" value="002"/>
  12. </bean>
  13. <bean id="car" class="com.baobaotao.Car" scope="singleton">
  14. <property name="brand" value=" 红旗 CA72"/>
  15. <property name="price" value="2000"/>
  16. </bean>
  17. </beans>

<?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">
<bean id="boss" class="com.baobaotao.Boss">
<property name="car" ref="car"/>
<property name="office" ref="office" />
</bean>
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="002"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value=" 红旗 CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>            当我们运行以下代码时,控制台将正确打出 boss 的信息:          清单 5. 测试类:AnnoIoCTest.java

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class AnnoIoCTest {
  4. public static void main(String[] args) {
  5. String[] locations = {"beans.xml"};
  6. ApplicationContext ctx =
  7. new ClassPathXmlApplicationContext(locations);
  8. Boss boss = (Boss) ctx.getBean("boss");
  9. System.out.println(boss);
  10. }
  11. }

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnoIoCTest {

public static void main(String[] args) {
String[] locations = {"beans.xml"};
ApplicationContext ctx =
new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) ctx.getBean("boss");
System.out.println(boss);
}
}
      这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。

2   @Autowired 

Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。

要实现我们要精简程序的目的。需要这样来处理: 
* 在applicationContext.xml中加入:

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

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

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

* 修改在原来注入spirng容器中的bean的方法。       在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法

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

  1. package com.baobaotao;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class Boss {
  4. @Autowired
  5. private Car car;
  6. @Autowired
  7. private Office office;
  8. }

package com.baobaotao;
import org.springframework.beans.factory.annotation.Autowired;

public class Boss {

@Autowired
private Car car;

@Autowired
private Office office;


}

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

  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
  7. <bean class="org.springframework.beans.factory.annotation.
  8. AutowiredAnnotationBeanPostProcessor"/>
  9. <!-- 移除 boss Bean 的属性注入配置的信息 -->
  10. <bean id="boss" class="com.baobaotao.Boss"/>
  11. <bean id="office" class="com.baobaotao.Office">
  12. <property name="officeNo" value="001"/>
  13. </bean>
  14. <bean id="car" class="com.baobaotao.Car" scope="singleton">
  15. <property name="brand" value=" 红旗 CA72"/>
  16. <property name="price" value="2000"/>
  17. </bean>
  18. </beans>

<?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 对方法或构造函数进行标注,如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。来看下面的代码:  对方法

  1. package com.baobaotao;
  2. public class Boss {
  3. private Car car;
  4. private Office office;
  5. @Autowired
  6. public void setCar(Car car) {
  7. this.car = car;
  8. }
  9. @Autowired
  10. public void setOffice(Office office) {
  11. this.office = office;
  12. }
  13. }

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。而下面的使用方法则对构造函数进行标注:

  1. package com.baobaotao;
  2. public class Boss {
  3. private Car car;
  4. private Office office;
  5. @Autowired
  6. public Boss(Car car ,Office office){
  7. this.car = car;
  8. this.office = office ;
  9. }
  10. }

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。

Spring@Autowired注解与自动装配的更多相关文章

  1. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  2. Spring@Autowired注解与自动装配(转发)

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  3. [转] Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  4. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

  5. 使用Spring的JavaConfig 和 @Autowired注解与自动装配

    1 JavaConfig  配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...

  6. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  7. @Resource注解完成自动装配

    @Resource注解是通过名字来自动装配的.在spring中自动装配的模式如果是通过名字来自动装配那么必须保证bean的名字和pojo 的属性名一直. 下面是详细代码:说明了@Resource注解是 ...

  8. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. Spring @Autowired 注解 学习资料

    Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...

随机推荐

  1. OC第一节 —— 类和对象

    一.类和对象的概念 1.1类 自己的定义: 具有相同或相似性质对象的抽象. 1.2 对象 自己的定义: 对象是人们要进行研究的任何物体,从最简单的整数到复杂的飞机 等均可以看做是对象. 举例说明: 类 ...

  2. 微信公众平台开放JS-SDK(微信内网页开发工具包)

    微信公众平台开放JS-SDK(微信内网页开发工具包),这次开放接口是质的飞跃,是对开发者和广大用户一个利好的消息.未来的公众号图文消息会更丰富多彩,准备脑洞大开吧!(第三方平台正式支持接入微信公众平台 ...

  3. poj.1988.Cube Stacking(并查集)

    Cube Stacking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submi ...

  4. 【Go入门教程8】总结(25个关键字)

    这一章我们主要介绍了Go语言的一些语法,通过语法我们可以发现Go是多么的简单,只有二十五个关键字.让我们再来回顾一下这些关键字都是用来干什么的. break    default      func  ...

  5. ASP.NET、C#调用外部可执行exe文件--多种方案

    一. try { //方法一 //调用自己的exe传递参数 //Process proc = new Process(); //proc.StartInfo.FileName = @"D:\ ...

  6. telnet: connect to address 127.0.0.1: Connection refused

    telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用telnet程序,用它连接 ...

  7. iOS开发初级课程

    iOS开发初级课程 针对学员 掌握Objective  C,C或者C++,有语言基础的学员,想从事iOS开发工作. iOS开发那些事-了解iOS开发(8集) 在课程中,我们首先介绍如何使用nib和故事 ...

  8. vim编译运行java程序

    想让你的vim可以编译运行java只要在你的配置文件里面加一句: map <F5> :!javac %&&java %:r <CR> 就可以编译运行java了

  9. iOS开发——UI基础-提示框

    提示框的种类有很多,废话不多说,直接上代码 一.文本提示框 运行结果如下: 代码实现如下: @interface ViewController () // 添加方法 - (IBAction)add; ...

  10. iOS 图片 的 聊天气泡显示 Objective-C

    - (void)viewDidLoad { [super viewDidLoad]; UIImageView *ImageView01 = [[UIImageView alloc] init]; [I ...