Spring组件扫描<context:component-scan/>使用详解

来源:互联网 发布:sqlserver 行合计 编辑:程序博客网 时间:2024/06/05 07:29
1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。
<!-- 注解注入 -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.liantuo.hotel.common.service.impl" />
<context:component-scan base-package="com.liantuo.hotel.common.dao.ibatis" />
<context:component-scan base-package="com.liantuo.hotel.app.dao.ibatis" />
<context:component-scan base-package="com.liantuo.hotel.app.service" />
<context:component-scan base-package="com.liantuo.hotel.app.service.ibatis" />
2.下面是引用spring framework开发手册中的一段话
Spring 2.5引入了更多典型化注解(stereotype annotations): @Component@Service和 @Controller@Component是所有受Spring管理组件的通用形式;而@Repository@Service和 @Controller则是@Component的细化,用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说,你能用@Component来注解你的组件类,但如果用@Repository@Service @Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。例如,这些典型化注解可以成为理想的切入点目标。当然,在Spring Framework以后的版本中, @Repository@Service和 @Controller也许还能携带更多语义。如此一来,如果你正在考虑服务层中是该用@Component还是@Service,那@Service显然是更好的选择。同样的,就像前面说的那样, @Repository已经能在持久化层中进行异常转换时被作为标记使用了。”
3.有了<context:component-scan>,另一个<context:annotation-config/>标签根本可以移除掉,因为已经被包含进去了。
4.<context:component-scan>提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表引入和排除的过滤。
如:<context:component-scan base-package="com.xhlx.finance.budget" >
<context:include-filter type="regex" expression=".service.*"/>
</context:component-scan>
5.filter标签在Spring3有五个type,如下:

Filter TypeExamples ExpressionDescriptionannotationorg.example.SomeAnnotation符合SomeAnnoation的target classassignableorg.example.SomeClass指定class或interface的全名aspectjorg.example..*Service+AspectJ语法regexorg\.example\.Default.*Regelar Expressioncustomorg.example.MyTypeFilterSpring3新增自訂Type,实作org.springframework.core.type.TypeFilter


1. <context:annotation-config />

它的作用是隐式地向 Spring 容器注册  
- AutowiredAnnotationBeanPostProcessor、
- CommonAnnotationBeanPostProcessor、
- PersistenceAnnotationBeanPostProcessor、
- RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。

其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:

依赖的类注解CommonAnnotationBeanPostProcessor@Resource 、@PostConstruct、@PreDestroyPersistenceAnnotationBeanPostProcessor的Bean@PersistenceContextAutowiredAnnotationBeanPostProcessor Bean@AutowiredRequiredAnnotationBeanPostProcessor@Required

  当然也可以自己进行注册:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>  <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

2. <context:component-scan base-package="com.*" >

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


在这里有一个比较有意思的问题,就是扫描是否需要在二个配置文件都配置一遍,我做了这么几种测试:

            

  (1)只在applicationContext.xml中配置如下

    <context:component-scan base-package="com.login" />

  启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效

  (2)只在spring-servlet.xml中配置上述配置

  启动正常,请求也正常,但是事物失效,也就是不能进行回滚

  (3)在applicationContext.xml和spring-servlet.xml中都配置上述信息

  启动正常,请求正常,也是事物失效,不能进行回滚

  (4)在applicationContext.xml中配置如下

    <context:component-scan base-package="com.login" />

  在spring-servlet.xml中配置如下

    <context:component-scan base-package="com.sohu.login.web" />

  此时启动正常,请求正常,事物也正常了。

  结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。


3. <mvc:annotation-driven />

  它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter



http://www.blogjava.net/crazycy/archive/2014/07/12/415738.html

0 0
原创粉丝点击