2012-05-23 24 views
52

有沒有一種簡單的方法來排除Spring 3.1中自動裝配的包/子包?例如,如果我想包含基本包com.example的組件掃描,有沒有簡單的方法來排除com.example.ignore從Spring自動裝配中排除子包裝?

(爲什麼呢?我想從我的集成測試排除某些組件)

回答

64

我不知道,你可以用<排除過濾器明確排除包>,但我敢打賭,使用正則表達式過濾器能有效地讓你有:

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

爲了讓註解爲基礎,你會註釋您希望將每個類都排除在集成測試之外,如@ com.example.annotation.ExcludedFromITests。然後,該組件掃描會是什麼樣子:

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

因爲現在你的源代碼本身的類不打算被包括在集成測試的應用程序上下文已經記錄更清晰。

10

這工作在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> 
+0

請注意,如果aspectj不在類路徑中,它會默默忽略排除。 – Vadzim

4

工作,我想你應該重構你的包在更方便的層次,所以他們走出基地的包。

但是,如果你不能做到這一點,請嘗試:

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

在這裏,你可以找到更多的例子:Using filters to customize scanning

+0

謝謝,原則上我同意重構的想法 - 這是我的第一個想法。不幸的是,對我的特殊情況來說這不是一個好的選擇。 – HolySamosa

+0

良好的包裝設計可以避免這種情況,但如果你不能...你不能哈哈哈,:) – richarbernal

39

對於相同的用例,我使用的是@ComponentScan。這與XML答案BenSchro10's相同,但是它使用註釋。兩者都使用的過濾器與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); 
    } 
} 
+0

這是正確的'現代'的答案 –

+0

也許它純粹懷舊,但我其實更喜歡XML – Kirby

+3

它可能是我的,但對於Spring 4.2,我必須使用'FilterType.REGEX'作爲這種方法 – ThanksForAllTheFish

3

有一兩件事似乎爲我的工作是這樣的:

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

或者在XML:

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

這將覆蓋默認resourcePattern這是"**/*.class"

這看起來像是隻包含基礎包的最安全的方法,因爲resourcePattern總是與基礎包相同並且相對於基礎包。

9

好像你通過XML做到了這一點,但如果你是在新的Spring最佳實踐的工作,你的配置是在Java中,你可以排除它們像這樣:

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

雖然你確實意識到這個問題已經超過4歲,並被問及Spring 3.1,對吧? –

+1

@JonathanW但是,對於來自谷歌搜索的人來說,這是一個非常有用的答案 – Stewart

11

對於春天4我使用以下
(我發佈它的問題是4歲,更多的人使用Spring 4比Spring 3。1):

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

您還可以使用@SpringBootApplication,根據Spring文檔它執行相同的功能爲以下三個註解: @Configuration@EnableAutoConfiguration@ComponentScan 在一個註解。

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