1

通常、@Autowired などの Spring でアノテーションを有効にするには、Spring XML にこれを含めることで行います。

  <context:annotation-config/>

初期化する前に、ApplicationContext (または実装) でプログラムでこれを行う方法はありますか?

4

3 に答える 3

1

@Configuration クラスで AnnotationConfigApplicationContext クラスを使用するだけで十分です。Java ベースのコンテナ構成は、コンポーネント スキャンの実行に依存しません。これは、XML ベースのコンポーネント構成に対する別のアプローチにすぎません。

次のリンクを参照してください。

コードランチ

Spring アノテーション - Spring Bean の自動構築を呼び出す @Configuration

于 2013-03-01T12:51:56.210 に答える
1

回避策として、次の内容の単純な 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 

あなたのアプリケーションコンテキストクラスで。

参照 - https://jira.springsource.org/browse/SPR-7888

于 2013-11-11T12:38:47.147 に答える
0

このハックが本当に必要な場合はAnnotationConfigBeanDefinitionParser.parse()、コンテキストを操作する方法と、コンテキストに登録する Bean 定義の種類を確認してから、ApplicationContext 実装を使用してプログラムで再現して、同じ効果を得ることができます。

この投稿は、Bean レジストリに新しい Bean 定義を追加する方法について役立つ場合があります。

于 2013-03-01T12:59:26.133 に答える