在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冲突问题。