Setter 方法中的 @Autowired

当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解,它会试图执行 byType 自动连接。换言之,加了@Autowired的Setter方法等同于byType自动装配模式。

看个例子:

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();}}

Main.app:

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/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.xsd"><context:annotation-config/><!-- Definition for textEditor bean without constructor-arg  --><bean id="textEditor" class="com.sap.TextEditor"></bean><!-- Definition for spellChecker bean --><bean id="spellChecker" class="com.sap.SpellChecker"></bean></beans>

因为是采用by Type装配,所以Beans.xml里的com.sap.SpellChecker的id可以任意指定:

删除了Setter方法的@Autowired注释后,TextEditor的spellChecker注入不会发生,因此应用会发生nullpointer异常。

给属性加上@Autowired方法

这样做可以不必显式地书写依赖属性的setter方法,可以少敲击几次键盘,让源代码更精简。

textEditor:

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 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/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.xsd"><context:annotation-config/><!-- Definition for textEditor bean --><bean id="textEditor" class="com.sap.TextEditor"></bean><!-- Definition for spellChecker bean --><bean id="spellChecker" class="com.sap.SpellChecker"></bean></beans>

同样,如果@Autowired注解被删除,SpellChecker成员变量的依赖注入将不会发生,此时应用会产生nullPointer异常。

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Spring 基于注解(annotation)的配置之@Autowired注解相关推荐

  1. Spring 基于注解(annotation)的配置之@Required注解

    从 Spring 2.5 开始就可以使用注解来配置依赖注入.注解连线在默认情况下在 Spring 容器中不打开.因此,在可以使用基于注解的连线之前,我们将需要在我们的 Spring 配置文件中启用它: ...

  2. Spring 基于注解(annotation)的配置之@Qualifier注解

    使用@Qualifier可以分别为同样类型的Bean分别注入不同的依赖值.看个例子: Student.java: package com.sap;public class Student {priva ...

  3. spring中自定义注解(annotation)与AOP中获取注解___使用aspectj的@Around注解实现用户操作和操作结果日志

    spring中自定义注解(annotation)与AOP中获取注解 一.自定义注解(annotation) 自定义注解的作用:在反射中获取注解,以取得注解修饰的类.方法或属性的相关解释. packag ...

  4. SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版

    SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版 注解版: 1.改druid 连接池,不改可以跳过这步 添加依赖 <dependency><groupId& ...

  5. hibernate mysql annotation_Hibernate基于注解annotation的配置

    Annotation在框架中是越来越受欢迎了,因为annotation的配置比起XML的配置来说方便了很多,不需要大量的XML来书写,方便简单了很多,只要几个annotation的配置,就可以完成我们 ...

  6. Spring基于@Configuration的类配置的内部源码实现

    概述 Spring容器启动时,即ApplicationContext接口实现类的对象实例执行refresh方法时,主要是通过执行ConfigurationClassPostProcessor这个Bea ...

  7. spring基于aspectj的AOP配置 aop:aspectj-autoproxy proxy-target-class=true

    精通Spring4.x--企业应用开发实战 8.5.1@AfterReturning("@annotation()")切点函数详解 代码实现的目标是为NaugthyWaiter类的 ...

  8. Spring框架----自动按照类型注入的Autowired注解

    当执行如下代码时(配置文件中未注入) private IAccountDao accountDao; public void saveAccount() {accountDao.saveAccount ...

  9. Java spring基于XML的aop配置实现

    1.依赖包 2.文件结构 3.接口类ISomeService package com.buckwheats.test;public interface ISomeService {public voi ...

最新文章

  1. 特斯拉再裁员3000多人,马斯克称别无选择
  2. WPF界面设计技巧(3)—实现不规则动画按钮
  3. 【控制】控制学习路线资源整理
  4. php fetchassoc 跨表,php-mysqli_fetch_assoc-如果同时更改数据会怎样?
  5. 盘点欧盟反垄断案对整个安卓生态造成的5大影响
  6. 性能远超AtomicLong,LongAdder原理完全解读
  7. 游戏服务器端引擎——DogSE的设计
  8. maven 父maven_Maven的春天
  9. Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序(二)
  10. 基于BS模式的航材电子商务交易平台(2)
  11. 1.docker学习
  12. 【汇编语言】通用数据处理指令——位操作类指令
  13. 机器学习基础算法23-XGBoost实践
  14. 在js中如何比较两个时间字符串的大小
  15. c语言计算器括号怎么解决,C语言计算器,该如何解决
  16. 我的Android进阶之旅------gt;Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项...
  17. mt7620n华硕固件下载_无法安装Windows10,这台电脑磁盘布局不受UEFI固件支持解决方法...
  18. PyCharm2021安装教程
  19. PHPAdmin数据类型
  20. android app targetsdk从23升级到28

热门文章

  1. 互联网时代IT系统的变革-硬件系统定制化发展
  2. 数据中心扩张和产能计划
  3. 再推荐一个安全的好软件
  4. MySQL数据类型和Java数据类型对应关系表
  5. springboot-vue项目前台1
  6. 【大话存储】学习笔记(7章), OSI模型
  7. 前端代码有关搜索引擎的代码
  8. 02_Jquery_02_元素选择器
  9. eclipse主题下载网站
  10. HDU 4535 吉哥系列故事——礼尚往来