zoukankan      html  css  js  c++  java
  • context:exclude-filter 与 context:include-filter 转

    context:exclude-filter 与 context:include-filter 转

    1 在主容器中(applicationContext.xml),将Controller的注解打消掉

    1. <context:component-scan base-package="com">  
    2.   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
    3. </context:component-scan>   


    2 而在springMVC配置文件中将Service注解给去掉 

    1. <context:component-scan base-package="com">  
    2.   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
    3.   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
    4.   </context:component-scan>   


    因为spring的context是父子容器,所以会产生冲突,Controller会进步前辈行扫描装配,而此时的 Service还没有进行事务的加强处理惩罚,获得的将是原样的Service(没有经过事务加强处理惩罚,故而没有事务处理惩罚才能) ,最后才是applicationContext.xml中的扫描设备进行事务处理惩罚

    上面的好像有错

    Spring注解自动注入Bean

    我们知道采用Spring注解时,配置如下:

    1. <context:annotation-config />  
    2.   
    3. <context:component-scan base-package="cn.itkt"></context:component-scan>  

    这样的话,在com包及其所有子包下的所有类如果含有@Component、@Controller、@Service、@Repository等 注解的话都会自动纳入到Spring容器中,但是每个类都一个个加上注解,有时难免觉得繁琐,其实Spring也为我们提供了自动为类加上注解的功能。配 置如下:

    1. <context:component-scan base-package="cn.itkt" use-default-filters="false">  
    2.     <context:include-filter type="regex" expression="cn.itkt.*.service.*.*" />  
    3.     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
    4. </context:component-scan>  

    我们可以看到加了context:include-filter标签和context:exclude-filter标签。

    context:include-filter

    此标签的含义是:在其扫描到的所有类中,全部自动加上注解并纳入Spring容器中,比如有个类为

    1. public class StudentService implements IStudentService {  
    2. }  

    那么该标签等用于为StudentService类加上@Component注解,且bean的id为studentService。

    1. @Component("studentService")  
    2. public class StudentService implements IStudentService {  
    3. }  

    context:exclude-filter

    此标签的含义是:排除扫描到的所有类,不纳入Spring容器中。

    但需要注意的是,采用自动注入,类名不能相同(即便包名不同),因为自动注入时,id与类名相同,所以如果两个类名一样的话,会因为Bean的id相同而报错。

    如果类名一定要相同的话,只能是其中一个类,手动加上注解并将名称改为其他。

  • 相关阅读:
    SAMBA服务和FTP服务讲解(week3_day1)--技术流ken
    Linux网络技术管理及进程管理(week2_day4)--技术流ken
    RAID磁盘阵列及CentOS7系统启动流程(week2_day3)--技术流ken
    Linux磁盘管理及LVM讲解(week2_day2)--技术流ken
    Linux计划任务及压缩归档(week2_day1)--技术流ken
    Linux权限管理(week1_day5)--技术流ken
    Linux高级命令进阶(week1_day2)--技术流ken
    k8s集群监控(十一)--技术流ken
    k8s部署使用Dashboard(十)--技术流ken
    k8s应用机密信息与配置管理(九)--技术流ken
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/5119733.html
Copyright © 2011-2022 走看看