2014-01-21 77 views
4

我的工作包含子模块弹簧加载应用弹簧的配置,大致看起来像以下:从另一个模块

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来运行在batch-jobs.xml中配置的批处理作业。

里面applicationContext.xml我有以下行:

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

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

尽管加载此文件,我得到NoSuchBeanDefinitionException。如果我将batch-jobs.xml中的所有内容复制到applicationContext.xml,它都可以正常工作。

+0

模块1在运行时看到模块2的类资源吗?即是类路径中的batch-jobs.xml文件吗? – Jukka

回答

0

您可以使用maven副本资源插件将资源复制到module1项目(在module1 pom.xml中添加副本资源插件)。假设您修改导入语句<import resource="classpath*: springjobs/batch-jobs.xml" />您可以使用复制资源插件将xml文件从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> 
+0

感谢您的想法,但目录不干净 – MewX

0

当你有多个模块和你的基地网络模块不知道喜欢捆绑插件模块(审计日志记录),你可以用下面的办法。

classpath*:applicationContext-*.xml 

你必须遵循你的Spring配置XML文件(的applicationContext-m1.xml,的applicationContext-m2.xml)的命名约定以加载使用类路径扫描弹簧的配置。

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"/> 
2

当你使用这样

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

它忽略文件无法找到星号(除其他事项外)

请参阅this question的细节

确保您参考其弹性配置XML与领先的斜线,这样的:

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

如果仍然抱怨文件批量jobs.xml未发现请确保您有模块2罐子在你的类路径上(将模块2的依赖添加到模块1)。

相关问题