Spring @Service生成bean名称的规则
今天碰到一个问题,写了一个@Service的bean,类名大致为:BKYInfoServcie.java
dubbo export服务的配置:
<dubbo:service interface="com.xxx.XxxService" ref="bKYInfoServcie" />
结果启动报错:找不到名为bKYInfoServcie的bean
bean的名字不是我预期的"bKYInfoServcie",临时将bean的名字指定成了bKYInfoServcie来解决的,即:@Service("bKYInfoServcie")
但还是觉得比较奇怪,之前一直以为Spring对注解形式的bean的名字的默认处理就是将首字母小写,再拼接后面的字符,但今天看来不是这样的。
回来翻了一下原码,原来还有另外的一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致

/**
* Derive a default bean name from the given bean definition.
* <p>The default implementation simply builds a decapitalized version
* of the short class name: e.g. "mypackage.MyJdbcDao" -> "myJdbcDao".
* <p>Note that inner classes will thus have names of the form
* "outerClassName.InnerClassName", which because of the period in the
* name may be an issue if you are autowiring by name.
* @param definition the bean definition to build a bean name for
* @return the default bean name (never {@code null})
*/
protected String buildDefaultBeanName(BeanDefinition definition) {
String shortClassName = ClassUtils.getShortName(definition.getBeanClassName());
return Introspector.decapitalize(shortClassName);
}
/**
* Utility method to take a string and convert it to normal Java variable
* name capitalization. This normally means converting the first
* character from upper case to lower case, but in the (unusual) special
* case when there is more than one character and both the first and
* second characters are upper case, we leave it alone.
* <p>
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
* as "URL".
*
* @param name The string to be decapitalized.
* @return The decapitalized version of the string.
*/
public static String decapitalize(String name) {
if (name == null || name.length() == 0) {
return name;
}
// 如果发现类的前两个字符都是大写,则直接返回类名
if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))){
return name;
}
// 将类名的第一个字母转成小写,然后返回
char chars[] = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
return new String(chars);
}
Spring @Service生成bean名称的规则的更多相关文章
- Spring @Component生成bean名称的规则(当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致
正确注入方式: @Autowired private TFeeMapper TFeeMapper; 错误注入方式 @Autowired private TFeeMapper tFeeMapper; 这 ...
- Spring中默认bean名称的生成策略/方式修改
最近公司项目打算模块化,其实一个原因也是为了能够整合公司多个业务的代码,比如一个资源xxx,两个业务中都有对这个资源的管理,虽然是一个资源,但是是完全不同的定义.完全不同的表.不同的处理逻辑.所以打算 ...
- 品Spring:负责bean定义注册的两个“排头兵”
别看Spring现在玩的这么花,其实它的“筹码”就两个,“容器”和“bean定义”. 只有先把bean定义注册到容器里,后续的一切可能才有可能成为可能. 所以在进阶的路上如果要想走的顺畅些,彻底搞清楚 ...
- Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名
今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自 ...
- 【Spring】10、Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名
@Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action) @Repository用于标注数据访问组件,即 ...
- 翻译-使用Spring WebService生成SOAP Web Service
原文链接:http://spring.io/guides/gs/producing-web-service/ 生成SOAP web service 该指南将带领你使用Spring创建一个基于SOAP的 ...
- 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl
目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...
- Spring配置文件结构对于生成Bean的影响
Spring配置文件结构对于生成Bean的影响 有段时间忙于毕设,导致Spring学习的东西忘了很多,所以最近又开始从头看Spring的基础.基础的Bean的装配不再多说了.这一次,主要是深入一点了解 ...
- paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService]
paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService] ...
随机推荐
- F2工作流引擎之 工作流运转模型(三)
1流程单起点单终止模型 单起点:一个流程定义必须有且唯一起点 单结束点:一个流程定义必须有且唯一结束点. 约定:提单与结束是每个流程必须有的活动,且唯一只有一个提单和结束. 2串行模型 描述:串行(S ...
- Icon资源详解[2]
本文分享&备忘最近了解到的icon资源在windows平台下相关的一部分知识.所有测试代码都尽可能的依赖win32 API实现.通过源码可以了解其结构,同时它们也是可复用的代码积累. ...
- 学习di'z地址
Swift学习地址https://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/ http://www.oschina.n ...
- bzoj 3122: [Sdoi2013]随机数生成器
#include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...
- 探索javascript----我对渐变轮播图的理解
对于一个没有编程基础的人来说,我时常希望能有人告诉我,当我们看到一个效果的时候,该怎样有条理地分析出它的行为,而我自己有必要加强这方面的 自省,对于一个轮播图我是这样看的,自动播放必然带有一个定时器, ...
- zipalign内存对齐优化
zipalign:android中SDK下tools文件夹 用来对资源文件的内存进行对齐优化 手工命令: 优化:zipalign -v 4 source.apk destination.apk 4: ...
- checkbox选中与取消选择
先上代码 <form> 你爱好的运动是?<br/> <input type="checkbox" name="items" val ...
- c# List 按类的指定字段排序
List<Report> list=Report.FindAllbyClassID(id); ) { list.Sort(delegate(Report x,Report y){ret ...
- dedecms搜索框制作
<form method=" name="kwtype"> <table width="> <tr> <td widt ...
- jquery事件重复绑定的快速解决方法
click等事件 解决:使用unbind("click")方法先解除绑定的事件再绑定新事件,即在给对象绑定事件之前先移除该对象上的原有事件 1 $("#test2&quo ...