Spring 基于注解(annotation)的配置之@Autowired注解
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注解相关推荐
- Spring 基于注解(annotation)的配置之@Required注解
从 Spring 2.5 开始就可以使用注解来配置依赖注入.注解连线在默认情况下在 Spring 容器中不打开.因此,在可以使用基于注解的连线之前,我们将需要在我们的 Spring 配置文件中启用它: ...
- Spring 基于注解(annotation)的配置之@Qualifier注解
使用@Qualifier可以分别为同样类型的Bean分别注入不同的依赖值.看个例子: Student.java: package com.sap;public class Student {priva ...
- spring中自定义注解(annotation)与AOP中获取注解___使用aspectj的@Around注解实现用户操作和操作结果日志
spring中自定义注解(annotation)与AOP中获取注解 一.自定义注解(annotation) 自定义注解的作用:在反射中获取注解,以取得注解修饰的类.方法或属性的相关解释. packag ...
- SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版
SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版 注解版: 1.改druid 连接池,不改可以跳过这步 添加依赖 <dependency><groupId& ...
- hibernate mysql annotation_Hibernate基于注解annotation的配置
Annotation在框架中是越来越受欢迎了,因为annotation的配置比起XML的配置来说方便了很多,不需要大量的XML来书写,方便简单了很多,只要几个annotation的配置,就可以完成我们 ...
- Spring基于@Configuration的类配置的内部源码实现
概述 Spring容器启动时,即ApplicationContext接口实现类的对象实例执行refresh方法时,主要是通过执行ConfigurationClassPostProcessor这个Bea ...
- spring基于aspectj的AOP配置 aop:aspectj-autoproxy proxy-target-class=true
精通Spring4.x--企业应用开发实战 8.5.1@AfterReturning("@annotation()")切点函数详解 代码实现的目标是为NaugthyWaiter类的 ...
- Spring框架----自动按照类型注入的Autowired注解
当执行如下代码时(配置文件中未注入) private IAccountDao accountDao; public void saveAccount() {accountDao.saveAccount ...
- Java spring基于XML的aop配置实现
1.依赖包 2.文件结构 3.接口类ISomeService package com.buckwheats.test;public interface ISomeService {public voi ...
最新文章
- 特斯拉再裁员3000多人,马斯克称别无选择
- WPF界面设计技巧(3)—实现不规则动画按钮
- 【控制】控制学习路线资源整理
- php fetchassoc 跨表,php-mysqli_fetch_assoc-如果同时更改数据会怎样?
- 盘点欧盟反垄断案对整个安卓生态造成的5大影响
- 性能远超AtomicLong,LongAdder原理完全解读
- 游戏服务器端引擎——DogSE的设计
- maven 父maven_Maven的春天
- Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序(二)
- 基于BS模式的航材电子商务交易平台(2)
- 1.docker学习
- 【汇编语言】通用数据处理指令——位操作类指令
- 机器学习基础算法23-XGBoost实践
- 在js中如何比较两个时间字符串的大小
- c语言计算器括号怎么解决,C语言计算器,该如何解决
- 我的Android进阶之旅------gt;Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项...
- mt7620n华硕固件下载_无法安装Windows10,这台电脑磁盘布局不受UEFI固件支持解决方法...
- PyCharm2021安装教程
- PHPAdmin数据类型
- android app targetsdk从23升级到28