English 中文(简体)
排除 Spring 自动接线的子包吗?
原标题:Exclude subpackages from Spring autowiring?

是否有一种简单的方法将包件/子包件排除在春季3.1的自动电线之外?

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

(为什么?我想将某些部件排除在我的整合测试之外)

最佳回答

我不确定您是否可以使用 & lt; exclude- filter- filter & gt; 明确地排除包包, 但我敢打赌, 使用 Regex 过滤器可以有效地将您送到那里 :

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

为了使它以注释为基础, 您给每个您想要被排除参加整合测试的班级都做笔记, 比如 : @ com.example. annotation. exclused fromItests. exclusations. exclusions. exclusmissions. exclusmissions. excasts.

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

这一点更清楚,因为现在你已经在源代码本身中记录了,该类人并不打算列入集成测试的申请范围。

问题回答

我用以下的conconcontonentScan 来表示相同的使用。 这与 < a href=" https://stackoverflow.com/uses/1403498/benschro10" > BenSchro10 s 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);
    }
}

For Spring 4 I use the following
(I am posting it as the question is 4 years old and more people use Spring 4 than Spring 3.1):

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

似乎你通过XML做到了这一点, 但如果你在新的 " 春天 " 最佳做法中工作, 你的配置将会在爪哇, 你可以把它们排除在外:

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

这个在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> 

我认为你应该重新构思你的包件 在更方便的等级结构, 所以他们是出 基本包包。

但如果你做不到 试试看

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

您可以在此找到更多例子 : < a href=""http://docs.spring.io/spring/docs/ pext/ spring- framework- reference/ htmlsingle/ #beans-scanning- filters" rel="nofollow" > 使用过滤器自定义扫描

有一件事似乎对我有用 是这样的:

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

在 XML 中 :

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

这将超过默认的 resources Patter , 即 , 即 **/*. class"

这似乎是将基本包只包括在内的最安全的方式,因为该资源生产者将始终与你的基本包相同,而且与你的基本包相对。

You can also use @SpringBootApplication, which according to Spring documentation does the same functionality as the following three annotations: @Configuration, @EnableAutoConfiguration @ComponentScan in one annotation.

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

您也可以包含特定的软件包, 并排除它们, 如 :

<强 > 包括并排除

 @SpringBootApplication
        (
                scanBasePackages = {
                        "com.package1",
                        "com.package2"
                },
                exclude = {org.springframework.boot.sample.class}
        )

<强 > JUST 排除

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




相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...