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

Hol*_*osa 75 spring autowired

有没有一种简单的方法可以在Spring 3.1中从自动装配中排除包/子包?

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

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

Jon*_*n W 81

我不确定您是否可以使用<exclude-filter>明确排除软件包,但我打赌使用正则表达式过滤器可以有效地将您带到那里:

 <context:component-scan base-package="com.example">
    <context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/>
 </context:component-scan>
Run Code Online (Sandbox Code Playgroud)

要使其基于注释,您需要使用@ com.example.annotation.ExcludedFromITests等注释要为集成测试排除的每个类.然后组件扫描看起来像:

 <context:component-scan base-package="com.example">
    <context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/>
 </context:component-scan>
Run Code Online (Sandbox Code Playgroud)

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

  • @阿明这可能是因为“。” 考虑任何字符,包括文字“。” (3认同)

Kir*_*rby 52

@ComponentScan对于相同的用例,我使用如下.这与BenSchro10的 XML答案相同,但这使用了注释.两者都使用过滤器type=AspectJ

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它可能是我,但是对于Spring 4.2,我必须使用`FilterType.REGEX`来实现这种方法 (7认同)
  • 只是子包的注释。如果你想排除它的子包 `"com.example.ignore"` 应该是 `pattern = "com.example.ignore..*"` (* 之前多一个点) (2认同)

Wit*_*rba 27

对于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 {
    // ...
}
Run Code Online (Sandbox Code Playgroud)


Ars*_*lid 12

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

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

  • @JonathanW但是,对于从谷歌搜索进入此页面的人来说,这是一个非常有用的答案 (5认同)

小智 11

这适用于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> 
Run Code Online (Sandbox Code Playgroud)


ric*_*nal 6

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

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

<context:component-scan base-package="com.example">
    ...
    <context:exclude-filter type="regex" expression="com\.example\.ignore.*"/>
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到更多示例:使用过滤器自定义扫描