@Autowired 注释可以在 setter 方法中被用于自动连接 bean。

你可以在 XML 文件中的 setter 方法中使用 @Autowired 注释来除去 元素。

当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中执行 byType 自动连接。

一个示例

  • 新建Spring项目

  • 创建 Java 类 TextEditor, SpellChecker 和 MainApp

这里是 TextEditor.java 文件的内容:

package hello;
import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
private SpellChecker spellChecker; @Autowired
public void setSpellChecker(SpellChecker spellChecker){
System.out.println("Inside setSpellChecker.");
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker(){
return spellChecker;
} public void spellCheck(){
spellChecker.checkSpelling();
}
}

下面是另一个依赖的类文件 SpellChecker.java 的内容:

package hello;

public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor");
}
public void checkSpelling(){
System.out.println("Inside checkSpelling.");
}
}

下面是 MainApp.java 文件的内容:

package hello;
//import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}

下面是配置文件 Beans.xml:

<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="hello.TextEditor" >
</bean> <!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="hello.SpellChecker"> </bean> </beans>

运行结果如下:

Inside SpellChecker constructor
Inside setSpellChecker.
Inside checkSpelling.

属性中的 @Autowired

你可以在属性中使用 @Autowired 注释来除去 setter 方法。

当时使用 为自动连接属性传递的时候,Spring 会将这些传递过来的值或者引用自动分配给那些属性。

利用在属性中 @Autowired 的用法,你的 TextEditor.java 文件将变成如下所示:

package hello;
import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
@Autowired
private SpellChecker spellChecker;
public TextEditor(){
System.out.println("Inside TextEditor constructor");
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker(){
return spellChecker;
} public void spellCheck(){
spellChecker.checkSpelling();
}
}

下面是配置文件 Beans.xml:

<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean -->
<bean id="textEditor" class="hello.TextEditor" >
</bean> <!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="hello.SpellChecker"> </bean> </beans>

运行结果如下

Inside TextEditor constructor
Inside SpellChecker constructor
Inside checkSpelling.

构造函数中的 @Autowired

也可以在构造函数中使用 @Autowired。

一个构造函数 @Autowired 说明当创建 bean 时,即使在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动连接。

下面的示例。

这里是 TextEditor.java 文件的内容:

package hello;
import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor");
this.spellChecker = spellChecker;
} public void spellCheck(){
spellChecker.checkSpelling();
}
}

下面是配置文件 Beans.xml:

<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="hello.TextEditor" >
</bean> <!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="hello.SpellChecker"> </bean> </beans>

运行结果如下

Inside SpellChecker constructor
Inside TextEditor constructor
Inside checkSpelling.

@Autowired 的(required=false)选项

默认情况下,@Autowired 注释意味着依赖是必须的,它类似于 @Required 注释,然而,你可以使用 @Autowired 的 (required=false) 选项关闭默认行为。

即使你不为 age 属性传递任何参数,下面的示例也会成功运行,但是对于 name 属性则需要一个参数。

你可以自己尝试一下这个示例,因为除了只有 Student.java 文件被修改以外,它和 @Required 注释示例是相似的。

Student.java 文件内容:

package hello;
import org.springframework.beans.factory.annotation.Autowired; public class Student {
private int age;
String name;
@Autowired(required = false)
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
@Autowired
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
} }

其中,setAge()方法使用 @Autowired 的 (required=false) 选项关闭默认行为。

配置文件 Beans.xml如下

<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean without constructor-arg -->
<bean id="student" class="hello.Student" >
<property name="name" value="番茄"></property>
</bean> </beans>

这里,,没有XML文件中设置属性age的值,仍能正确运行该程序,结果如下:

name: 番茄
age: 0

每天学习一点点,每天进步一点点。

最新文章

  1. word-break|overflow-wrap|word-wrap——CSS英文断句浅析
  2. faster-rcnn(testing): ubuntu14.04+caffe+cuda7.5+cudnn5.1.3+opencv3.0+matlabR2014a环境搭建记录
  3. block的使用
  4. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON FillUpShape2
  5. 利用curl抓取远程页面内容
  6. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.5
  7. 1006: [HNOI2008]神奇的国度
  8. mac 环境下使用virtual box 虚拟机(win7)与主机之间互相ping通
  9. (转载)C# 编程 使用可空类型
  10. hdu_5734_Acperience
  11. java的两种异常runtimeException和checkedException
  12. PHP使用hash_algos函数计算哈希值,之间的性能排序
  13. attr与prop html与text
  14. [零] Java 语言运行原理 JVM原理浅析 入门了解简介 Java语言组成部分 javap命令使用
  15. Thinkpad 小红点飘移的不完美解决办法
  16. MySQL数据库(三)索引总结
  17. 咏南中间件支持DELPHI6及以上版本开发的客户端
  18. java判断一个字符串是否包含某个字符
  19. WSDL解析
  20. hihocoder [Offer收割]编程练习赛12 [1494] ---- 一面砖墙

热门文章

  1. 怎么将swagger API导出为HTML或者PDF
  2. Spring Cloud OpenFeign使用教程
  3. Spring5参考指南:AspectJ注解
  4. 《Java 开发从入门到精通》—— 2.3 使用IDE工具序
  5. db2 锁表
  6. Hadoop学习笔记(一)——安装与配置
  7. nginx经验分享
  8. C语言编程入门题目--No.8
  9. 学习笔记之pip的基本使用
  10. C - A Plug for UNIX POJ - 1087 网络流