我們的項(xiàng)目通常會(huì)發(fā)布在不同的系統(tǒng)富弦、不同的受眾環(huán)境提供外部或內(nèi)部訪問(wèn)沟娱,如:內(nèi)測(cè)、公測(cè)腕柜、生產(chǎn)環(huán)境济似、基于 WIN/LINUX等。發(fā)布至不同的環(huán)境盏缤,我們通常會(huì)配置不同的數(shù)據(jù)庫(kù)賬戶密碼砰蠢、連接數(shù)、生成測(cè)試數(shù)據(jù)等唉铜。如果沒(méi)有一套固定的機(jī)制自動(dòng)配置台舱,而在發(fā)布時(shí)手工修改,犯錯(cuò)率不僅很大潭流,同時(shí)效率也很低竞惋。
程序員的優(yōu)點(diǎn)是懶柜去,缺點(diǎn)是不夠懶。O(∩_∩)O哈哈哈~
以下分為 Spring App 和 Spring MVC 兩種場(chǎng)景介紹拆宛,目的是打包前改一個(gè)變量嗓奢,它能自動(dòng)讀取對(duì)應(yīng)配置。如 env = prod
先創(chuàng)建一個(gè) MyProperties 用于裝載屬性 key/value:
package com.caobug.demo.springenv;
/**
* 屬性
* <p>
* Created by caobug on 16/6/26.
*
* @author caobug
* @since 1.7
*/
public class MyProperties {
private String ipAddress;
private String username;
private String password;
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "MyProperties{" +
"ipAddress='" + ipAddress + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
再創(chuàng)建配置文件spring-config.xml
用于關(guān)聯(lián)不同環(huán)境的配置浑厚。這里可以靈活變通為讀取某個(gè)配置文件或直接注入蔓罚。
<?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.xsd">
<beans profile="developer">
<bean id="myProperties" class="com.caobug.demo.springenv.MyProperties">
<property name="username" value="a"/>
<property name="ipAddress" value="192.168.1.2"/>
<property name="password" value="123456"/>
</bean>
</beans>
<beans profile="production">
<bean id="myProperties" class="com.caobug.demo.springenv.MyProperties">
<property name="username" value="b"/>
<property name="ipAddress" value="192.168.1.3"/>
<property name="password" value="654321"/>
</bean>
</beans>
</beans>
以上代碼和平時(shí)的依賴注入沒(méi)什么差別,僅僅是多了屬性 profile="developer"
瞻颂,他用于告訴 Spring 這需要特定環(huán)境才生效豺谈。這里還有一種寫(xiě)法是把以下片段:
<beans xmlns="http://www.springframework.org/schema/beans"
替換為:
<beans profile="developer" xmlns="http://www.springframework.org/schema/beans"
這樣做之后將需要?jiǎng)?chuàng)建不同的XML文件,環(huán)境一多就不好維護(hù)了贡这。
我們用JUNIT驗(yàn)證下我們的做法:
package com.caobug.demo.springenv;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by caobug on 16/6/26.
*
* @author caobug
* @since 1.7
*/
public class MyPropertiesTest {
@Test(expected = NoSuchBeanDefinitionException.class)
public void notFound() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
// ctx.getEnvironment().setActiveProfiles("developer");
// ctx.refresh();
MyProperties myProperties = ctx.getBean("myProperties", MyProperties.class);
System.out.println("developer myProperties = " + myProperties);
}
@Test
public void developer() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
ctx.getEnvironment().setActiveProfiles("developer");
ctx.refresh();
MyProperties myProperties = ctx.getBean("myProperties", MyProperties.class);
System.out.println("developer myProperties = " + myProperties);
}
@Test
public void production() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
ctx.getEnvironment().setActiveProfiles("production");
ctx.refresh();
MyProperties myProperties = ctx.getBean("myProperties", MyProperties.class);
System.out.println("production myProperties = " + myProperties);
}
}
我們需要特別注意的是茬末,如果我們沒(méi)有為ctx
設(shè)定對(duì)應(yīng)的環(huán)境(developer or production),或者有設(shè)定但未調(diào)用ctx.refresh();
時(shí)盖矫,若嘗試獲取bean
將會(huì)拋出NoSuchBeanDefinitionException
異常丽惭,請(qǐng)參考以上notFound()
測(cè)試方法。
樓上說(shuō):想RUN就RUN辈双!不管了责掏,我們?cè)賮?lái)看下 Spring MVC 中如何配置: