从Spring自动assembly中排除子包?

有没有一种简单的方法来排除在Spring 3.1中的自动assembly的包/子包?

例如,如果我想包含一个com.example基础包的组件扫描,是否有一个简单的方法来排除com.example.ignore

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

我不确定你可以使用一个<exclude-filter>来明确地排除软件包,但我打赌使用正则expression式filter可以有效地帮助你:

  <context:component-scan base-package="com.example"> <context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/> </context:component-scan> 

为了使它基于注释,你可以使用类似@ com.example.annotation.ExcludedFromITests的方式注释你想要排除集成testing的每个类。 然后,组件扫描将如下所示:

  <context:component-scan base-package="com.example"> <context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/> </context:component-scan> 

这是更清晰的,因为现在你已经在源代码中logging了这个类不打算包含在集成testing的应用上下文中。

对于相同的用例,我使用@ComponentScan如下所示。 这与BenSchro10的 XML答案是一样的,但是这使用了注释。 两者都使用type=AspectJ的filter

 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); } } 

这在Spring 3.0.5中可用。 所以,我认为它会在3.1

 <context:component-scan base-package="com.example"> <context:exclude-filter type="aspectj" expression="com.example.dontscanme.*" /> </context:component-scan> 

看来你已经通过XML完成了这个工作,但是如果你正在使用新的Spring最佳实践,那么你的configuration将是Java,你可以这样排除它们:

 @Configuration @EnableWebMvc @ComponentScan(basePackages = "net.example.tool", excludeFilters = {@ComponentScan.Filter( type = FilterType.ASSIGNABLE_TYPE, value = {JPAConfiguration.class, SecurityConfig.class}) }) 

对于Spring 4,我使用以下内容
(我发布这个问题是因为问题是4岁,更多的人使用Spring 4比Spring 3.1):

 @Configuration @ComponentScan(basePackages = "com.example", excludeFilters = @Filter(type=FilterType.REGEX,pattern="com\\.example\\.ignore\\..*")) public class RootConfig { // ... } 

我认为你应该在更方便的层次结构中重构你的包,所以它们不在基础包中。

但是,如果你不能这样做,请尝试:

 <context:component-scan base-package="com.example"> ... <context:exclude-filter type="regex" expression="com\.example\.ignore.*"/> </context:component-scan> 

在这里你可以find更多的例子: 使用filter来自定义扫描

有一件事情对我来说似乎是这样的:

 @ComponentScan(basePackageClasses = {SomeTypeInYourPackage.class}, resourcePattern = "*.class") 

或者在XML中:

 <context:component-scan base-package="com.example" resource-pattern="*.class"/> 

这覆盖了默认的"**/*.class" resourcePattern

这看起来像是只包含基本包的最安全的方式,因为resourcePattern总是与您的基本包相同。

你也可以使用@SpringBootApplication ,根据Spring文档,它和下面的三个注释具有相同的function: @ Configuration, @ EnableAutoConfiguration @ComponentScan在一个注释中。

 @SpringBootApplication(exclude= {Foo.class}) public class MySpringConfiguration {}