课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring @Autowired 注释

当前位置:免费教程 » Java相关 » Spring

@Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。

@Autowired 注释可以在 setter 方法中被用于自动连接 bean,就像 @Autowired 注释,容器,一个属性或者任意命名的可能带有多个参数的方法。

Setter 方法中的 @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, SpellCheckerMainApp
4 src 文件夹下创建 Beans 配置文件 Beans.xml
5 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并且按如下解释的那样运行应用程序。

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

  1. package com.tutorialspoint;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class TextEditor {
  4. private SpellChecker spellChecker;
  5. @Autowired
  6. public void setSpellChecker( SpellChecker spellChecker ){
  7. this.spellChecker = spellChecker;
  8. }
  9. public SpellChecker getSpellChecker( ) {
  10. return spellChecker;
  11. }
  12. public void spellCheck() {
  13. spellChecker.checkSpelling();
  14. }
  15. }

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

  1. package com.tutorialspoint;
  2. public class SpellChecker {
  3. public SpellChecker(){
  4. System.out.println("Inside SpellChecker constructor." );
  5. }
  6. public void checkSpelling(){
  7. System.out.println("Inside checkSpelling." );
  8. }
  9. }

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

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  7. TextEditor te = (TextEditor) context.getBean("textEditor");
  8. te.spellCheck();
  9. }
  10. }

下面是配置文件 Beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <context:annotation-config/>
  12.  
  13. <!-- Definition for textEditor bean without constructor-arg -->
  14. <bean id="textEditor" class="com.tutorialspoint.TextEditor">
  15. </bean>
  16.  
  17. <!-- Definition for spellChecker bean -->
  18. <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
  19. </bean>
  20.  
  21. </beans>

一旦你已经完成的创建了源文件和 bean 配置文件,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:

  1. Inside SpellChecker constructor.
  2. Inside checkSpelling.

属性中的 @Autowired

你可以在属性中使用 @Autowired 注释来除去 setter 方法。当时使用 为自动连接属性传递的时候,Spring 会将这些传递过来的值或者引用自动分配给那些属性。所以利用在属性中 @Autowired 的用法,你的 TextEditor.java 文件将变成如下所示:

  1. package com.tutorialspoint;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class TextEditor {
  4. @Autowired
  5. private SpellChecker spellChecker;
  6. public TextEditor() {
  7. System.out.println("Inside TextEditor constructor." );
  8. }
  9. public SpellChecker getSpellChecker( ){
  10. return spellChecker;
  11. }
  12. public void spellCheck(){
  13. spellChecker.checkSpelling();
  14. }
  15. }

下面是配置文件 Beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <context:annotation-config/>
  12.  
  13. <!-- Definition for textEditor bean -->
  14. <bean id="textEditor" class="com.tutorialspoint.TextEditor">
  15. </bean>
  16.  
  17. <!-- Definition for spellChecker bean -->
  18. <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
  19. </bean>
  20.  
  21. </beans>

一旦你在源文件和 bean 配置文件中完成了上面两处改变,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:

  1. Inside TextEditor constructor.
  2. Inside SpellChecker constructor.
  3. Inside checkSpelling.

构造函数中的 @Autowired

你也可以在构造函数中使用 @Autowired。一个构造函数 @Autowired 说明当创建 bean 时,即使在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动连接。让我们检查一下下面的示例。

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

  1. package com.tutorialspoint;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class TextEditor {
  4. private SpellChecker spellChecker;
  5. @Autowired
  6. public TextEditor(SpellChecker spellChecker){
  7. System.out.println("Inside TextEditor constructor." );
  8. this.spellChecker = spellChecker;
  9. }
  10. public void spellCheck(){
  11. spellChecker.checkSpelling();
  12. }
  13. }

下面是配置文件 Beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <context:annotation-config/>
  12.  
  13. <!-- Definition for textEditor bean without constructor-arg -->
  14. <bean id="textEditor" class="com.tutorialspoint.TextEditor">
  15. </bean>
  16.  
  17. <!-- Definition for spellChecker bean -->
  18. <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
  19. </bean>
  20.  
  21. </beans>

一旦你在源文件和 bean 配置文件中完成了上面两处改变,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:

  1. Inside TextEditor constructor.
  2. Inside SpellChecker constructor.
  3. Inside checkSpelling.

@Autowired 的(required=false)选项

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

即使你不为 age 属性传递任何参数,下面的示例也会成功运行,但是对于 name 属性则需要一个参数。你可以自己尝试一下这个示例,因为除了只有 Student.java 文件被修改以外,它和 @Required 注释示例是相似的。

  1. package com.tutorialspoint;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class Student {
  4. private Integer age;
  5. private String name;
  6. @Autowired(required=false)
  7. public void setAge(Integer age) {
  8. this.age = age;
  9. }
  10. public Integer getAge() {
  11. return age;
  12. }
  13. @Autowired
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. }
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号