spring 排除指定的类或者包扫描

<!-- 排除Controller注解的扫描 -->
<context:component-scan base-package="exampleBean">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 排除扫描符合正则表达式的类,此处排除com.wx.comm.util包下的所有类 -->
<context:component-scan base-package="exampleBean">
    <context:exclude-filter type="regex"
        expression="com.wx.comm.util.*" />
</context:component-scan>

<!-- 排除指定包exampleBean下的CommFF类的扫描 -->
<context:component-scan base-package="exampleBean">
    <context:exclude-filter type="assignable"
        expression="exampleBean.CommFF" />
</context:component-scan>

参考:https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/

7.10.4 Using filters to customize scanning

By default, classes annotated with @Component@Repository@Service@Controller, or a custom annotation that itself is annotated with @Component are the only detected candidate components. However, you can modify and extend this behavior simply by applying custom filters. Add them as includeFilters or excludeFiltersparameters of the @ComponentScan annotation (or as include-filter or exclude-filter sub-elements of the component-scan element). Each filter element requires the type and expression attributes. The following table describes the filtering options.

Filter Type Example Expression Description

annotation (default)


org.example.SomeAnnotation


An annotation to be present at the type level in target components.


assignable


org.example.SomeClass


A class (or interface) that the target components are assignable to (extend/implement).


aspectj


org.example..*Service+


An AspectJ type expression to be matched by the target components.


regex


org\.example\.Default.*


A regex expression to be matched by the target components class names.


custom


org.example.MyTypeFilter


A custom implementation of the org.springframework.core.type .TypeFilter interface.

The following example shows the configuration ignoring all @Repository annotations and using "stub" repositories instead.

@Configuration
@ComponentScan(basePackages = "org.example",
        includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
        excludeFilters = @Filter(Repository.class))
public class AppConfig {
    ...
}

原文地址:https://www.cnblogs.com/815346909/p/10330058.html

时间: 2024-07-29 04:43:12

spring 排除指定的类或者包扫描的相关文章

Spring IoC 源码分析 (基于注解) 之 包扫描

在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫描的过滤规则.那我们今天就来看下包扫描的具体过程. 还是先看下面的代码: AnnotationConfigApplicationContext类 //该构造函数会自动扫描以给定的包及其子包下的所有类,并自动识别所有的Spring Bean,将其注册到容器中 public AnnotationConf

整合Spring时Service层为什么不做全局包扫描详解

转载:http://blog.csdn.net/s740556472/article/details/54879954 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两个框架中,web.xml会这样写到: <!-- 加载spring容器 --> <!-- 初始化加载application.xml的各种配置文件 --> <context-param> <param-name&

Spring 自动转配类 在类中使用@Bean 注解进行转配但是需要排除该类说明

在spring中可以使用 @Component @Configuration @Bean(实例化后返回该bean)进行类实例的自动装配. 需求: 排除指定需要自动转配的类. 说明: 1.在以上注解中 @Component @Configuration 可以通过 SpringApplication(exclude/excludeName) / @ComponentScan(excludeFilters={@Filter(type=FilterType.ANNOTATION,value=Enable

为啥Spring和Spring MVC包扫描要分开

开始学习springmvc各种小白问题 根据例子配置了spring扫描包,但是一直提示404错误,经过大量搜索,发现,扫描包的配置应该写在springmvc的配置文件中,而不是springmvc 配置成功的applicationContext 即 spring配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/sche

spring包扫描问题

最近做项目时发现一个spring包扫描问题,项目中使用spring.springMVC.mybatis框架,因为整个项目是按模块来分布式开发,最终将各个模块整合在一起,但是整合时发现有些模块的service层和controller层包没有扫描到. 举个例子:A模块的service层包为 com.project.A.service,B模块的service层的包为com.project.base.B.service:在spring的配置文件里包扫描器的配置为: <context:component-

java动态加载指定的类或者jar包反射调用其方法

序言 有时候,项目中会用到java动态加载指定的类或者jar包反射调用其方法来达到模块的分离,使各个功能之间耦合性大大降低,更加的模块化,代码利用率更高.模式中的代理模式就用到java的这一机制.下边就让我们通过代码来看看如何实现此功能. 代码详细 package loadjarclass; import java.io.File; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoad

java动态载入指定的类或者jar包反射调用其方法

序言 有时候.项目中会用到java动态载入指定的类或者jar包反射调用其方法来达到模块的分离,使各个功能之间耦合性大大减少,更加的模块化.代码利用率更高.模式中的代理模式就用到java的这一机制. 下边就让我们通过代码来看看怎样实现此功能. 代码具体 package loadjarclass; import java.io.File; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoa

为啥Spring和Spring MVC包扫描要分开?

背景:       最近在搭建新工程的时候发现有些Spring的配置不是很了解,比如Spring 配置里面明明配置了component-scan,为啥Spring MVC配置文件还需要配置一下,这样岂不是多此一举?由于以前基本是在现有的工程上直接开发或者别的工程的配置文件直接拷贝过来,所以也没太关注这个问题.出于好奇,谷歌了一下发现原来这个里面大有学问呢,详情请见下文.正常代码如下: Xml代码   <!-- spring 配置文件--> <context:component-scan 

Spring 的优秀工具类盘点第 2 部分

特殊字符转义 由于 Web 应用程序需要联合使用到多种语言,每种语言都包含一些特殊的字符,对于动态语言或标签式的语言而言,如果需要动态构造语言的内容时,一个我们经常会碰到的问题就是特殊字符转义的问题.下面是 Web 开发者最常面对需要转义的特殊字符类型: HTML 特殊字符: JavaScript 特殊字符: SQL 特殊字符: 如果不对这些特殊字符进行转义处理,则不但可能破坏文档结构,还可以引发潜在的安全问题.Spring 为 HTML 和 JavaScript 特殊字符提供了转义操作工具类,