Properties files and XML file configuration issues in spring configuration

Source: Internet
Author: User

Spring facilitates our projects to build quickly, powerful, and naturally it will be a complex system!

Here is the issue of configuration file properties management.

Some don't involve code logic, just configuration data, Can be placed in the Xxxx.properties file, the project function complex, often properties file a lot, at this time, it is easier to confuse people, some properties of the file content is always loaded, the application starts, the error continues to burst, saying that a parameter failed to load, this is what reason?

In fact, this is the spring configuration, org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer This bean will only be loaded once. In spring's XML configuration file, there are multiple occurrences of this bean configuration, and when spring loads, it will only load the first one, and the latter will not be loaded.

How to solve it? In fact, it is very simple, such as one of my applications, there are internal.properties and jdbc.properties two files. This can be resolved as follows:

1<bean id="Configrealm" class="Org.springframework.beans.factory.config.PropertiesFactoryBean">2<property name="Locations">3<list>4                 <value>classpath:conf/internal.properties</value>5                 <value>classpath:conf/jdbc.properties</value>6</list>7</property>8</bean>9<bean id="Propertyconfigurer" class="Org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">Ten<property name="Properties" ref="Configrealm"/> One</bean>

If you have more than one, you can add it in the list similar to the red part!

Here, say a little more, XML configuration problem, of course, not a functional problem, but the habit of the problem!

Large spring-based projects, often have many XML configuration files, such as DAO, such as the configuration of permissions Shiro, such as Redis configuration and so on, in Web. XML, the Context-param field can be configured as follows:

1    <context-param>2         <param-name>contextConfigLocation</param-name>3         <param-value>classpath:conf/spring-dao.xml,classpath:conf/spring-servlet.xml</param-value>4      </context-param>

However, a better approach would be to put some of the application module's XML configuration in an XML file, such as Applicationcontext.xml, via import resource. For example, the contents of my application Applicationcontext.xml file:

1<?xml version="1.0"encoding="UTF-8"?>2<beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3xmlns:context="Http://www.springframework.org/schema/context"4xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5xsi:schemalocation="6http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd7http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd">8     9<bean id="Configrealm" class="Org.springframework.beans.factory.config.PropertiesFactoryBean">Ten<property name="Locations"> One<list> A<value>classpath:conf/Internal.properties</value> -<value>classpath:conf/jdbc.properties</value> -</list> the</property> -</bean> -<bean id="Propertyconfigurer" class="Org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> -<property name="Properties" ref="Configrealm"/> +</bean> -  +<import resource="Applicationcontext-springdata.xml"/> A<import resource="Applicationcontext-security.xml"/> at<import resource="Applicationcontext-ehcache.xml"/> -<import resource="Applicationcontext-task.xml"/> -<import resource="Applicationcontext-external.xml"/> -<import resource="Applicationcontext-message.xml"/>-- -</beans>

This applicationcontext.xml can be loaded in the Context-param configuration section of Web. XML, as follows:

1     <context-param>2         <param-name>contextConfigLocation</param-name>3         <param-value>classpath:conf/applicationContext.xml</param-value>4     </context-param>

This way, it will make the configuration file structure of the project very clear!

Properties files and XML file configuration issues in spring configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.