Spring中的Spring JSR-250 注释之@Resource
Spring 注释 resource 250
2023-09-14 09:03:08 时间
@Resource注解相当于By Name装配方式。
TextEditor:
package com.sap;
import javax.annotation.Resource;
public class TextEditor {
private SpellChecker spellChecker;
@Resource(name= "spellChecker223")
public void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker(){
return spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
意思是,需要在Beans.xml里寻找一个id 为spellChecker223的bean:
<?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="spellChecker223" class="com.sap.SpellChecker">
</bean>
</beans>
这里的id必须和TextEditor.java里@Resource name属性指定的一致。
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关文章
- Spring boot项目分环境Maven打包,动态配置文件,动态配置项目
- spring学习笔记(16)趣谈spring 事件机制[2]:多监听器流水线式顺序处理
- 使用spring实现邮件的发送(含测试,源码,注释)
- 大叔问题定位分享(34)Spring的RestTemplate请求json数据后内容被修改
- Spring AOP编程-aspectJ介绍
- Spring 视图和视图解析器简介
- Restful: Spring Boot with Mongodb
- Spring Boot:定制HTTP消息转换器
- 不使用spring的情况下用java原生代码操作mongodb数据库的两种方式
- Java面试题 对spring IOC和AOP的理解