天天看點

Spring中加載propertities配置檔案

0、Spring加載classpath路徑下的propertities配置檔案

<!-- 使用spring自帶的占位符替換功能 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 允許JVM參數覆寫 -->
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<!-- 忽略沒有找到的資源檔案 -->
		<property name="ignoreResourceNotFound" value="true" />
		<!-- 配置資源檔案 -->
		<property name="locations">
			<list>
				<value>classpath:baseInfo.properties</value>
				<value>classpath:db.properties</value>
			</list>
		</property>
	</bean>
           

db.properties的配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student
jdbc.username=root
jdbc.password=1234
           

baseInfo.properties的配置

my.jspPath=/jsp
my.htmlPath=/html
           

1、用于資料庫連接配接池的配置   

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 配置資料源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="5" />
	</bean>


</beans>
           

2、在Java程式中使用propertities中的配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class ProService {
	@Value("${my.jspPath}")
	private String jspPath;
	@Value("${my.htmlPath}")
	private String htmlPath;

	public void test() {
		System.out.println("jspPath:" + this.jspPath + ", htmlPath:"
				+ this.htmlPath);
	}
}
           

注意baseInfo.properties和ProService.java的代碼實作

Spring中加載propertities配置檔案

3、自定義實作propertities配置檔案的加載

      使用上面的方法,但是這有一個問題,我每用一次配置檔案中的值,就要聲明一個局部變量。有沒有用代碼的方式,直接讀取配置檔案中的值。答案就是重寫PropertyPlaceholderConfigurer。

      3.1 重寫PropertyPlaceholderConfigurer

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {

	private static Map<String, String> propertyMap;

	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess,
			Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		propertyMap = new HashMap<String, String>();
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			propertyMap.put(keyStr, value);
		}
	}

	// static method for accessing context properties
	public static Object getProperty(String name) {
		return propertyMap.get(name);
	}
}
           

       3.2 在Spring的配置檔案裡面, 用上面的類,代替PropertyPlaceholderConfigurer

<!-- 使用spring自帶的占位符替換功能 -->
	<bean class="com.utils.PropertyPlaceholder">
		<!-- 允許JVM參數覆寫 -->
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<!-- 忽略沒有找到的資源檔案 -->
		<property name="ignoreResourceNotFound" value="true" />
		<!-- 配置資源檔案 -->
		<property name="locations">
			<list>
				<value>classpath:baseInfo.properties</value>
				<value>classpath:db.properties</value>
			</list>
		</property>
	</bean>
           

        3.2 Java程式中的用法

import org.springframework.stereotype.Service;

import com.utils.PropertyPlaceholder;

@Service
public class MyProService {

	public void test() {
		System.out.println("jspPath:"
				+ PropertyPlaceholder.getProperty("my.jspPath") + ", htmlPath:"
				+ PropertyPlaceholder.getProperty("my.htmlPath"));
	}
}
           

        具體的代碼實作,就是PropertyPlaceholder.getProperty("my.jspPath").

4、測試代碼

public class App {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath:spring/bean-*.xml");
		System.out.println(context);

		// 擷取并測試
		ProService proService = context.getBean("proService", ProService.class);
		System.out.println(proService);
		proService.test();

		// 擷取并測試
		MyProService myproService = context.getBean("myProService",
				MyProService.class);
		System.out.println(myproService);
		myproService.test();
		// 擷取并測試
		DataSource dataSource = context.getBean("dataSource", DataSource.class);
		System.out.println(dataSource);
	}
}
           

5、源碼下載下傳

繼續閱讀