@autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。
@autowired 注释可以在 setter 方法中被用于自动连接 bean,就像 @autowired 注释,容器,一个属性或者任意命名的可能带有多个参数的方法。
你可以在 xml 文件中的 setter 方法中使用 @autowired 注释来除去 元素。当 spring遇到一个在 setter 方法中使用的 @autowired 注释,它会在方法中视图执行 bytype 自动连接。
让我们使 eclipse ide 处于工作状态,然后按照如下步骤创建一个 spring 应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名为 springexample 的项目,并且在所创建项目的 src 文件夹下创建一个名为 com.tutorialspoint 的包。 |
2 | 使用 add external jars 选项添加所需的 spring 库文件,就如在 spring hello world example 章节中解释的那样。 |
3 | 在 com.tutorialspoint 包下创建 java 类 texteditor, spellchecker 和 mainapp。 |
4 | 在 src 文件夹下创建 beans 配置文件 beans.xml。 |
5 | 最后一步是创建所有 java 文件和 bean 配置文件的内容,并且按如下解释的那样运行应用程序。 |
这里是 texteditor.java 文件的内容:
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.autowired;
public class texteditor {
private spellchecker spellchecker;
@autowired
public void setspellchecker( spellchecker spellchecker ){
this.spellchecker = spellchecker;
}
public spellchecker getspellchecker( ) {
return spellchecker;
}
public void spellcheck() {
spellchecker.checkspelling();
}
}
下面是另一个依赖的类文件 spellchecker.java 的内容:
package com.tutorialspoint;
public class spellchecker {
public spellchecker(){
system.out.println("inside spellchecker constructor." );
}
public void checkspelling(){
system.out.println("inside checkspelling." );
}
}
下面是 mainapp.java 文件的内容:
package com.tutorialspoint;
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="com.tutorialspoint.texteditor">
</bean>
<!-- definition for spellchecker bean -->
<bean id="spellchecker" class="com.tutorialspoint.spellchecker">
</bean>
</beans>
一旦你已经完成的创建了源文件和 bean 配置文件,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:
inside spellchecker constructor.
inside checkspelling.
你可以在属性中使用 @autowired 注释来除去 setter 方法。当时使用 为自动连接属性传递的时候,spring 会将这些传递过来的值或者引用自动分配给那些属性。所以利用在属性中 @autowired 的用法,你的 texteditor.java 文件将变成如下所示:
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.autowired;
public class texteditor {
@autowired
private spellchecker spellchecker;
public texteditor() {
system.out.println("inside texteditor constructor." );
}
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="com.tutorialspoint.texteditor">
</bean>
<!-- definition for spellchecker bean -->
<bean id="spellchecker" class="com.tutorialspoint.spellchecker">
</bean>
</beans>
一旦你在源文件和 bean 配置文件中完成了上面两处改变,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:
inside texteditor constructor.
inside spellchecker constructor.
inside checkspelling.
你也可以在构造函数中使用 @autowired。一个构造函数 @autowired 说明当创建 bean 时,即使在 xml 文件中没有使用 元素配置 bean ,构造函数也会被自动连接。让我们检查一下下面的示例。
这里是 texteditor.java 文件的内容:
package com.tutorialspoint;
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="com.tutorialspoint.texteditor">
</bean>
<!-- definition for spellchecker bean -->
<bean id="spellchecker" class="com.tutorialspoint.spellchecker">
</bean>
</beans>
一旦你在源文件和 bean 配置文件中完成了上面两处改变,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:
inside spellchecker constructor.
inside texteditor constructor.
inside checkspelling.
默认情况下,@autowired 注释意味着依赖是必须的,它类似于 @required 注释,然而,你可以使用 @autowired 的 (required=false) 选项关闭默认行为。
即使你不为 age 属性传递任何参数,下面的示例也会成功运行,但是对于 name 属性则需要一个参数。你可以自己尝试一下这个示例,因为除了只有 student.java 文件被修改以外,它和 @required 注释示例是相似的。
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.autowired;
public class student {
private integer age;
private string name;
@autowired(required=false)
public void setage(integer age) {
this.age = age;
}
public integer getage() {
return age;
}
@autowired
public void setname(string name) {
this.name = name;
}
public string getname() {
return name;
}
}