关于spring自动检测组件的使用方式网上太多了,而且也不是我记录的重点,我想说下一点可能你还不知道的经验 我们知道如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。 先写一个小例子,剩下的在下面解释 <!-- 定义扫描根路径为leot.test,不使用默认的扫描方式 --> <context:component-scan base-package="leot.test" use-default-filters="false"> </context:component-scan> 下面是引用spring framework开发手册中的一段话“ Spring 2.5引入了更多典型化注解(stereotype annotations): 下面是网上目前关于组件扫描最详细的介绍 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>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。 filter標籤在Spring3有五個type,如下:
所以上例用的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。 我按他的例子,配置了我自己的如下: <context:component-scan base-package="com.xhlx.finance.budget" </context:component-scan> 但是死活扫描不到,网上又没有更好的讲解,没办法只好自己试,改成 <context:component-scan base-package="com.xhlx.finance.budget" > </context:component-scan> 这样连没有加注解的类也扫描并实例化,结果报错,因为有的类根本就没有默认的构造函数不能实例化,能不报错吗 改成 <context:component-scan base-package="com.xhlx.finance.budget" </context:component-scan>问题依旧 <context:component-scan base-package="com.xhlx.finance.budget" > </context:component-scan> 嘿,这次可以了,写全限定名就可以,表达式却不行,但是如果我有成千上百个还得一个一个这样配置啊?不行,继续研究,我想有个base-package的配置,从表面意思来看这是个“基本包”,是否表示下面的过滤路径是基于这个包的呢?于是试着改成 <context:component-scan base-package="com.xhlx.finance.budget" > </context:component-scan> 嘿,行了。 |
|