当前位置: 首页 > 文档资料 > Spring 中文教程 >

@Autowired

优质
小牛编辑
131浏览
2023-12-01

@Autowired注释提供了对自动装配应在何处以及如何完成的更精细控制。 @Autowired注释可用于在setter方法上自动装配bean,就像@Required注释,构造函数,具有任意名称和/或多个参数的属性或方法一样。

@Autowired on Setter Methods

您可以在setter方法上使用@Autowired注释来删除XML配置文件中的“property”元素。 当Spring找到与setter方法一起使用的@Autowired注释时,它会尝试对该方法执行byType自动装配。

例子 (Example)

让我们使用Eclipse IDE并按照以下步骤创建Spring应用程序 -

描述
1创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包cn.xnip
2使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3cn.xnip包下创建Java类TextEditorMainAppMainApp
4src文件夹下创建Beans配置文件Beans.xml
5最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是TextEditor.java文件的内容 -

package cn.xnip;
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 cn.xnip;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling(){
      System.out.println("Inside checkSpelling." );
   }
}

以下是MainApp.java文件的内容 -

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

完成创建源和bean配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,这将打印以下消息 -

Inside SpellChecker constructor.
Inside checkSpelling.

@Autowired on Properties

您可以在属性上使用@Autowired注释来摆脱setter方法。 当您使用“属性”传递自动装配属性的值时,Spring将自动为传递的值或引用分配这些属性。 因此,在属性上使用@Autowired时, TextEditor.java文件将变为如下 -

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

完成源和bean配置文件中的上述两个更改后,让我们运行该应用程序。 如果您的应用程序一切正常,这将打印以下消息 -

Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.

@Autowired on Constructors

您也可以将@Autowired应用于构造函数。 构造函数@Autowired注释表明在创建bean时应该自动装配构造函数,即使在XML文件中配置bean时没有使用元素。 我们来看看下面的例子。

这是TextEditor.java文件的内容 -

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

完成源和bean配置文件中的上述两个更改后,让我们运行该应用程序。 如果您的应用程序一切正常,这将打印以下消息:

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

@Autowired with(required = false)选项

默认情况下,@ Autowired注释意味着需要与@Required注释类似的依赖关系,但是,您可以通过对@Autowired使用(required=false)选项来关闭默认行为。

即使您没有为age属性传递任何值,但以下示例仍然有效,但仍需要name属性。 您可以自己尝试此示例,因为这与@Required注释示例类似,只是更改了Student.java文件。

package cn.xnip;
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;
   }
}