context:component-scan注入的bean的id默认命名规则

在spring配置中,我们常常使用如下context:component-scan属性来自动注入bean

   <!-- 自动扫描 -->
	<context:component-scan base-package="com.java1234.service" />

那么注入的bean的id是如何命名的呢?

	/**
	 * 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);
	}

观察上述org.springframework.context.annotation.AnnotationBeanNameGenerator类下的 buildDefaultBeanName的注解,我们容易发现自动扫面的bean的id命名遵循如下规则:
一般情况下,将类名的首字母改为小写,作为bean的id。如com.java.String类对应的bean的id为"string";

特殊情况,即类名超过2个字符,且前两个字符均大写,则直接用类名作为bean的id。如com.chenchenchen.URL类对应的bean的id为"URL";

结语

了解了这个以后我们可以通过id调用自动注入的bean。此外,如果存在多个程序员同时编写一个项目的情况,可以通过继承AnnotationBeanNameGenerator类,并重写buildDefaultBeanName()的方式来重新制定命名规则(如改用全限定类名作为bean的id),解决bean的id冲突问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值