`

context:component-scan

 
阅读更多

<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。 

<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 
相关阅读:spring组件扫描<context:component-scan/>使用详解 
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> 
实行示例 

Java代码   收藏代码
  1. package pub.spring;  
  2.   
  3. import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;  
  4. import org.springframework.beans.factory.config.BeanDefinition;  
  5. import org.springframework.beans.factory.support.BeanDefinitionRegistry;  
  6. import org.springframework.context.annotation.AnnotationBeanNameGenerator;  
  7. import org.springframework.core.type.AnnotationMetadata;  
  8.   
  9.   
  10. public class BeanNameGenerator extends AnnotationBeanNameGenerator {  
  11.   
  12.     private String BASE_PACKAGE_NAME;  
  13.   
  14.     private String convertJavaNameToUrlName(String name) {  
  15.         StringBuilder sb = new StringBuilder();  
  16.         for (int n = 0; n < name.length(); n++) {  
  17.             char c = name.charAt(n);  
  18.             if (Character.isUpperCase(c)) {  
  19.                 if (n > 0) {  
  20.                     sb.append('_');  
  21.                 }  
  22.                 c = Character.toLowerCase(c);  
  23.             }  
  24.             sb.append(c);  
  25.         }  
  26.         return sb.toString();  
  27.     }  
  28.   
  29.     @Override  
  30.     public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {  
  31.         String name;  
  32.         String className = definition.getBeanClassName();  
  33.   
  34.         final String CONTROLLER_POSTFIX = "Action";  
  35.         if (className.endsWith(CONTROLLER_POSTFIX)) {  
  36.   
  37.             String suffix = null;  
  38.   
  39.             AnnotatedBeanDefinition annotatedDef = (AnnotatedBeanDefinition) definition;  
  40.             AnnotationMetadata amd = annotatedDef.getMetadata();  
  41.   
  42.             final String controllerAnnotation = "org.springframework.stereotype.Controller";  
  43.             String controllerName = (String) amd.getAnnotationAttributes(controllerAnnotation).get("value");  
  44.             if (controllerName != null && controllerName.length() > 0) {  
  45.                 // explicit specified postfix  
  46.                 if (controllerName.charAt(0) == '.') {  
  47.                     suffix = controllerName;  
  48.                 }  
  49.                 // for backword compatible  
  50.                 // explicit specified struts uri  
  51.                 else if (controllerName.indexOf('.') == -1) {  
  52.                     return controllerName + ".do";  
  53.                 }  
  54.                 // explicit specified uri  
  55.                 else {  
  56.                     return controllerName;  
  57.                 }  
  58.             }  
  59.   
  60.             if (BASE_PACKAGE_NAME == null) {  
  61.                 BASE_PACKAGE_NAME = className.substring(0,  
  62.                     className.indexOf(".web.") + ".web".length());  
  63.             }  
  64.   
  65.             int pos = className.lastIndexOf('.');  
  66.             String namePart = className.substring(pos + 1,  
  67.                 className.length() - CONTROLLER_POSTFIX.length());  
  68.             namePart = convertJavaNameToUrlName(namePart);  
  69.   
  70.             String packagePart = className.substring(BASE_PACKAGE_NAME.length(), pos);  
  71.             if (packagePart.indexOf('_') != -1) {  
  72.                 packagePart = packagePart.replace("_."".");  
  73.             }  
  74.             assert packagePart.endsWith(".action");  
  75.             packagePart = packagePart.substring(0, packagePart.length() - ".action".length());  
  76.   
  77.             name = packagePart + '.' + namePart;  
  78.   
  79.             if (name.startsWith(".app.")) {  
  80.                 name = name.substring(".app".length());  
  81.             }  
  82.   
  83.             // postfix specified  
  84.             if (suffix != null) {  
  85.                 // do nothing  
  86.             }  
  87.             // common .do actions  
  88.             else if (name.endsWith(".operate") ||  
  89.                 name.endsWith(".functions")) {  
  90.                 suffix = ".do";  
  91.             }  
  92.             //fall back to html  
  93.             else {  
  94.                 suffix = ".html";  
  95.             }  
  96.             name = name.replace('.''/') + suffix;  
  97.         }  
  98.         else {  
  99.             name = super.generateBeanName(definition, registry);  
  100.         }  
  101.         return name;  
  102.     }  
  103.   
  104. }  
分享到:
评论

相关推荐

    Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    主要介绍了Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决的相关资料,需要的朋友可以参考下

    Spring扫描器—spring组件扫描使用详解

    NULL 博文链接:https://gaozzsoft.iteye.com/blog/1523898

    Spring注释 注入方式源码示例,Annotation

    &lt;context:component-scan base-package="Mode"&gt;&lt;/context:component-scan&gt; //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...

    SpringMVC+Hibernate实例

    &lt;context:component-scan base-package="com.bbs"/&gt; &lt;!--注解支持--&gt; &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; ...

    springMVC技术概述

    配置使用注解的Handler和Service等等使用&lt;context:component-scan&gt; 不过springBoot已经省略了这些配置 常用注解:@Controller @RestController(Controller+ResponseBody) @Service @Transactional @Mapper @...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;context:component-scan base-package="org.whvcse"&gt;&lt;/context:component-scan&gt; &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- &lt;aop:config&gt; &lt;aop:pointcut id="defaultServiceOperation" ...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    //applicationContext.xml文件中添加 ... xmlns:xsi=... &lt;context:component-scan base-package="com.haijian" /&gt;

    springmvcwendang

    &lt;context:component-scan base-package="com.createnets.springmvc.web" /&gt; (2)新建一个Student类 用于测试注解 (3)配置注解 @Controller @RequestMapping("/list") (3)配置视图名称 &lt;!-- 配置视图名称 -...

    一个整合ssm框架的实例

    &lt;context:component-scan base-package="com.jxy.java.service" /&gt; &lt;!-- 导入数据库配置文件 --&gt; &lt;context:property-placeholder location="classpath:jdbc.properties"/&gt; &lt;!-- 配置数据库连接池 --&gt; ...

    Spring AOP配置源码

    &lt;context:component-scan base-package="com.spring.*"/&gt; &lt;aop:config&gt; &lt;aop:aspect id="aspect" ref="logIntercepter"&gt; &lt;aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/&gt; ...

    quartz 定时任务

    &lt;context:component-scan base-package="cn.ly.quartz.service" /&gt; &lt;!-- job --&gt; class="org.springframework.scheduling.quartz.JobDetailFactoryBean"&gt; &lt;!-- trigger触发器 --&gt; ...

    维生药业小项目 SSH简单学习项目

    维生药业小项目 SSH简单学习项目 &lt;?xml version="1.0" encoding="UTF-8"?&gt; ... xmlns:context="http://www.springframework.org/schema/context" ... &lt;context:component-scan base-package="com.sixth" /&gt; &lt;/beans&gt;

    Spring Context测试

    学习spring组件扫描(Component Scanning)的代码 使用方法:直接把工程导入,直接Run ...&lt;context:component-scan base-package="com.test"&gt;&lt;/context:component-scan&gt; 2.在需要装配的类的上面全部加上@Component

    spring杂谈 作者zhang KaiTao

    1.26 context:component-scan扫描使用上的容易忽略的use-default-filters 1.27 idea内嵌jetty运行springmvc项目报ConversionFailedException 1.28 springmvc 3.2 @MatrixVariable注解 1.29 spring3.2 带matrix变量的...

    spring

    context:component-scan:作用是可以使用@ Component,@ Controller,@ Service等等来省略xml配置文件中的bean元素,简化配置 context:component-scan是上下文:annotation-config的超集,配置了前者则不需要配置...

    java微信公众号MVC开发框架

    spring配置文件中唯一需要配置的bean是WeixinConfigurer类,&lt;context:component-scan base-package="com.github.jweixin.jwx.weixin.service" /&gt;是可选配置,但里面封装了微信接口服务类,建议一定要配置进spring...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;context:component-scan base-package="com.mvc" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;aop:config proxy-...

    Maven拆分代码.zip

    &lt;context:component-scan base-package="com.itheima.service"/&gt; &lt;!--aop面向切面编程,切面就是切入点和通知的组合--&gt; &lt;!--配置事务管理器--&gt; &lt;!--配置事务的通知--&gt; &lt;tx:advice id="advice"&gt; &lt;tx:...

    springweb3.0MVC注解(附实例)

    &lt;context:component-scan base-package="com.baobaotao.web"/&gt; &lt;!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; AnnotationMethodHandlerAdapter"/&gt; &lt;!-- ③:对模型视图名称的解析,即在...

Global site tag (gtag.js) - Google Analytics