Spring @Autowired注解
@Autowired在Setter方法:
您可以使用@Autowired注解放在setter方法来摆脱在XML配置文件中的<property>元素。当Spring发现setter方法使用@ Autowired注解,它会尝试对方法进行byType的自动装配。
例子
我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
步骤 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes TextEditor, SpellChecker and MainApp under the com.yiibaipackage. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
这里是TextEditor.java文件的内容:
package com.yiibai; 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.yiibai; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling(){ System.out.println("Inside checkSpelling." ); } }
以下是MainApp.java文件的内容:
package com.yiibai; 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.yiibai.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,将打印以下信息:
Inside SpellChecker constructor. Inside checkSpelling.
@Autowired 在 Properties:
您可以使用@Autowired注解的属性摆脱setter方法。当使用<property>通过自动装配属性的值Spring会自动分配这些属性与传递的值或引用。因此,@Autowired上的属性的使用TextEditor.javafile将如下:
package com.yiibai; 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.yiibai.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,将打印以下信息:
Inside TextEditor constructor. Inside SpellChecker constructor. Inside checkSpelling.
@Autowired 在构造函数:
可以将@Autowired在构造函数中。构造函数和@Autowired注解表明该构造函数应该在创建这个bean时,自动装配,即使在配置bean的XML文件没有<constructor-arg>元素被使用。让我们检查下面的例子。
这里是TextEditor.java文件的内容:
package com.yiibai; 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.yiibai.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,将打印以下信息:
Inside TextEditor constructor. Inside SpellChecker constructor. Inside checkSpelling.
@Autowired 与 (required=false) 选项
默认情况下,@ Autowired注解意味着依赖关系需要类似@ Required注解,但是,你可以通过使用(required=false)选项使用@Autowired的关闭的默认行为。
下面的例子将工作,即使你没有通过age属性的任何值,但它仍然会为name属性的要求。你自己可以试试这个例子,因为这是使用@Required注解例子不同的是仅Student.java文件已被更改。
package com.yiibai; 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; } }