轉自: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文件。
<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構建的項目中,我們都知道核心的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中
----------------------------------美麗的分割線--------------------------------------------------------
好吧,我承認,上面這些我只是收集,還未全部驗證。