`
蔡尧东
  • 浏览: 32727 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

context:component-scan配置策略

阅读更多
Spring applicationContext.xml的<context:component-scan>標籤用途比我想像的還要實用。而且後來才知道,有了<context:component-scan>,另一個<context:annotation-config/>標籤根本可以移除掉,因為被包含進去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下尋找有@Component和@Configuration的target Class。而現在如下的飯粒:
<context:component-scan base-package="com.foo" use-default-filters="false">
    <context:include-filter type="regex" expression="com.foo.bar.*Config"/>
    <context:include-filter type="regex" expression="com.foo.config.*"/>
</context:component-scan>
  <context:component-scan>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。
  filter標籤在Spring3有五個type,如下:
Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ語法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter
  所以上例用的regex就有個語病,com.foo.config.* 可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因為小數點在Regex是任意字元,是故要用\.把小數點跳脫為佳。(2010/3/15補充:但要使用\.方式,其use-default-filters不能為false,否則抓不到,感覺是Bug)
  Spring3提供豐富的Filter支援,有益配置策略,不需面臨Configuration Hell,比如Regex的com\.foo\.*\.action\.*Config,這樣就可以找到com.foo package下所有action子package的*Config的target class。
  2010/3/18補充:後來在AppConfig前忘了加@Component,AppConfig內尚留有@Bean,奇怪的是還是能work。我猜有加@Bean的method的class,若沒特別註解AppConfig是@Repository、@Service還是@Configuration,一律被Spring3視為@Component。
分享到:
评论

相关推荐

    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技术概述

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

    SpringMVC+Hibernate实例

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

    Spring 3.0所需jar文件和对应的配置文件

    nested exception is java.lang.IllegalStateException: Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are ...

    Spring Context测试

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

    springmvcwendang

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

    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" ...

    学习ssm整理的xml配置笔记

    ssm 部分xml配置,主要是事务配置、&lt;context:component-scan&gt;使用说明

    spring

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

    一个整合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; ...

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

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

    bundery#docs#注解1

    spring注解使用注解之前,要先在 application.xml 文件中添加&lt;context:component-scan base-package="co

    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; ...

    维生药业小项目 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;

    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; ...

    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:...

    springmvc-ibatis

    &lt;context:component-scan base-package="com.org"/&gt; &lt;!-- 匹配jsp文件下面的所有.jsp的页面 --&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;!-- p:prefix="/" p:suffix=".jsp" /&gt;--&gt; ...

    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-...

Global site tag (gtag.js) - Google Analytics