您的位置:首页 > 移动开发

spring加载jar包中多个xml配置文件 mappingLocations

2014-09-03 16:44 441 查看
在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示:

Java代码

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:beanconfigs/applicationContext_1.xml,
classpath*:beanconfigs/applicationContext_2.xml,
...
</param-value>
</context-param>


这样太复杂了,对于一个大的项目而言,要在这里写入太多的配置,影响美观还害怕引入的xml减少。可以自定义一个applicationContext_all.xml,使用import引入其他配置文件,如下所示:

Java代码

<import resource="beanconfigs/applicationContext_1.xml" />
<import resource="beanconfigs/applicationContext_2.xml" />
...


可以使用通配符设置,如下所示:

Java代码

<import resource="beanconfigs/applicationContext_*.xml" />


这样在spring配置就可以写成如下所示:

Java代码

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext_all.xml
</param-value>
</context-param>


另,见网上资料:http://www.iteye.com/problems/9008

请问Spring如何在jar文件里面按文件夹加载配置文件?

一个Web应用有多个模块(假设有org和auth两个模块), 我希望为每个模块创建一个项目, 在项目中维护模块用到的配置文件. 然后将这些模块分别打包成jar放到web应用的WEB-INF/lib下.

现在用单元测试, 在Web应用中运行单元测试, 如果在Web应用的Build Path/Project中添加模块项目, 单元测试能够成功, 如果使用Build Path/Libraries添加模块jar文件, 运行单元测试失败. Spring中加载配置文件代码如下:

Xml代码

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:/config/hibernate/app/</value>
<value>classpath*:/config/hibernate/framework/</value>
</list>
</property>
...
</bean>


每个jar包里面都有/config/hibernate/framework文件夹

网上找到一个相关的讨论: http://forum.springframework.org/archive/index.php/t-10029.html
好像是说对于directory的加载必须是文件夹必须存在于文件系统中, jar下面的文件夹找不到.不知道这个问题有没有办法解决?

我刚才试了一下, 如果把配置文件改成

Xml代码

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingLocations">
<list>
<value>classpath*:/config/hibernate/framework/*.xml</value>
</list>
</property>
...
</bean>


果然可以了

Spring中如何加载多个配置文件
http://tech.it168.com/jd/2008-04-01/200804011615198.shtml
1.第一种,使用数组

  代码

ApplicationContext contex=new ClassXmlApplicationContext(bew String["a1.xml","a2.xml"]);


  2.第二种,只用通配符

  代码

ApplicationContext contex=new ClassXmlApplicationContext("a*.xml");
  //但此种方法只对文件系统中的xml文件有效,针对jar包中的无效


  3.第三种,引入

  代码

  

ApplicationContext contex=new ClassXmlApplicationContext("a1.xml");   //在a1.xml中   //执行resource路径为相对a1.xml的路径


转:/article/4410796.html


Spring中使用classpath*加载配置文件,jar包中的配置文件不加载问题

jar中配置文件放在“/”(根目录)下时,通过classpath*加载配置文件,jar包中的配置文件不会加载,

这是因为Spring使用classpath加载配置文件时需要借助JDK的ClassLoader.getResources(String name)方法,而该方法有一个局限:当传入的参数为空字符串时,即我们本意是想从根目录获取文件,这时JDK只会返回存在于文件系统中的资源,而在jar包中的资源并不会被返回。
解决方法是将配置文件放在根的下一级目录内,例如/conf/application-context.xml,web.xml中配置为classpath*:conf/**/*application-context.xml。

比如你的abc.jar包中顶层目录包含一个applicationContext-service.xml文件,并且abc.jar包放在WEB-INF/lib目录下

在web.xml中配置如下:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath*:applicationContext.xml,

classpath*:applicationContext-service.xml,

</param-value>

</context-param>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml,
classpath*:applicationContext-service.xml,
</param-value>
</context-param>


这样配置,是可以自动加载的!

转:/article/4696882.html


Spring的 classpath 通配符加载配置文件

classpath:app-Beans.xml
说明:无通配符,必须完全匹配

classpath:App?-Beans.xml
说明:匹配一个字符,例如 App1-Beans.xml 、 App2-Beans.xml

classpath:user/*/Base-Beans.xml
说明:匹配零个或多个字符串(只针对名称,不匹配目录分隔符等),例如:user/a/Base-Beans.xml 、 user/b/Base-Beans.xml ,但是不匹配 user/Base-Beans.xml

classpath:user/**/Base-Beans.xml
说明:匹配路径中的零个或多个目录,例如:user/a/ab/abc/Base-Beans.xml,同时也能匹配 user/Base-Beans.xml

