微信分享接口网站开发 php,网站优秀作品,页面设计素材网站,长沙教育类网站建设转载自 Spring Autowired 注释
Spring Autowired 注释
Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。
Autowired 注释可以在 setter 方法中被用于自动连接 bean#xff0c;就像 Autowired 注释#xff0c;容器#xff0c;一个属性或者任意命名的可…转载自 Spring Autowired 注释
Spring Autowired 注释
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, 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;Autowiredpublic 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 version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdcontext:annotation-config/!-- Definition for textEditor bean without constructor-arg --bean idtextEditor classcom.tutorialspoint.TextEditor/bean!-- Definition for spellChecker bean --bean idspellChecker classcom.tutorialspoint.SpellChecker/bean/beans
一旦你已经完成的创建了源文件和 bean 配置文件让我们运行一下应用程序。如果你的应用程序一切都正常的话这将会输出以下消息
Inside SpellChecker constructor.
Inside checkSpelling.
属性中的 Autowired
你可以在属性中使用 Autowired 注释来除去 setter 方法。当时使用 为自动连接属性传递的时候Spring 会将这些传递过来的值或者引用自动分配给那些属性。所以利用在属性中 Autowired 的用法你的 TextEditor.java 文件将变成如下所示
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {Autowiredprivate SpellChecker spellChecker;public TextEditor() {System.out.println(Inside TextEditor constructor. );} public SpellChecker getSpellChecker( ){return spellChecker;} public void spellCheck(){spellChecker.checkSpelling();}
}
下面是配置文件 Beans.xml
?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdcontext:annotation-config/!-- Definition for textEditor bean --bean idtextEditor classcom.tutorialspoint.TextEditor/bean!-- Definition for spellChecker bean --bean idspellChecker classcom.tutorialspoint.SpellChecker/bean/beans
一旦你在源文件和 bean 配置文件中完成了上面两处改变让我们运行一下应用程序。如果你的应用程序一切都正常的话这将会输出以下消息
Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.
构造函数中的 Autowired
你也可以在构造函数中使用 Autowired。一个构造函数 Autowired 说明当创建 bean 时即使在 XML 文件中没有使用 元素配置 bean 构造函数也会被自动连接。让我们检查一下下面的示例。
这里是 TextEditor.java 文件的内容
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {private SpellChecker spellChecker;Autowiredpublic TextEditor(SpellChecker spellChecker){System.out.println(Inside TextEditor constructor. );this.spellChecker spellChecker;}public void spellCheck(){spellChecker.checkSpelling();}
}
下面是配置文件 Beans.xml
?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdcontext:annotation-config/!-- Definition for textEditor bean without constructor-arg --bean idtextEditor classcom.tutorialspoint.TextEditor/bean!-- Definition for spellChecker bean --bean idspellChecker classcom.tutorialspoint.SpellChecker/bean/beans
一旦你在源文件和 bean 配置文件中完成了上面两处改变让我们运行一下应用程序。如果你的应用程序一切都正常的话这将会输出以下消息
Inside SpellChecker constructor.Inside TextEditor constructor.
Inside checkSpelling.
Autowired 的requiredfalse选项
默认情况下Autowired 注释意味着依赖是必须的它类似于 Required 注释然而你可以使用 Autowired 的 requiredfalse 选项关闭默认行为。
即使你不为 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(requiredfalse)public void setAge(Integer age) {this.age age;} public Integer getAge() {return age;}Autowiredpublic void setName(String name) {this.name name;} public String getName() {return name;}
}