<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
相关阅读:[url=http://blog.sina.com.cn/s/blog_57769b7b0100tt5x.html]spring组件扫描<context:component-scan/>使用详解[/url]
name-generator:指定产生规则
当一个 Bean 被自动检测到时,会根据那个扫描器的 BeanNameGenerator 策略生成它的 bean 名称。默认情况下,对于包含 name 属性的 @Component、@Repository、 @Service 和 @Controller,会把 name 取值作为 Bean 的名字。如果这个注解不包含 name 值或是其他被自定义过滤器发现的组件,默认 Bean 名称会是小写开头的非限定类名。如果你不想使用默认 bean 命名策略,可以提供一个自定义的命名策略。
If you do not want to rely on the default bean-naming strategy, you can provide a custom
bean-naming strategy. First, implement the BeanNameGenerator interface, and be sure to
include a default no-arg constructor. Then, provide the fully-qualified class name when
configuring the scanner:
<beans>
<context:component-scan base-package="org.example"
name-generator="org.example.MyNameGenerator" />
</beans>
实行示例
<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
相关阅读:[url=http://blog.sina.com.cn/s/blog_57769b7b0100tt5x.html]spring组件扫描<context:component-scan/>使用详解[/url]
name-generator:指定产生规则
当一个 Bean 被自动检测到时,会根据那个扫描器的 BeanNameGenerator 策略生成它的 bean 名称。默认情况下,对于包含 name 属性的 @Component、@Repository、 @Service 和 @Controller,会把 name 取值作为 Bean 的名字。如果这个注解不包含 name 值或是其他被自定义过滤器发现的组件,默认 Bean 名称会是小写开头的非限定类名。如果你不想使用默认 bean 命名策略,可以提供一个自定义的命名策略。
If you do not want to rely on the default bean-naming strategy, you can provide a custom
bean-naming strategy. First, implement the BeanNameGenerator interface, and be sure to
include a default no-arg constructor. Then, provide the fully-qualified class name when
configuring the scanner:
<beans>
<context:component-scan base-package="org.example"
name-generator="org.example.MyNameGenerator" />
</beans>
实行示例
package pub.spring;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.type.AnnotationMetadata;
public class BeanNameGenerator extends AnnotationBeanNameGenerator {
private String BASE_PACKAGE_NAME;
private String convertJavaNameToUrlName(String name) {
StringBuilder sb = new StringBuilder();
for (int n = 0; n < name.length(); n++) {
char c = name.charAt(n);
if (Character.isUpperCase(c)) {
if (n > 0) {
sb.append('_');
}
c = Character.toLowerCase(c);
}
sb.append(c);
}
return sb.toString();
}
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
String name;
String className = definition.getBeanClassName();
final String CONTROLLER_POSTFIX = "Action";
if (className.endsWith(CONTROLLER_POSTFIX)) {
String suffix = null;
AnnotatedBeanDefinition annotatedDef = (AnnotatedBeanDefinition) definition;
AnnotationMetadata amd = annotatedDef.getMetadata();
final String controllerAnnotation = "org.springframework.stereotype.Controller";
String controllerName = (String) amd.getAnnotationAttributes(controllerAnnotation).get("value");
if (controllerName != null && controllerName.length() > 0) {
// explicit specified postfix
if (controllerName.charAt(0) == '.') {
suffix = controllerName;
}
// for backword compatible
// explicit specified struts uri
else if (controllerName.indexOf('.') == -1) {
return controllerName + ".do";
}
// explicit specified uri
else {
return controllerName;
}
}
if (BASE_PACKAGE_NAME == null) {
BASE_PACKAGE_NAME = className.substring(0,
className.indexOf(".web.") + ".web".length());
}
int pos = className.lastIndexOf('.');
String namePart = className.substring(pos + 1,
className.length() - CONTROLLER_POSTFIX.length());
namePart = convertJavaNameToUrlName(namePart);
String packagePart = className.substring(BASE_PACKAGE_NAME.length(), pos);
if (packagePart.indexOf('_') != -1) {
packagePart = packagePart.replace("_.", ".");
}
assert packagePart.endsWith(".action");
packagePart = packagePart.substring(0, packagePart.length() - ".action".length());
name = packagePart + '.' + namePart;
if (name.startsWith(".app.")) {
name = name.substring(".app".length());
}
// postfix specified
if (suffix != null) {
// do nothing
}
// common .do actions
else if (name.endsWith(".operate") ||
name.endsWith(".functions")) {
suffix = ".do";
}
//fall back to html
else {
suffix = ".html";
}
name = name.replace('.', '/') + suffix;
}
else {
name = super.generateBeanName(definition, registry);
}
return name;
}
}