2

あるスプリングコンテキストを別のモジュールのスプリングコンテキストからインポートするにはどうすればよいですか?

module2 --uses--> module1

私はmodule1とmodule2(私のMavenモデル)を持っていますが、どちらも春のアプリです。両方のプロジェクトは同じ親ディレクトリにあります。

module1には'module1/ Bean1'クラスがあります(これは春の豆です)

moodule2はmodule1を使用することになっています。したがって、module2 / src / main / resources / context2.xmlで、次のように「参照」をcontext1に配置しようとします。

context2.xml:

 <import resource="classpath*:module1/resources/context1.xml" />

したがって、module2でこのコードを使用することを期待しています。

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
                        new String[] {"context2.xml"});
Bean1 bean1 = (Bean1) appContext.getBean("bean1"); // It uses bean defined in the module1

しかし、今は例外があります。 「bean1」という名前のBeanは定義されていません。

--context1.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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />
    <context:component-scan base-package="module1"/>    <!-- for @Component, @Service, @Repository being scanned in particular folder -->

4

1 に答える 1

1

context1.xml が /module1/src/main/resources/ の下にある場合、代わりに

<import resource="classpath*:module1/resources/context1.xml" />

使用する必要があります

<import resource="classpath*:context1.xml" />
于 2012-08-17T14:20:35.360 に答える