3.1 環(huán)境與Profile
0x01 原因:
在開(kāi)發(fā)階段泰鸡,某些環(huán)境相關(guān)的配置并不適合直接遷移到生產(chǎn)環(huán)境中伦乔。比如數(shù)據(jù)庫(kù)配置、加密算法以及與外部系統(tǒng)的集成是跨環(huán)境部署。
0x02 Spring提供的解決方案
在3.1版本中漩氨,spring引入了bean profile功能。通過(guò)將可能發(fā)生變化的不同的bean定義到一個(gè)或多個(gè)profile中遗增,當(dāng)其對(duì)應(yīng)的profile處于激活(active)狀態(tài)時(shí),則該bean生效。
0x03 使用方法
1. Java配置
在Java配置中辩涝,可以使用@Profile注解指定某個(gè)bean屬于哪一個(gè)profile葱峡。
例如抡草,對(duì)于數(shù)據(jù)庫(kù)的DataSource的配置:
@Configuration
@Profile("dev")
public class DevelopmentProfileConfig {
@bean(destroyMethod="shutdown")
public DataSource dataSource() {
return new ……
}
}
這個(gè)Profile注解應(yīng)用在了類(lèi)級(jí)別上,表明只有在dev profile激活時(shí)時(shí)蔗坯,該類(lèi)中的bean才會(huì)被創(chuàng)建康震。
同時(shí),我們還可以創(chuàng)建一個(gè)應(yīng)用于生產(chǎn)環(huán)境的配置宾濒。
@Configuration
@Profile("prod")
public class ProductionProfileConfig {
@bean
public DataSource dataSource() {
return new ……
}
}
該類(lèi)中的bean腿短,只有在prod profile激活時(shí)才會(huì)生效。
另外绘梦,在spring 3.1中橘忱,只能在類(lèi)級(jí)別上使用@Profile注解,而從spring 3.2 開(kāi)始卸奉,也可以在方法級(jí)別上使用@Profile注解钝诚。這樣,就可以在同一個(gè)類(lèi)中榄棵,定義屬于不同profile的bean凝颇。
需要注意的是,沒(méi)有指定profile的bean配置秉继,在任何情況下都會(huì)被創(chuàng)建祈噪。
2.在XML中配置
如果項(xiàng)目使用xml進(jìn)行配置的話,也可以在xml中定義bean所屬的profile尚辑。
可以在xml文件最外層的beans標(biāo)簽內(nèi)加入profile="dev"
屬性辑鲤。
也可以在內(nèi)嵌的beans標(biāo)簽中加入profile="dev"
屬性。
例如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd"
profile="dev"> <-----在最外層的beans中定義
<util:list id="trackList">
<value>Track1</value>
<value>Track2</value>
</util:list>
<bean id="compactDisc" class="soundsystem.BlankDisc" c:title="Sgt Peppers" c:artist="The Beatles" c:tracks-ref="trackList">
</bean>
<bean id="cdPlayer" class="soundsystem.CDPlayer" p:cd-ref="compactDisc">
</bean>
</beans>
或者如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<util:list id="trackList">
<value>Track1</value>
<value>Track2</value>
</util:list>
<beans profile="dev"> <-----在嵌套的beans標(biāo)簽中加入杠茬,只在該標(biāo)簽內(nèi)部生效
<bean id="compactDisc" class="soundsystem.BlankDisc" c:title="Sgt Peppers" c:artist="The Beatles" c:tracks-ref="trackList">
</bean>
</beans>
<bean id="cdPlayer" class="soundsystem.CDPlayer" p:cd-ref="compactDisc">
</bean>
</beans>
0x04 激活對(duì)應(yīng)的profile
前面說(shuō)了在指定profile定義bean月褥,那么該如何激活對(duì)應(yīng)的profile,使bean生效呢瓢喉?
spring在確定哪個(gè)profile處于激活狀態(tài)時(shí)宁赤,依賴(lài)兩個(gè)屬性:
- spring.profiles.active
- spring.profiles.default
如果設(shè)置了spring.profiles.active
屬性的話,則使用它的值栓票,否則就會(huì)查找spring.profiles.default
屬性的值决左。如果這兩個(gè)值都沒(méi)有設(shè)置,則只會(huì)創(chuàng)建沒(méi)有定義在profile中的bean走贪。
這兩個(gè)屬性的設(shè)置方法:
- 作為DispatcherServlet的初始化參數(shù)
- 作為Web應(yīng)用的上下文參數(shù)
- 作為JNDI條目
- 作為環(huán)境變量
- 作為JVM的系統(tǒng)屬性
- 在集成測(cè)試類(lèi)上佛猛,使用@ActiveProfiles注解設(shè)置
在繼承測(cè)試類(lèi)中,可以使用如下代碼激活dev profile:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {CDPlayerConfigDev.class})
@ActiveProfiles("dev") <-----在這里使用注解設(shè)置
public class CDPlayerTest {
}
感謝您的閱讀坠狡!
本賬號(hào)已經(jīng)不再更新继找,更多文章請(qǐng)移步我的個(gè)人博客https://www.zacharyjia.me