0

私はしばらくの間、XML ベースの構成を使用してきました。Spring を DI として使用する Vaadin アプリケーションがありますが、興味はありませんDispacherServlet。グローバル (ユーザー所有の依存関係ではなく) を注入するために使用するルート コンテキストのみです。

それが機能する方法コンテンツを含むファイルを
定義しました:root-context.xml

<context:annotation-config />
<context:spring-configured />
<context:load-time-weaver />
<context:component-scan base-package="com.example" />

そして、私web.xmlはそれを持っています:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

次に、私のクラスのいくつかは注釈で定義され@Component、他のクラスは注釈で定義されます(後者は主にユーザーセッションに属しているため、キーワード@Configurableで作成されたインスタンスごとにDIが必要です)。new

context.xmlの行のファイルがあります。

<Loader delegate="false" loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" />

そしてspring-instrument-tomcat-3.2.1.RELEASE.jarTomcatのlibディレクトリにあります。
すべての依存関係が (を使用して@Autowire)@Configurableクラスに正しく注入されます。

動作しない方法 最近、コンテキストの初期化を取り除き、Javaクラスに移動
しようとしました。 次のようにクラスを作成しました。root-context.xml@Configuration

@Configuration
@EnableSpringConfigured
@EnableLoadTimeWeaving
@ComponentScan("com.example")
public class BeansConfiguration
{
}

さらに、web.xmlエントリを変更しました:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.example.spring.BeansConfiguration</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

残念ながら、奇妙なことが起こり始めました。例を示しましょう。クラス構造を単純化すると、次のようになります。

@Component
public class ComponentA
{
}

@Configurable
public class BeanB
{
    @Autowired
    private ComponentA componentA;
}

@Configurable
public class BeanC
{
    @Autowired
    private ComponentA componentA;

    private BeanB beanB;

    public BeanC(BeanB beanB)
    {
        this.beanB = beanB;
    }
}

@Configurable
public class Application
{
    @Autowired
    private ComponentA componentA;

    public Application()
    {
    }

    public void init()
    {
        BeanC beanC = new BeanC(new BeanB());
    }
}

XML セットアップを使用すると、機能すると、ComponentASpring によってすべての@Configurableオブジェクトに正しく挿入されます。奇妙なことに、アノテーションのみの構成では注入されません (常にBeanCです) 。ComponentAnullBeanBApplication

なぜそれが起こるのか、何か考えはありますか?行をコメントアウトしweb.xmlて以前の (XML ベースの) 構成に戻るとすぐに、すべてが機能し始めます。

幸いなことに、XML Spring エントリは、対応するアノテーション ベースのエントリよりも隠れた何かを登録していると思います。私はそれが何であるかを見つけようとして半日を費やしましたが、あきらめました. 提案をいただければ幸いです。

4

0 に答える 0