spring加载jar包中多个配置文件(转)
转自:http://evan0625.iteye.com/blog/1598366
在使用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的路径
转:http://webwork.iteye.com/blog/519844
Spring中使用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>
这样配置,是可以自动加载的!
转:http://www.cnblogs.com/taven/archive/2012/10/24/2737556.html
Spring的 classpath 通配符加载配置文件
classpath:app-Beans.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包:从jar包中读取资源文件
<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>
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" />
转:
http://blog.csdn.net/feihong247/article/details/7831064
spring加载hibernate映射文件的几种方式
第一种:
<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中
----------------------------------美丽的分割线--------------------------------------------------------
好吧,我承认,上面这些我只是收集,还未全部验证。
转载于:https://www.cnblogs.com/GarfieldTom/p/3723915.html
spring加载jar包中多个配置文件(转)相关推荐
- spring加载jar包中多个配置文件
转自:http://www.cnblogs.com/GarfieldTom/p/3723915.html <import resource="classpath*:applicatio ...
- java实现动态加载jar包中的class(破坏双亲委派来实现)
背景 最近在项目中在使用Drools来做规则,drools确实功能比较强大,但是学习成本也比较高,语法比较繁琐.我们项目是做的动态规则,用户可以通过上传jar包的方式来动态的再我们ruleengine ...
- java 中 Native.loadLibrary 不能加载 jar 包中库文件的解决方案
比如拿 jna 加载海康测速库文件为例: windows环境: (HCNetSDK) Native.loadLibrary("E:\\xxx\\HCNetSDK.dll", HCN ...
- IntelliJ IDEA中无法加载jar包导致出现“cannot resolve symbol...”问题的解决
IntelliJ IDEA中无法加载jar包导致出现"cannot resolve symbol..."问题的解决 之前也经常碰到这样的错误,通过reimport.清缓存等方法都可 ...
- Spring Boot 引入jar 包中的 controller
[问题] Spring Boot 引入jar 包中的 controller [解决] 默认情况下 Spring Boot 在启动时, 只会扫描启动类对应的包以及子包进行装配, 因此jar包中的cont ...
- 动态加载jar包(一)
一.编写被调用的类 package com.qunar.helloworld;public class HelloWorld {public String sayHello(){return (&qu ...
- 使用反射获得jar包中的类、方法、参数、返回值类型,然后动态加载jar包运行方法
其实我的需求是在系统中 测试用户导入的jar包,所有网上找了很多解决方案,踩过很多坑,再加上以前搞c++的,对java不是很熟, 这里特意记录下,算是基本满足需求了 使用反射获得jar包中的类.方法. ...
- java动态打jar包_java动态加载jar包
目录结构如下. ├── main │ ├── java │ │ └── lu │ │ └── tool │ │ └── jar │ │ ├── InterfaceR ...
- java程序动态加载jar包,并调用其中的方法
再编写Java应用程序的时候我们通常需要动态的加载jar,具体的代码如下: demo1.jar中的invoke方法: package com.amx.test; public class Test ...
最新文章
- 【Android 插件化】Hook 插件化框架 ( 通过反射获取 “插件包“ 中的 Element[] dexElements )
- 图像分割 2020 最新进展
- Linux学习笔记-文件权限与路径
- c语言switch编写计算器,超级新手,用switch写了个计算器程序,求指导
- 如何修改浏览器服务器时间格式,浏览器模式怎么改模式
- java java 大端_Java 大小端转换
- 第三方app_为什么第三方APP不能下载呢?
- 牛客挑战赛47 A 一道GCD问题
- c语言指针++_C ++此指针| 查找输出程序| 套装3
- linux外接NetApp存储,netapp linux iscsi 实现
- python 图片数据增强_数据增强之批量修改图像尺寸大小(附Python实现)
- 20145209刘一阳《JAVA程序设计》第1周学习总结
- Linux应用总结:自动删除n天前日志
- Android UI开发第三十二篇——Creating a Navigation Drawer
- C++ 自旋锁简单实现
- 复习-arrary和arraylist的对比以及arraylist的遍历中删除的原理
- python爬大学生就业信息报告_Python语言爬虫——Python 岗位分析报告
- 网络工程师——交换技术(线路交换、分组交换技术、帧中继交换、信元交换技术)
- 学习 MySQL 需要知道的 28 个小技巧
- cookie的工作原理、cookie的重要性