通常、@Autowired などの Spring でアノテーションを有効にするには、Spring XML にこれを含めることで行います。
<context:annotation-config/>
初期化する前に、ApplicationContext (または実装) でプログラムでこれを行う方法はありますか?
通常、@Autowired などの Spring でアノテーションを有効にするには、Spring XML にこれを含めることで行います。
<context:annotation-config/>
初期化する前に、ApplicationContext (または実装) でプログラムでこれを行う方法はありますか?
@Configuration クラスで AnnotationConfigApplicationContext クラスを使用するだけで十分です。Java ベースのコンテナ構成は、コンポーネント スキャンの実行に依存しません。これは、XML ベースのコンポーネント構成に対する別のアプローチにすぎません。
次のリンクを参照してください。
回避策として、次の内容の単純な spring-aspect-config.xml ファイルを作成しました。
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<context:spring-configured/>
そしてそれを私のアプリケーションコンテキストクラスにインポートとして追加しました
@ImportResource("classpath:spring-aspect-config.xml")
プログラムで指定することで同じことができます
@EnableSpringConfigured
あなたのアプリケーションコンテキストクラスで。
このハックが本当に必要な場合はAnnotationConfigBeanDefinitionParser.parse()
、コンテキストを操作する方法と、コンテキストに登録する Bean 定義の種類を確認してから、ApplicationContext 実装を使用してプログラムで再現して、同じ効果を得ることができます。
この投稿は、Bean レジストリに新しい Bean 定義を追加する方法について役立つ場合があります。