classpath:**/*-Beans.xml
说明:表示在所有的类路径中查找和加载文件名以“-Beans.xml”结尾的配置文件,但重复的文件名只加载其中一个,视加载顺序决定

classpath*:user/**/*-Beans.xml
classpath*:**/*-Beans.xml
说明:“classpath*:”表示加载多个资源文件,即使重名也会被加载,比如app1.jar中有一个config-Beans.xml,app2.jar中也有一个config-Beans.xml,这个时候,两个都会加载。

转:/article/3744858.html

Spring XML文件中导入位于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>

<?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>

<?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文件。

转:/article/4462762.html


【解惑】深入jar包:从jar包中读取资源文件

如果我想在spring的配置文件中引用jar中的hibernate映射xml文件,怎么办?

<property name="mappingResources">

<list>

<value>com/dao/maps/Order.hbm.xml</value>

</list>

</property>

Order.hbm.xml这个文件是放在一个框架的jar中的。

如果我不在自己的项目内相同目录创建Order.hbm.xml这个文件,则系统启动加载该文件时抛出文件不存在的异常。

<property name="mappingJarLocations">

<list>

<value>WEB-INF/lib/test.jar</value>

</list>

</property>

转:/article/3744858.html


Spring XML文件中导入位于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>

<?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>

<?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文件。

我测试:
<!-- 如果资源文件在jar包中: -->

<import resource="classpath*:com/garfield/config/applicationContext-*.xml" />

转:

/article/8312199.html

spring加载hibernate映射文件的几种方式

在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionFactory的所属类LocalSessionFactoryBean的“mappingResources”属性,方式包括(mappingResources,mappingLocations、mappingDirectoryLocations与mappingJarLocations
)定义方法如下:

第一种:
<property name="mappingResources">
<list>
<value>com/w3cs/vlar/hibernate/Person.hbm.xml</value>
<value>com/w3cs/vlar/hibernate/Car.hbm.xml</value>
<value>com/w3cs/vlar/hibernate/Engine.hbm.xml</value>
<value>com/w3cs/vlar/hibernate/Toy.hbm.xml</value>
</list>
</property>
当配置文件变得越来越多,阅读和修改起来也越来越麻烦,而且基于XML的配置也可能带来输入的错误,导致你可能因为一个字符的错误而浪费半天时间去寻找错误。
第二种:
在这种情况下,可以使用LocalSessionFactoryBean的“mappingDirectoryLocations”属性来定义映射文件,只要指出映射文件所在文件夹就可以了,Spring会替你找出该文件夹内所有的映射文件,定义方法如下:
<property name="mappingDirectoryLocations">
<list>
<value>WEB-INF/mappings</value>
</list>
</property>
第三种:
当然,它的属性值也可以通过classpath来指出,这时所指定的是工程的类路径
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/my/package/*.hbm.xml</value>
</list>
</property>
第四种:
<!-- 增加了对大对象字段处理配置Begin -->

<bean id ="oracleLobHandler"

class ="org.springframework.jdbc.support.lob.OracleLobHandler"

lazy-init ="true" >

<property name ="nativeJdbcExtractor" ref ="nativeJdbcExtractor" />

</bean>

<bean id ="nativeJdbcExtractor" class ="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"

lazy-init ="true"/>

<!-- 增加了对大对象字段处理配置End -->
<!-- 定义Hibernatte框架中需要的SesscionFactory对象//-->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<!--增加了对大对象字段处理配置Begin -->

<property name ="lobHandler" ref ="oracleLobHandler"/>

<!--增加了对大对象字段处理配置End -->

<property name="mappingDirectoryLocations">

<list>

<value>classpath:/my/package/login/dao/pojo/</value>

<value>classpath:/my/package/jpf/dao/pojo/</value>

......
</list>

</property>

spring整合hibernate配置文件中的sessionfactory中,配置映射文件有好多种方法:
LocalSessionFactoryBean有好几个属性用来查找hibernate映射文件:mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations。
他们的区别:

mappingResources:指定classpath下具体映射文件名

<property name="mappingResources">

<value>petclinic.hbm.xml</value>

</property>

mappingLocations:可以指定任何文件路径,并且可以指定前缀:classpath、file等

<property name="mappingLocations">

<value>/WEB-INF/petclinic.hbm.xml</value>

</property>
<property name="mappingLocations">

<value>classpath:/com/company/domain/petclinic.hbm.xml</value>

</property>

也可以用通配符指定,'*'指定一个文件(路径)名,'**'指定多个文件(路径)名,例如:

<property name="mappingLocations">

<value>classpath:/com/company/domain/**/maps/*.hbm.xml</value>

</property>

上面的配置是在com/company/domain包下任何maps路径下的hbm.xml文件都被加载为映射文件mappingDirectoryLocations:指定映射的文件路径

mappingJarLocations:指定加载的映射文件在jar文件中

当有mappingLocations存在时,mappingResources中对hibernate映射文件的配置是不加载的,因此,需要把映射文件配置都放到mappingLocations中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: