2016-12-20 3 views
5

<import>が別のxmlであり、すべてのパッケージをスキャンする必要があるコンテキストで、コンポーネントスキャンを使用して読み込む一部のBeanを除外する方法。 これは私がメインのコンテキストにそれを置く場合nicly作品:テストコンテキストからクラスを除外する

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

しかし、私はライブ環境でのコントローラが必要です。

コントローラクラスの読み込みを統合テストコンテキストから除外したいと思います。これをどのように達成することが可能でしょうか?

答えて

3

あなたは、特定の@ActiveProfile("test")

EDITを使用してテストを

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
     <!-- define profile beans at the end of the configuration file --> 
    <beans profile="test"> 
    <context:component-scan base-package="com.main"> 
      <context:exclude-filter expression="com.main.*Controller" type="regex"/> 
    </context:component-scan> 
    </beans> 

    <beans profile="!test"> 
    <context:component-scan base-package="com.main"/> 
    </beans> 

を使用して注釈を付けることによって、その(参照How to set a Spring profile to a package?)のための春のプロファイルを使用することができます。

あなたのXMLが定義されていない場合<component:scan>タグを使用すると、Java構成を使用してユニットテストからパッケージスキャンを制御できます。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) 
public class HelperTest { 

    @Configuration 
    @ComponentScan(basePackages = "yourPackage", 
      excludeFilters = @ComponentScan.Filter(value = Controller.class, type = FilterType.ANNOTATION)) 
    @ImportResource(locations = "classpath:context.xml") 
    static class TestConfiguration { 


    } 
+1

私はメインのコンテキストを変更したくない次のよう コントローラは、その後@ComponentScan excludeFilterを除外することができます。可能であれば、テストの中で何を除外すべきかを言いたいと思います。 :) –

+0

私は答えを更新し、それが合うかどうか教えてください。 –

関連する問題