探索Spring默认注入Bean Name

@Component
public class Cv {

	public String fff() {
		return "fff";
	}

}```
---
```java
@Component
public class VC {
	
	public String dd(){
		return "dddd";
	}

}```
---
```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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<context:component-scan base-package="com.ioc">
	</context:component-scan>
</beans>```
---
```java
public class TestIoc {

	[[[[[@Test](http://my.oschina.net/azibug)](http://my.oschina.net/azibug)](http://my.oschina.net/azibug)](http://my.oschina.net/azibug)](http://my.oschina.net/azibug)
	public void testIoc(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");
		VC c = (VC)ctx.getBean("vC");
		System.out.println(c.dd());
		Cv v=(Cv)ctx.getBean("cv");
		System.out.println(v.fff());
	}
}```
---
使用IOC 获取注入的Bean name 的时候遇到了一个奇怪的问题,以前总以为如果不显示声明Bean name,默认注入的是Bean id 是首字母小写字符串
。发现了不全是这样的,一个Bean 为VC,另一个是Cv,然而使用getBean(String name)方法获取bean 的时候。发现VC 类使用getBean("vC"),无法获取bean,而getBean("cv")的时候可以获取IOC 注入的bean.
debug 的时候发现VC 类注入的是"VC",而不是"vC".
源码在AnnotationBeanNameGenerator
```java
/**
	 * 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](http://my.oschina.net/u/2303379)](http://my.oschina.net/u/2303379)](http://my.oschina.net/u/2303379) definition the bean definition to build a bean name for
	 * [[[@return](http://my.oschina.net/u/556800)](http://my.oschina.net/u/556800)](http://my.oschina.net/u/556800) the default bean name (never {@code null})
	 */
	protected String buildDefaultBeanName(BeanDefinition definition) {
		String shortClassName = ClassUtils.getShortName(definition.getBeanClassName());
		return Introspector.decapitalize(shortClassName);
	}

可以看出是首字母小写的注入方式,但是例子中VC 却不适用,究竟VC 是怎么注入进去的呢? 正在开会,待续。。。 got it!

 /**
     * 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);
    }```
由此可以看出,只要Bean的第一个和第二个字母都是大写的话,则不会按照首字母小写注入。

转载于:https://my.oschina.net/heiseguoguo/blog/710270

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值