When using spring to load a configuration file in a jar package, wildcard characters are not supported and need to be introduced as follows:
Java code
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
Classpath*:beanconfigs/applicationcontext_1.xml,
Classpath*:beanconfigs/applicationcontext_2.xml,
...
</param-value>
</context-param>
This is too complicated, for a large project, to write too many configurations here, the impact of aesthetics is also afraid of the introduction of XML reduction. You can customize a applicationcontext_all.xml, using import to introduce additional configuration files, as follows:
Java code
<import resource= "Beanconfigs/applicationcontext_1.xml"/>
<import resource= "Beanconfigs/applicationcontext_2.xml"/>
...
You can use the wildcard settings as follows:
Java code
<import resource= "Beanconfigs/applicationcontext_*.xml"/>
This can be written in the spring configuration as follows:
Java code
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
Classpath*:applicationcontext_all.xml
</param-value>
</context-param>
Also, see online information: http://www.iteye.com/problems/9008
How does spring load the config file in the jar file by folder?
A Web application has multiple modules (assuming org and auth two modules), and I want to create a project for each module that maintains the configuration file used by the module in the project. These modules are then packaged separately into jars and placed under the web-inf/lib of the Web application.
Now that you are running unit tests in a web app with unit tests, if you add a module project to the Web app's build path/project, the unit test succeeds, and if you add a module jar file using Build path/libraries, the run unit test fails. The load configuration file code in spring is as follows:
XML code
<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>
There are/config/hibernate/framework folders inside each jar package.
Find a related discussion on the Internet: http://forum.springframework.org/archive/index.php/t-10029.html
It seems that the load for directory must be a folder that must exist in the file system, and the folder underneath the jar cannot be found. Do you have any idea how to solve this problem?
I just tried it, if I change the configuration file to
XML code
<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>
Yes, sure.
How to load multiple configuration files in spring
Http://tech.it168.com/jd/2008-04-01/200804011615198.shtml
1. First, use arrays
Code
ApplicationContext contex=new classxmlapplicationcontext (bew string["A1.xml", "A2.xml"]);
2. The second type, using only wildcard characters
Code
ApplicationContext contex=new classxmlapplicationcontext ("A*.xml");
However, this method is only valid for XML files in the file system and is not valid for the JAR package
3. Thirdly, the introduction of
Code
ApplicationContext contex=new classxmlapplicationcontext ("A1.xml"); In A1.xml//execute path of resource path to relative A1.xml
Ext.: http://webwork.iteye.com/blog/519844
The configuration file in the jar package does not load the problem with the classpath* load configuration file in spring
When the configuration file in the jar is placed under the "/" (root directory), the configuration file is loaded through classpath* and the configuration file in the jar package is not loaded.
This is because spring uses CLASSPATH to load the configuration file with the Classloader.getresources (String name) method of the JDK, which has a limitation: when the passed argument is an empty string, the That is, we intend to get the file from the root directory, and the JDK will only return the resources that exist in the file system, and the resources in the jar package will not be returned.
The workaround is to place the configuration file in the lower-level directory of the root, such as/conf/application-context.xml,web.xml, configured as Classpath*:conf/**/*application-context.xml.
For example, your Abc.jar package contains a applicationcontext-service.xml file in the top-level directory, and Abc.ja The R package is placed in the web-inf/lib directory
The configuration in Web. XML is as follows:
- <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>
This configuration, it can be loaded automatically!
Ext.: http://www.cnblogs.com/taven/archive/2012/10/24/2737556.html
spring classpath with multibyte configuration file
Classpath:app-beans.xml
Description: No wildcard characters, must match exactly
Classpath:app?-beans.xml
Description: Matches a character, such as App1-beans.xml, App2-beans.xml
Classpath:user/*/base-beans.xml
Description: Matches 0 or more strings (names only, does not match directory separators, etc.), for example: User/a/base-beans.xml, User/b/base-beans.xml, but does not match user/base-beans.xml
Classpath:user/**/base-beans.xml
Description: Matches 0 or more directories in the path, for example: User/a/ab/abc/base-beans.xml, and can also match user/base-beans.xml
Classpath:**/*-beans.xml
Description: Represents a configuration file that finds and loads a file name ending with "-beans.xml" in all classpath, but only one of the duplicate filenames is loaded, depending on the load order
Classpath*:user/**/*-beans.xml
classpath*:* */*-beans.xml
Description: "classpath*:" means loading multiple resource files, even if the same name will be loaded, such as App1.jar in a config-beans.xml,app2.jar there is also a config-beans.xml, this time, two will be loaded.
Ext.: http://josh-persistence.iteye.com/blog/1940297
Import XML files in the jar package in the Spring XML file
In a spring-based project, we all know that the core context configuration file is Applicationcontext.xml or {projectname}-serverlet.xml, if we want to split the configuration file, Then just import a few other profiles in the core configuration file.
For example: If the current project name is Cms-validator, we assume that the core context configuration file for spring now is:
Cms-validator-servlet.xml. We can import additional configuration files in this configuration file:
- <?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>
Obviously, the above scenario is that these configuration files and the current configuration file are in the same directory of project, so if we want to import the configuration file in the jar package, how to handle it? Assuming that these configuration files are in Validator-rest-1.0.jar, you can use the
- <?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>
Just use the * number to complete the import file from the jar package.
Ext.: http://hxraid.iteye.com/blog/483115
"doubts" dive into the jar package: Read the resource file from the jar package
What if I want to refer to the Hibernate mapping XML file in the jar in the spring configuration file?
<property name= "Mappingresources" >
<list>
<value>com/dao/maps/Order.hbm.xml</value>
</list>
</property>
Order.hbm.xml This file is placed in a jar of a framework.
If I do not create Order.hbm.xml this file in the same directory in my project, the system initiates an exception that does not exist when the file is loaded.
<property name= "Mappingjarlocations" >
<list>
<value>WEB-INF/lib/test.jar</value>
</list>
</property>
Ext.: http://josh-persistence.iteye.com/blog/1940297
Import XML files in the jar package in the Spring XML file
In a spring-based project, we all know that the core context configuration file is Applicationcontext.xml or {projectname}-serverlet.xml, if we want to split the configuration file, Then just import a few other profiles in the core configuration file.
For example: If the current project name is Cms-validator, we assume that the core context configuration file for spring now is:
Cms-validator-servlet.xml. We can import additional configuration files in this configuration file:
- <?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>
Obviously, the above scenario is that these configuration files and the current configuration file are in the same directory of project, so if we want to import the configuration file in the jar package, how to handle it? Assuming that these configuration files are in Validator-rest-1.0.jar, you can use the
- <?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>
Just use the * number to complete the import file from the jar package.
I test:
<!--If the resource file is in the jar package:--
<import resource= "Classpath*:com/garfield/config/applicationcontext-*.xml"/>
Turn:
http://blog.csdn.net/feihong247/article/details/7831064
Spring loading Hibernate mapping files in several ways
The mapping file is configured in spring Applicationcontext.xml, typically in the bean instance of <sessionFactory>, if the mapping file is configured to be small, You can use the "mappingresources" property of the Sessionfactory class Localsessionfactorybean, including (Mappingresources,mappinglocations, Mappingdirectorylocations and Mappingjarlocations) define the following methods:
The first type:
<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>
As configuration files become more and more cumbersome to read and modify, and XML-based configuration can lead to input errors, you may be wasting half a day searching for errors because of a single character error.
The second type:
In this case, you can use the Localsessionfactorybean "Mappingdirectorylocations" property to define the mapping file, as long as you indicate the folder where the map file is located, Spring will find out all the mapping files in that folder for you, as defined by the following:
<property name= "Mappingdirectorylocations" >
<list>
<value>WEB-INF/mappings</value>
</list>
</property>
The third type:
Of course, its property values can also be indicated by Classpath, at which point the class path of the project is specified
<property name= "Mappingdirectorylocations" >
<list>
<value>classpath:/my/package/*.hbm.xml</value>
</list>
</property>
The fourth type:
<!--increased processing configuration of large object fields 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"/>
<!--added to large object field processing configuration End--
<!--define the Sesscionfactory objects required in the Hibernatte framework//-->
<bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<!--increased processing configuration of large object fields Begin--
<property name = "Lobhandler" ref = "Oraclelobhandler"/>
<!--added to large object field processing configuration End--
<property name= "Mappingdirectorylocations" >
<list>
<value>classpath:/my/package/login/dao/pojo/</value>
<value>classpath:/my/package/jpf/dao/pojo/</value>
......
</list>
</property>
There are several ways to configure a mapping file in the sessionfactory in the Spring integration Hibernate configuration file:
Localsessionfactorybean has several properties to look up hibernate mapping files: mappingresources, Mappinglocations, Mappingdirectorylocations and Mappingjarlocations.
The difference between them:
Mappingresources: Specify the specific mapping file name under Classpath
<property name= "Mappingresources" >
<value>petclinic.hbm.xml</value>
</property>
Mappinglocations: You can specify any file path, and you can specify a prefix: classpath, file, and so on
<property name= "Mappinglocations" >
<value>/WEB-INF/petclinic.hbm.xml</value>
</property>
<property name= "Mappinglocations" >
<value>classpath:/com/company/domain/petclinic.hbm.xml</value>
</property>
You can also specify by using a wildcard character, ' * ' to specify a file (path) name, ' * * ' to specify multiple file (path) names, for example:
<property name= "Mappinglocations" >
<value>classpath:/com/company/domain/**/maps/*.hbm.xml</value>
</property>
The above configuration is the Hbm.xml file under any maps path under the Com/company/domain package is loaded as a mapping file Mappingdirectorylocations: Specifies the mapped file path
Mappingjarlocations: Specifies that the loaded mapping file is in the jar file
When a mappinglocations exists, the configuration of the Hibernate mapping file in Mappingresources is not loaded, so the mapping file configuration needs to be placed in mappinglocations
Spring loads multiple configuration files in a jar package [reprint]