首页 > 编程知识 正文

@autowired注解 required,autowired

时间:2023-05-04 19:41:39 阅读:148034 作者:2272

我需要先知道别的东西。 默认自动线。 它由xml文件组成,可以设置为byName、byType、constructor和autodetect。 例如,在byName中,其他bean的id名称将自动与此bean的set**匹配并自动加载,而无需显式写入依赖于bean的对象。 @Autowired是JavaBean使用的注释,以byType格式用于注入指定字段或方法所需的外部资源。 两者的功能相同,可以是减少或者消除属性或构造器参数的设置,只是配置地点不同。 autowire的四种模式差异:

首先,我们来看一下bean实例化和@Autowired程序集流程。 一切都从bean工厂的getBean方法开始,每次调用方法时都会返回一个bean实例。 无论当前是否存在,如果不存在,则实例化并装配它,否则直接返回。 Spring MVC是什么时候开始bean实例化过程的? 实际上,组件扫描刚完成)

在实例化和程序集期间多次递归调用getBean方法以解决类之间的依赖关系。

Spring几乎考虑了所有的可能性,所以方法特别复杂,但完整而有条理。

@Autowired最终会基于类型搜索和组装元素,但设置beans default-auto wire=' by name ' /会影响最终的类型匹配搜索。 之前基于BeanDefinition的autowire类型设置PropertyValue是值得进一步的,因为其中有新实例的创建和注册。 是那个自动wire by name方法。

用@Autowired说明使用方法吧

Setter方法的@Autowired

可以在JavaBean的setter方法中使用@Autowired注释。 如果遇到setter方法中使用的@Autowired注释,Spring将在该方法中执行byType自动汇编。

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

package com.tutorialspoint; importorg.spring framework.beans.factory.annotation.auto wired; 公共类文本编辑器{ privatespellcheckerspellchecker; @ autowiredpublicvoidsetspellchecker (spell checker ) { this.spell checker=spell checker; } publicspellcheckergetspellchecker () { return spellChecker; } public void spellCheck () { spellChecker.checkSpelling ); }以下是另一个依赖类文件SpellChecker.java的内容:

package com.tutorialspoint; 公共类拼写检查器(公共拼写检查器) system.out.println (insidespellcheckerconstructor.' ); } public void checkSpelling (() system.out.println ) ' insidecheckspelling.' ); 以下是MainApp.java文件的内容。

package com.tutorialspoint; importorg.spring framework.context.application context; importorg.spring framework.context.support.classpathxmlapplicationcontext; publicclassmainapp { publicstaticvoidmain [ ] args } { applicationcontext=newclasspathxmlapplicationcontext文本编辑器te.spellCheck (; }接下来是配置文件Beans.xml。

? XML version=' 1.0 ' encoding=' utf-8 '? beans xmlns=' http://www.spring framework.org/schema/beans ' xmlns 3360 xsi=' http://www.w3.org/2001/XML schema-200

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
你可以在属性中使用 @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。一个构造函数 @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 TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.

@Autowired 的(required=false)选项
默认情况下,@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; }}

Quote:
http://my.oschina.net/HeliosFly/blog/203902
http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-autowired-annotation.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。