从另一个模块加载spring配置
我正在开发一个包含子模块的spring应用程序,大致如下所示:从另一个模块加载spring配置,spring,configuration,spring-batch,Spring,Configuration,Spring Batch,我正在开发一个包含子模块的spring应用程序,大致如下所示: project |-- module1 | |-- src | | `-- main | | |-- java | | `-- resources | | |-- applicationContext.xml | | `-- web.xml | `-- pom.xml |-- module2 | |-- src | | `--
project
|-- module1
| |-- src
| | `-- main
| | |-- java
| | `-- resources
| | |-- applicationContext.xml
| | `-- web.xml
| `-- pom.xml
|-- module2
| |-- src
| | `-- main
| | |-- java
| | `-- resources
| | `-- batch-jobs.xml
| `-- pom.xml
`-- pom.xml
模块1包含web应用程序配置。
module2包含使用spring批处理
运行批处理作业,这些批处理作业在batch jobs.xml
中配置
在applicationContext.xml
中,我有以下行:
<import resource="classpath*: batch-jobs.xml" />
据我所知,这个文件正在加载。我假设这是因为之前我使用的是classpath:batch jobs.xml
(没有*
),它找不到文件
尽管加载了这个文件,我还是得到了一个
NoSuchBeanDefinitionException
。如果我将batch jobs.xml
中的所有内容复制到applicationContext.xml
,效果很好。您可以使用maven copy resource plugin将资源复制到module1项目(在module1 pom.xml中添加复制资源插件)。假设您修改了导入语句
,您可以使用复制资源插件将xml文件从模块2的资源目录复制到模块1
<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>
org.apache.maven.plugins
maven资源插件
2.6
复制--公共资源
初始化
复制资源
${basedir}/src/main/resources/spingjobs
${basedir}/./project/module2/src/main/resources
当您有多个模块,并且您的基本web模块不知道插件(审计、日志)之类的捆绑模块时,您可以使用以下方法
您必须遵循spring配置XML文件(applicationContext-m1.XML,applicationContext-m2.XML)的命名约定,以便使用spring的类路径扫描加载配置。这也可以通过使用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"/>
模块A,
的xml文件是A.xml
模块B。
B的xml文件是B.xml
我必须在模块A中包含模块B的配置文件。
步骤1:
在模块A中,pom.xml文件包含模块B的依赖项。
1
步骤2:
转到xml配置文件(例如a.xml)并导入模块B的配置文件。
当您像这样使用asterix时
<import resource="classpath*: batch-jobs.xml" />
它会忽略找不到的文件(除其他外)
有关详细信息,请参见例如
请确保使用前导斜杠引用其spring配置xml,如下所示:
<import resource="classpath:/batch-jobs.xml" />
如果它仍然抱怨文件batch-jobs.xml找不到,请确保类路径上有模块2 jar(将对模块2的依赖添加到模块1) 模块1是否在运行时看到模块2的类资源?例如,batch-jobs.xml文件是否在类路径中?谢谢您的想法,但目录不干净
<import resource="classpath:/batch-jobs.xml" />