Spring 3.1の自動配線からパッケージ/サブパッケージを除外する簡単な方法はありますか?
たとえば、com.example
の基本パッケージにコンポーネントスキャンを含める場合、com.example.ignore
を除外する簡単な方法はありますか?
(なぜ?統合テストから一部のコンポーネントを除外したい)
<exclude-filter>を使用してパッケージを明示的に除外できるかどうかはわかりませんが、regexフィルターを使用すると効果的にそこに到達できます。
<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>
これは、クラスが統合テストのアプリケーションコンテキストに含まれることを意図していないことをソースコード自体に文書化したためです。
同じユースケースに対して、次のように@ComponentScan
を使用しています。これは 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);
}
}
Spring 4の場合、次を使用します
(質問は4歳で、Spring 3.1よりも多くの人がSpring 4を使用しているため、投稿しています):
@Configuration
@ComponentScan(basePackages = "com.example",
excludeFilters = @Filter(type=FilterType.REGEX,pattern="com\\.example\\.ignore\\..*"))
public class RootConfig {
// ...
}
XMLを使用してこれを実行したようですが、新しいSpringのベストプラクティスで作業している場合、設定はJavaになり、次のように除外できます。
@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>
ここで、より多くの例を見つけることができます: フィルターを使用してスキャンをカスタマイズする
私のために働くと思われる1つのことはこれです:
@ComponentScan(basePackageClasses = {SomeTypeInYourPackage.class}, resourcePattern = "*.class")
またはXMLで:
<context:component-scan base-package="com.example" resource-pattern="*.class"/>
これは、デフォルトのresourcePattern
("**/*.class"
)をオーバーライドします。
これは、resourcePatternが常に同じで、ベースパッケージに対して相対的であるため、ベースパッケージのみを含める最もタイプセーフな方法のように思えます。
@ SpringBootApplication を使用することもできます。これは、Springのドキュメントによれば、次の3つの注釈と同じ機能を実行します。@ Configuration 、@ EnableAutoConfiguration@ ComponentScan1つの注釈。
@SpringBootApplication(exclude= {Foo.class})
public class MySpringConfiguration {}
また、次のように特定のパッケージを含めて除外することもできます。
包含および除外(両方)
@SpringBootApplication
(
scanBasePackages = {
"com.package1",
"com.package2"
},
exclude = {org.springframework.boot.sample.class}
)
除外するだけ
@SpringBootApplication(exclude= {com.package1.class})
public class MySpringConfiguration {}