Spring 3.1でパッケージ/サブパッケージを自動配線から除外する簡単な方法はありますか?
たとえば、の基本パッケージにコンポーネントスキャンを含めたい場合、com.example
除外する簡単な方法はありcom.example.ignore
ますか?
(なぜですか?統合テストから一部のコンポーネントを除外したいのですが)
<exclude-filter>を使用してパッケージを明示的に除外できるかどうかはわかりませんが、正規表現フィルターを使用すると効果的にそこに到達できると思います。
<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の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歳であり、Spring3.1よりもSpring4を使用する人が多いため投稿しています)。
@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})
})
これはSpring3.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のドキュメントによると、1つのアノテーションで@ Configuration、@ EnableAutoConfiguration@ ComponentScanの3つのアノテーションと同じ機能を実行します 。
@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 {}
既存の回答への単なる追加。
サブパッケージからクラスを除外したいが、基本パッケージからは除外したくない場合は、次のように変更"com.example.ignore.*
できます。"com.example.ignore.*..*"
スプリングブートでこれを確認しました:2.4.1
この回答から取得したコードスニペット
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);
}
}