Spring 如何引用 jar中的XML(转)

在基于Spring构建的项目中,我们都知道核心的Context配置文件是ApplicationContext.xml或者{projectName}-serverlet.xml, 如果我们想拆分配置文件,那么只需在核心的配置文件中import其它的几个配置文件即可。

举例说明:如果当前的项目名称为cms-validator,我们假定现在Spring的核心的Context配置文件是:

cms-validator-servlet.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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       ">
   
    <import resource="cms-validator-common.xml"/>
    <import resource="cms-validator-hibernate.xml"/>
    <import resource="cms-validator-service.xml"/>
    <import resource="cms-validator-dao.xml"/>

    
</beans>

很显然,上面的方案是这些配置文件和当前的配置文件都在一个project的同一个目录中,那么如果我们想导入的配置文件在jar包,怎么处理?假设这几个配置文件在validator-rest-1.0.jar中,则可以用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       ">
   

    <import resource="lib/validator-rest-1.0.jar*/cms-validator-common.xml"/>
    <import resource="lib/validator-rest-1.0.jar*/cms-validator-hibernate.xml"/>
    <import resource="lib/validator-rest-1.0.jar*/cms-validator-service.xml"/>
    <import resource="lib/validator-rest-1.0.jar*/cms-validator-dao.xml"/>
    
</beans>

只要用*号,就可以完成从jar包中import文件。 

猜你喜欢

转载自frank1998819.iteye.com/blog/1940804