Spring 基于注解(annotation)的配置之@Autowired注解
2023-09-14 09:03:08 时间
Setter 方法中的 @Autowired
当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解,它会试图执行 byType 自动连接。换言之,加了@Autowired的Setter方法等同于byType自动装配模式。
看个例子:
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public 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/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.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 {
@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.sap.TextEditor">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.sap.SpellChecker">
</bean>
</beans>
同样,如果@Autowired注解被删除,SpellChecker成员变量的依赖注入将不会发生,此时应用会产生nullPointer异常。
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关文章
- Spring配置类为什么要分Full和Lite模式
- ssm(Spring+Spring mvc+mybatis)——index.jsp
- spring利用javamail,quartz定时发送邮件 <转>
- Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL
- spring boot项目如何测试,如何部署
- Spring Cloud Alibaba 2.2.8 版本发布与社区未来规划介绍
- 07-spring学习-bean的其他配置(了解)
- Spring boot使用Aspose.Slides操作ppt转PDF、转图片
- maven 创建spring boot 需要的配置[一]
- spring boot通过命令行配置属性
- spring boot 启动脚本
- Spring Boot 应用使用 application.yml 和 application.properties 的区别
- Spring 基于 Java 的配置
- 在阿里云容器服务上开发基于Docker的Spring Cloud微服务应用
- 对Spring 及SpringMVC的理解
- 【超详细demo】Spring Boot 多数据源配置
- Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务
- Spring Cloud是一系列框架的有序集合
- Spring Security---将用户数据存入数据库详解
- Spring创建和初始化的源码流程分析
- spring cloud :五、分布式配置中心(spring cloud config)
- 慕课9、消息驱动的微服务-Spring Cloud Alibaba RocketMQ