Spring的@Autowired注解
该@Autowired注解提供了在哪里以及如何自动装配应实现更细粒度的控制。@Autowired注解可用于在setter方法上自动连接bean,就像@Required注释,构造函数,属性或具有任意名称和/或多个参数的方法一样。
Setter方法中的@Autowired
您可以在setter方法上使用@Autowired注释来摆脱XML配置文件中的<property>元素。当Spring发现使用setter方法的@Autowired注释时,它会尝试在方法上执行autowire="byType"的自动连接。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testannotationautowired</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testannotationautowired</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> </dependencies>
</project>
SpellChecker.java:
package com.jsoft.testspring.testannotationautowired; public class SpellChecker {
public SpellChecker(){
System.out.println("SpellChecker无参数构造函数初始化");
} public void checkSpelling(){
System.out.println("SpellChecker检查方法");
}
}
TextEditor.java:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
private SpellChecker spellChecker;
private String name; @Autowired
public void setSpellChecker(SpellChecker spellChecker){
System.out.println("TextEditor通过setter初始化");
this.spellChecker = spellChecker;
} public void spellCheck() {
this.spellChecker.checkSpelling();
} public void setName(String name){
this.name = name;
} public void getName(){
System.out.println(this.name);
} }
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="spellChecker" class="com.jsoft.testspring.testannotationautowired.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testannotationautowired.TextEditor">
<property name="name" value="Hello World!"></property>
</bean> </beans>
此时可以看见textEditor的bean上少了一个<property>标签(元素),但是也能运行正常,因为在TextEditor类中标记了@Autowired的注解,自动匹配去匹配相同类型的bean,与在bean中设置autowire="byType"属性保持一致。
运行结果:
而原始的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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="spellChecker" class="com.jsoft.testspring.testannotationautowired.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testannotationautowired.TextEditor">
<property name="spellChecker" ref="spellChecker"></property>
<property name="name" value="Hello World!"></property>
</bean> </beans>
属性中的@Autowired
您可以在属性上使用@Autowired注解来摆脱setter方法。当你使用<property>传递自动连线属性的值时,Spring将自动为传递的值或引用分配这些属性。因此,随着@Autowired对属性的使用,您的TextEditor.java文件将如下所示:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
@Autowired
private SpellChecker spellChecker;
private String name; public void spellCheck() {
this.spellChecker.checkSpelling();
} public void setName(String name){
this.name = name;
} public void getName(){
System.out.println(this.name);
} }
可以看出上面代码上SpellChecker的setter方法已经去掉了,而在SpellChecker的私有属性上加入了@Autowired的注解。它会自动创建setter方法。
而beans.xml不需要改变。
运行结果如下:
构造函数中的@Autowired
您也可以将@Autowired应用于构造函数。构造函数@Autowired注解表示构造函数在创建bean时应该是自动连线的,即使在XML文件中配置bean时也不使用<constructor-arg>元素。例子如下:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
private SpellChecker spellChecker;
private String name; @Autowired
public TextEditor(SpellChecker spellChecker){
System.out.println("TextEditor通过初始化方法赋值SpellChecker");
this.spellChecker = spellChecker;
} public void spellCheck() {
this.spellChecker.checkSpelling();
} public void setName(String name){
this.name = name;
} public void getName(){
System.out.println(this.name);
} }
而beas.xml不用改变。
测试结果:
@Autowired的(required=false)选项
默认情况下,@Autowired注解意味着依赖是必须的,它类似于@Required注解,然而,你可以使用@Autowired的(required=false)选项关闭默认行为。例子如下:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
private SpellChecker spellChecker;
private String name; @Autowired
public TextEditor(SpellChecker spellChecker){
System.out.println("TextEditor通过初始化方法赋值SpellChecker");
this.spellChecker = spellChecker;
} public void spellCheck() {
this.spellChecker.checkSpelling();
} @Autowired(required=false)
public void setName(String name){
this.name = name;
} public void getName(){
System.out.println(this.name);
} }
指定了SpellChecker的setter方法不是必须的,那么也修改beans.xml文件,使其不传入此值。修改后的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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="spellChecker" class="com.jsoft.testspring.testannotationautowired.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testannotationautowired.TextEditor"> </bean> </beans>
测试结果:
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test12/testannotationautowired
最新文章
- GetWord 3.3 屏幕取词
- WCF X.509验证
- GIT如何添加权限模块
- 入门:JavaWeb Cookie
- hdu 2066 一个人的旅行 Dijkstra
- Cookie的属性(cookie的设置、获取和删除)
- JS知识点概况
- 百度editor调用【图片上传阿里云】
- 基于三星I9250演示自己弄的Miracast功能-手机对手机
- sql server事物控制
- python继承的实例
- angular中的$q服务
- JavaScript函数节流和函数防抖之间的区别
- 面试 9:Java 玩转冒泡排序
- hive安装详解
- Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment:
- JAVA线程池ScheduledExecutorService周期性地执行任务 与单个Thread周期性执行任务的异常处理
- Jenkins 安装简记录
- WinForm 随手记
- Java泛型与Restlet客户端
热门文章
- FPGA-信号边缘检测
- codevs 1422 河城荷取
- Python 使用re模块实现正则表达式
- 安装 Zend Studio 报错:0x80070666
- 位(bit)、字节(byte)、字
- 5 秒创建 k8s 集群[转]
- syntax error : missing &#39;;&#39; before identifier
- 复制webp图片到word || 微信webp图片不能复制 || 如何复制webp到word
- Java 对象的创建以及类加载
- Properties类操作.properties配置文件方法总结