Spring配置之<context:include-filter/>和<context:exclude-filter/>详解

本文详细解析了SpringMVC中组件扫描的配置方法,包括<context:component-scan>的使用,<context:include-filter>和<context:exclude-filter>的区别及应用技巧,帮助开发者正确配置控制器和服务层。

我们在Spring MVC中的配置中一般会遇到这两个标签,作为<context:component-scan />的子标签出现。我们在使用这两个标签时候要注意以下几点:
1、我们经常Spring-applicationContext.xml和Spring-MVC.xml分开配置,在Spring-MVC.xml中只对@Controller扫描即可,作为一个控制器,其他事情不需要做,在Spring-applicationContext.xml只对一些事务逻辑的注解扫描。context:exclude-filter 指定扫描包时要排除的类,context:include-filter 指定扫描时要包含的类。
2、现在给定一个项目包的机构:

com.wzy.controlller
com.wzy.service

如,给两个包机构
(1)Spring-MVC.xml中的配置:

<!-- 扫描@Controller注解 -->
<context:component-scan base-package="com.wzy.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

不难看出,最终将包写上,而不能这样写base-package=”com.wzy”,这种写法对于include-filter来讲它都会扫描,而不是仅仅扫描@Controller,这点需要注意,他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use-default-filters=”false”。如:

<context:component-scan base-package="com.wzy" use-default-filters="false">
    <!--
    指定只扫描哪些组件,默认是全都扫描进来,所以需要设置use-default-filters="false"
    -->
    <context:include-filter 
            type="annotation"
            expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

(2)Spring-applicationContext.xml中的配置:

type="annotation":
<!-- 配置扫描注解,不扫描@Controller注解 -->
<context:component-scan base-package="com.wzy">
   <!--
   type="annotation":按照注解进行排除,标注了指定注解的组件不要扫描
   expression:注解的全类名
   -->
   <context:exclude-filter
           type="annotation"
           expression=" org.springframework.stereotype.Controller"/>
</context:component-scan>


 type="assignable"
<context:component-scan base-package="com.wzy">
  <!-- 
         type="assignable" :指定排除某个具体的类,按照类排除
         expression:类的全类名
  -->
  <context:exclude-filter 
          type="assignable" 
          expression="com.hh.service.BookService"/>
</context:component-scan>

可以看出,这是要扫描com.wzy包下的所有子类,不包含@Controller了,对于exculude-filter不存在包不精确后都进行扫描的问题。

server: port: 9999 undertow: # max-http-post-size: 10MB # 平替 tomcat server.tomcat.max-swallow-siz, undertow该值默认为-1 worker-threads: 16 # 4核CPU标准配置 buffers: websocket: 8192 # WebSocket缓冲 以字节为单位,这里设置为8 KB io: 16384 # IO操作缓冲 以字节为单位,这里设置为16 KB error: include-exception: true include-stacktrace: ALWAYS include-message: ALWAYS servlet: context-path: /daochuan compression: enabled: true min-response-size: 1024 mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/* management: endpoints: web: exposure: include: metrics,jeecghttptrace spring: flyway: # 是否启用flyway enabled: false # 是否关闭要清除已有库下的表功能,生产环境必须为true,否则会删库,非常重要!!! clean-disabled: true servlet: multipart: max-file-size: 50MB max-request-size: 50MB mail: # 定时任务发送邮件 timeJobSend: false host: smtp.163.com username: jeecgos@163.com password: ?? properties: mail.smtp.timeout: 10000 # 连接超时(毫秒) mail.smtp.connectiontimeout: 10000 # 连接超时(毫秒) mail.smtp.writetimeout: 10000 # 写入超时(毫秒) mail.smtp.auth: true smtp.ssl.enable: true mail.debug: true # 启用调试模式(查看详细日志) ## quartz定时任务,采用数据库方式 quartz: job-store-type: jdbc initialize-schema: embedded #定时任务启动开关,true-开 false-关 auto-startup: true #延迟1秒启动定时任务 startup-delay: 1s #启动时更新己存在的Job overwrite-existing-jobs: true properties: org: quartz: scheduler: instanceName: MyScheduler instanceId: AUTO jobStore: class: org.springframework.scheduling.quartz.LocalDataSourceJobStore driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate tablePrefix: QRTZ_ isClustered: true misfireThreshold: 12000 clusterCheckinInterval: 15000 threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true #json 时间戳统一转换 jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 jpa: open-in-view: false aop: proxy-target-class: true #配置freemarker freemarker: # 设置模板后缀名 suffix: .ftl # 设置文档类型 content-type: text/html # 设置页面编码格式 charset: UTF-8 # 设置页面缓存 cache: false prefer-file-system-access: false # 设置ftl文件路径 template-loader-path: - classpath:/templates template_update_delay: 0 # 设置静态文件路径,js,css等 mvc: static-path-pattern: /** #Spring Boot 2.6+后映射匹配的默认策略已从AntPathMatcher更改为PathPatternParser,需要手动指定为ant-path-matcher pathmatch: matching-strategy: ant_path_matcher resource: static-locations: classpath:/static/,classpath:/public/ autoconfigure: exclude: - com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure - org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration datasource: druid: stat-view-servlet: enabled: true loginUsername: admin loginPassword: 123456 allow: web-stat-filter: enabled: true dynamic: druid: # 全局druid参数,绝大部分值默认保持一致。(现已支持的参数如下,不清楚含义不要乱设置) # 连接池的配置信息 # 初始化大小,最小,最大 initial-size: 5 min-idle: 5 maxActive: 1000 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 testWhileIdle: true testOnBorrow: false testOnReturn: false # 打开PSCache,并且指定每个连接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,slf4j # 允许SELECT语句的WHERE子句是一个永真条件 wall: selectWhereAlwayTrueCheck: false # 打开mergeSql功能;慢SQL记录 stat: merge-sql: true slow-sql-millis: 5000 datasource: master: url: jdbc:mysql://8.153.87.117:3306/daochuan_plus?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root password: Wyf341024+ driver-class-name: com.mysql.cj.jdbc.Driver
05-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值