@Autowired 注释可以在 setter 方法中被用于自动连接 bean。

你可以在 XML 文件中的 setter 方法中使用 @Autowired 注释来除去 元素。

当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中执行 byType 自动连接。

一个示例

  • 新建Spring项目

  • 创建 Java 类 TextEditor, SpellChecker 和 MainApp

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

package hello;
import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
private SpellChecker spellChecker; @Autowired
public void setSpellChecker(SpellChecker spellChecker){
System.out.println("Inside setSpellChecker.");
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker(){
return spellChecker;
} public void spellCheck(){
spellChecker.checkSpelling();
}
}

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

package hello;

public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor");
}
public void checkSpelling(){
System.out.println("Inside checkSpelling.");
}
}

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

package hello;
//import org.springframework.context.support.AbstractApplicationContext;
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="hello.TextEditor" >
</bean> <!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="hello.SpellChecker"> </bean> </beans>

运行结果如下:

Inside SpellChecker constructor
Inside setSpellChecker.
Inside checkSpelling.

属性中的 @Autowired

你可以在属性中使用 @Autowired 注释来除去 setter 方法。

当时使用 为自动连接属性传递的时候,Spring 会将这些传递过来的值或者引用自动分配给那些属性。

利用在属性中 @Autowired 的用法,你的 TextEditor.java 文件将变成如下所示:

package hello;
import org.springframework.beans.factory.annotation.Autowired; public class TextEditor {
@Autowired
private SpellChecker spellChecker;
public TextEditor(){
System.out.println("Inside TextEditor constructor");
}
// a getter method to return spellChecker
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="hello.TextEditor" >
</bean> <!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="hello.SpellChecker"> </bean> </beans>

运行结果如下

Inside TextEditor constructor
Inside SpellChecker constructor
Inside checkSpelling.

构造函数中的 @Autowired

也可以在构造函数中使用 @Autowired。

一个构造函数 @Autowired 说明当创建 bean 时,即使在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动连接。

下面的示例。

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

package hello;
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="hello.TextEditor" >
</bean> <!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="hello.SpellChecker"> </bean> </beans>

运行结果如下

Inside SpellChecker constructor
Inside TextEditor constructor
Inside checkSpelling.

@Autowired 的(required=false)选项

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

即使你不为 age 属性传递任何参数,下面的示例也会成功运行,但是对于 name 属性则需要一个参数。

你可以自己尝试一下这个示例,因为除了只有 Student.java 文件被修改以外,它和 @Required 注释示例是相似的。

Student.java 文件内容:

package hello;
import org.springframework.beans.factory.annotation.Autowired; public class Student {
private int age;
String name;
@Autowired(required = false)
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
@Autowired
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
} }

其中,setAge()方法使用 @Autowired 的 (required=false) 选项关闭默认行为。

配置文件 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="student" class="hello.Student" >
<property name="name" value="番茄"></property>
</bean> </beans>

这里,,没有XML文件中设置属性age的值,仍能正确运行该程序,结果如下:

name: 番茄
age: 0

每天学习一点点,每天进步一点点。

Spring @Autowired 注释的更多相关文章

  1. Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  2. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  3. @Autowired 注释与@Qualifier 注释

    @Service("OrganDaoIbatis") public class OrganDaoIbatis extends BaseDao implements IOrganDa ...

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

    1.@Autowired 可以用在多个地方,在 setter 方法上,属性上 或者 带有多个参数的任意方法上. Setter 方法中的 @Autowired. 当 Spring遇到一个在 setter ...

  5. Spring@Autowired注解与自动装配(转发)

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  6. [Spring]@Autowired,@Required,@Qualifier注解

    @Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...

  7. [转] Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  8. Spring @Qualifier 注释

    可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配. 在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指 ...

  9. 使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱

    1.当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的某一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释来精确配置. 2.示例 ...

随机推荐

  1. Hexo博客插入图片的方法

    Hexo博客插入图片的方法 hexo图片blog hexo blog 插入图片的方法总结 hexo 的blog 内容是根据 markdown 文件的内容生成的html文件, 生成的文件全部在 /pub ...

  2. 尤雨溪的vue怎么学,应该从vue-cli开始,为什么?

    带手机验证码登陆, 带全套购物车系统 带数据库 前后端分离开发 带定位用户功能 数据库代码为本地制作好了 带支付宝支付系统 带django开发服务器接口教程 地址:   https://www.dua ...

  3. 当setWidth()和setHeight()方法不起作用时

    当在Android开发中用方法setWidth()和setHeight()动态设置控件的宽高时,当被改后的宽高小雨原来的宽高时,这两个方法将不会生效. 解决办法: 1 2 3 4 LayoutPara ...

  4. Nakamori Akina

    听过中森明菜的歌以后,一直想写点什么.恰好前段时间看过她的一个访谈https://b23.tv/av13810011,节目里已经39岁左右的她看着已经有些衰老,但是那份属于她的天真却保持的很好. 节目 ...

  5. python(面向对象-类封装调用)

    一.面对对象思想 (1)大家肯定听过 Python 中”一切皆对象“的说法,但可能并不了解它的具体含义,只是在学习的时候听说 Python 是面向对象的编程语言,本节将向大家详细介绍 Python 面 ...

  6. Automatic Reference Counting

    NSObject简化版alloc: struct obj_layout { NSUInteger retained; }; + (id)alloc { int size = sizeof(struct ...

  7. 支付宝小程序云开发serverless----获取用户的user_id

    支付宝小程序云开发serverless----获取用户的user_id 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 开通云调 ...

  8. Face The Right Way 开关(POJ3276)

    描述: \( N 头牛排成了一列.每头牛或者向前或者向后.为了让所有的牛都面向前方,农夫约翰买了 一台自动转向的机器. 这个机器在购买时就必须设定一个数值 K,机器每操作一次恰好使 K 头连续的牛转向 ...

  9. 记忆化搜索 E - Loppinha, the boy who likes sopinha Gym - 101875E

    E - Loppinha, the boy who likes sopinha Gym - 101875E 这个题目是一个dp,这个应该很容易看出来,但是对于状态的定义其实有点难去想, 看了题解dp[ ...

  10. .NETcore中使用jwt来对api进行身份验证

    对于 登陆,身份,授权这之类的操作,我们最常用的几种方法无非就是 cookie session token 这三者的差别 https://www.cnblogs.com/moyand/p/904797 ...