首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否从Spring自动装配中排除子包?

是否从Spring自动装配中排除子包?
EN

Stack Overflow用户
提问于 2012-05-24 01:46:06
回答 7查看 137.1K关注 0票数 90

在Spring3.1中,有没有一种简单的方法可以从自动装配中排除一个包/子包?

例如,如果我想在com.example的基础包中包含一个组件扫描,有没有一种简单的方法可以排除com.example.ignore

(为什么?我想从我的集成测试中排除一些组件)

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2012-05-24 02:11:02

我不确定您是否可以使用显式排除包,但我打赌使用正则表达式过滤器将有效地将您带到那里:

代码语言:javascript
运行
复制
 <context:component-scan base-package="com.example">
    <context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/>
 </context:component-scan>

要使其基于注释,您可以使用诸如@com.example.annotation.ExcludedFromITests之类的内容来注释您希望在集成测试中排除的每个类。然后,组件扫描将如下所示:

代码语言:javascript
运行
复制
 <context:component-scan base-package="com.example">
    <context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/>
 </context:component-scan>

这一点更清楚了,因为现在您已经在源代码本身中记录了该类并不打算包含在集成测试的应用程序上下文中。

票数 91
EN

Stack Overflow用户

发布于 2015-05-13 03:27:30

对于相同的用例,我使用@ComponentScan的方法如下所示。这与BenSchro10's XML answer相同,但使用了注释。两者都使用带有type=AspectJ的过滤器

代码语言:javascript
运行
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.example" },
    excludeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.example.ignore.*"))
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
票数 61
EN

Stack Overflow用户

发布于 2016-08-11 02:47:26

看起来您已经通过XML实现了这一点,但是如果您使用的是新的Spring最佳实践,那么您的配置应该是Java的,并且您可以将它们排除在外:

代码语言:javascript
运行
复制
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.example.tool",
  excludeFilters = {@ComponentScan.Filter(
    type = FilterType.ASSIGNABLE_TYPE,
    value = {JPAConfiguration.class, SecurityConfig.class})
  })
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10725192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档