从另一个模块加载弹簧配置

时间:2014-01-21 13:49:07

标签: spring configuration spring-batch

我正在开发一个包含子模块的spring应用程序,大致如下所示:

project
|-- module1
|   |-- src
|   |   `-- main
|   |       |-- java
|   |       `-- resources
|   |           |-- applicationContext.xml
|   |           `-- web.xml
|   `-- pom.xml
|-- module2
|   |-- src
|   |   `-- main
|   |       |-- java
|   |       `-- resources
|   |           `-- batch-jobs.xml
|   `-- pom.xml
`-- pom.xml

module1 包含网络应用配置。 module2 包含使用spring-batch运行批处理作业,这些作业在batch-jobs.xml中配置。

applicationContext.xml内,我有以下一行:

<import resource="classpath*: batch-jobs.xml" />

据我所知,这个文件正在加载。我假设这是因为之前我使用的是classpath: batch-jobs.xml(没有*),但找不到该文件。

尽管加载了这个文件,我得到了NoSuchBeanDefinitionException。如果我将所有内容从batch-jobs.xml复制到applicationContext.xml,则可以正常使用。

4 个答案:

答案 0 :(得分:3)

当你使用像这样的星号时

<import resource="classpath*: batch-jobs.xml" />

它会忽略它找不到的文件(除其他外)

参见例如this question了解详情

确保使用前导斜杠引用其弹簧配置xml,如下所示:

<import resource="classpath:/batch-jobs.xml" />

如果仍然抱怨文件batch-jobs.xml未找到,请确保您的类路径上有模块2 jar(将module2的依赖性添加到模块1)。

答案 1 :(得分:0)

您可以使用maven复制资源插件将资源复制到module1项目(在module1 pom.xml中添加复制资源插件)。假设你修改了import语句<import resource="classpath*: springjobs/batch-jobs.xml" />你可以使用copy resource plugin将x2文件从module2的资源目录复制到module1。

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy--common-resources</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/src/main/resources/spingjobs</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/../project/module2/src/main/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

答案 2 :(得分:0)

当您有多个模块且基本Web模块不知道插件(审计,日志记录)等捆绑模块时,您可以使用以下方法。

classpath*:applicationContext-*.xml

您必须遵循spring配置XML文件(applicationContext-m1.xml,applicationContext-m2.xml)的命名约定,才能使用spring的类路径扫描加载配置。

答案 3 :(得分:0)

这也可以使用maven配置来实现。我不知道你是否正在实现Maven。但是你可以按照下面的说法来实现这个目标......

Module A, 
xml file of A is a.xml
Module B.
xml file of B is b.xml

I have to include the config file of Module B in Module A.

Step 1:
In Module A pom.xml file include the dependency of Module B.
        <dependency>
            <groupId><module_package></groupId>
            <artifactId><module_name></artifactId>
            <version>1.0</version>
        </dependency>

Step 2:
Go to the xml configuration file(eg a.xml) and import the configuration file of Module B.
<import resource="b.xml"/>
相关问题