spring配置xml文件_XML配置文件中的Spring配置文件
spring配置xml文件
我的上一個(gè)博客非常簡(jiǎn)單,因?yàn)樗w了我從Spring 3.0.x到Spring 3.1.x的輕松升級(jí),最后我提到可以將Spring模式升級(jí)到3.1,以利用Spring的最新功能。 在今天的博客中,我將介紹這些功能中最酷的功能之一:Spring配置文件。 但是,在談?wù)撊绾螌?shí)現(xiàn)Spring配置文件之前,我認(rèn)為探索它們正在解決的問題是一個(gè)好主意,這是需要為不同的環(huán)境創(chuàng)建不同的Spring配置。 之所以會(huì)出現(xiàn)這種情況,通常是因?yàn)槟膽?yīng)用程序在其開發(fā)生命周期中需要連接到多個(gè)類似的外部資源,并且連接頻率通常更高,并且這些“外部資源”通常不是數(shù)據(jù)庫,盡管它們可以是JMS隊(duì)列,Web服務(wù),遠(yuǎn)程EJB等。您的應(yīng)用程序在運(yùn)行之前必須工作的環(huán)境數(shù)量通常取決于幾件事,包括您組織的業(yè)務(wù)流程,應(yīng)用程序的規(guī)模以及它的“重要性”(即,如果您正在編寫稅收表)系統(tǒng)為您的國家/地區(qū)提供稅收服務(wù),則測(cè)試過程可能比為本地商店編寫電子商務(wù)應(yīng)用程序時(shí)更為嚴(yán)格。 為使您有所了解,以下是想到的所有不同環(huán)境的快速列表(可能不完整):
這不是一個(gè)新問題,通常可以通過為每個(gè)環(huán)境創(chuàng)建一組Spring XML和屬性文件來解決。 XML文件通常包含一個(gè)導(dǎo)入其他環(huán)境特定文件的主文件。 然后在編譯時(shí)將它們耦合在一起以創(chuàng)建不同的WAR或EAR文件。 這種方法已經(jīng)使用了多年,但確實(shí)存在一些問題:
Spring bean配置中的差異通常可以分為兩部分。 首先,存在特定于環(huán)境的屬性,例如URL和數(shù)據(jù)庫名稱。 通常使用PropertyPlaceholderConfigurer類和相關(guān)的$ {}標(biāo)記將它們注入到Spring XML文件中。
<bean id='propertyConfigurer' class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'><property name='locations'><list><value>db.properties</value></list></property></bean>其次,有特定于環(huán)境的bean類,例如數(shù)據(jù)源,它們通常根據(jù)您連接數(shù)據(jù)庫的方式而有所不同。
例如,在開發(fā)中,您可能具有:
<bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'><property name='driverClassName'><value>${database.driver}</value></property><property name='url'><value>${database.uri}</value></property><property name='username'><value>${database.user}</value></property><property name='password'><value>${database.password}</value></property></bean>…無論是測(cè)試還是現(xiàn)場(chǎng)直播,您只要寫:
<jee:jndi-lookup id='dataSource' jndi-name='jdbc/LiveDataSource'/>Spring準(zhǔn)則指出,僅應(yīng)在上面的第二個(gè)示例中使用Spring概要文件:特定于Bean的類,并且您應(yīng)繼續(xù)使用PropertyPlaceholderConfigurer初始化簡(jiǎn)單的Bean屬性; 但是,您可能希望使用Spring配置文件將特定于環(huán)境的PropertyPlaceholderConfigurer注入到Spring上下文中。
話雖如此,我將在示例代碼中打破這一約定,因?yàn)槲蚁胗米詈?jiǎn)單的代碼來演示Spring配置文件的功能。
Spring配置文件和XML配置
在XML配置方面,Spring 3.1在spring-beans模式的bean元素中引入了新的profile屬性:
在不同環(huán)境中啟用和禁用配置文件時(shí),此配置文件屬性充當(dāng)開關(guān)。
為了進(jìn)一步解釋所有這些,我將使用一個(gè)簡(jiǎn)單的想法,即您的應(yīng)用程序需要加載一個(gè)人員類,并且該人員類包含不同的屬性,具體取決于您的程序在其上運(yùn)行的環(huán)境。
Person類非常簡(jiǎn)單,看起來像這樣:
public class Person {private final String firstName;private final String lastName;private final int age;public Person(String firstName, String lastName, int age) {this.firstName = firstName;this.lastName = lastName;this.age = age;}public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public int getAge() {return age;}}…并在以下XML配置文件中定義:
<?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'profile='test1'><bean id='employee' class='profiles.Person'><constructor-arg value='John' /><constructor-arg value='Smith' /><constructor-arg value='89' /></bean> </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'profile='test2'><bean id='employee' class='profiles.Person'><constructor-arg value='Fred' /><constructor-arg value='ButterWorth' /><constructor-arg value='23' /></bean> </beans>…分別稱為test-1-profile.xml和test-2-profile.xml (請(qǐng)記住這些名稱,它們?cè)谝院蠛苤匾?#xff09;。 如您所見,配置的唯一區(qū)別是名字,姓氏和年齡屬性。
不幸的是,僅僅定義個(gè)人資料還不夠,您必須告訴Spring您正在加載哪個(gè)個(gè)人資料。 這意味著遵循舊的“標(biāo)準(zhǔn)”代碼現(xiàn)在將失敗:
@Test(expected = NoSuchBeanDefinitionException.class)public void testProfileNotActive() {// Ensure that properties from other tests aren't setSystem.setProperty('spring.profiles.active', '');ApplicationContext ctx = new ClassPathXmlApplicationContext('test-1-profile.xml');Person person = ctx.getBean(Person.class);String firstName = person.getFirstName();System.out.println(firstName);}幸運(yùn)的是,有幾種選擇配置文件的方法,在我看來,最有用的方法是使用“ spring.profiles.active”系統(tǒng)屬性。 例如,以下測(cè)試現(xiàn)在將通過:
System.setProperty('spring.profiles.active', 'test1');ApplicationContext ctx = new ClassPathXmlApplicationContext('test-1-profile.xml');Person person = ctx.getBean(Person.class);String firstName = person.getFirstName();assertEquals('John', firstName);顯然,您不想像我上面那樣對(duì)代碼進(jìn)行硬編碼,最佳實(shí)踐通常意味著將系統(tǒng)屬性定義與應(yīng)用程序分開。 這使您可以選擇使用簡(jiǎn)單的命令行參數(shù),例如:
-Dspring.profiles.active='test1'…或通過添加
# Setting a property value spring.profiles.active=test1Tomcat的catalina.properties
因此,僅此而已:您可以使用bean元素配置文件屬性創(chuàng)建Spring XML配置文件,并通過將spring.profiles.active系統(tǒng)屬性設(shè)置為配置文件的名稱來打開要使用的配置文件 。
獲得一些額外的靈活性
但是,這還不是故事的結(jié)局,因?yàn)镾pring的Guy已添加了許多以編程方式加載和啟用配置文件的方式-如果您愿意的話。
@Testpublic void testProfileActive() {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext('test-1-profile.xml');ConfigurableEnvironment env = ctx.getEnvironment();env.setActiveProfiles('test1');ctx.refresh();Person person = ctx.getBean('employee', Person.class);String firstName = person.getFirstName();assertEquals('John', firstName);}在上面的代碼中,我使用了新的ConfigurableEnvironment類來激活“ test1”配置文件。
@Testpublic void testProfileActiveUsingGenericXmlApplicationContextMultipleFilesSelectTest1() {GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();ConfigurableEnvironment env = ctx.getEnvironment();env.setActiveProfiles('test1');ctx.load('*-profile.xml');ctx.refresh();Person person = ctx.getBean('employee', Person.class);String firstName = person.getFirstName();assertEquals('John', firstName);}但是,Spring專家們建議您使用GenericApplicationContext類而不是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,因?yàn)檫@樣可以提供更大的靈活性。 例如,在上面的代碼中,我已經(jīng)使用GenericApplicationContext的load(...)方法使用通配符來加載許多配置文件:
ctx.load('*-profile.xml');還記得以前的文件名嗎? 這將同時(shí)加載test-1-profile.xml和test-2-profile.xml 。
配置文件還具有額外的靈活性,可讓您一次激活多個(gè)。 如果看下面的代碼,您會(huì)看到我正在激活我的test1和test2配置文件:
@Testpublic void testMultipleProfilesActive() {GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();ConfigurableEnvironment env = ctx.getEnvironment();env.setActiveProfiles('test1', 'test2');ctx.load('*-profile.xml');ctx.refresh();Person person = ctx.getBean('employee', Person.class);String firstName = person.getFirstName();assertEquals('Fred', firstName);}請(qǐng)注意,在本示例中,我有兩個(gè)ID為“ employee”的bean,并且無法分辨哪個(gè)是有效的,并且應(yīng)該優(yōu)先。 從我的測(cè)試中,我猜想讀取的第二個(gè)將覆蓋或屏蔽對(duì)第一個(gè)的訪問。 沒關(guān)系,因?yàn)槟粦?yīng)該擁有多個(gè)具有相同名稱的bean –激活多個(gè)配置文件時(shí)需要提防。
最后,可以做的更好的簡(jiǎn)化之一是使用嵌套的<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' xmlns:context='http://www.springframework.org/schema/context'xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd'><beans profile='test1'><bean id='employee1' class='profiles.Person'><constructor-arg value='John' /><constructor-arg value='Smith' /><constructor-arg value='89' /></bean></beans><beans profile='test2'><bean id='employee2' class='profiles.Person'><constructor-arg value='Bert' /><constructor-arg value='William' /><constructor-arg value='32' /></bean></beans></beans>盡管以最小的靈活性為代價(jià),但是這消除了通配符和加載多個(gè)文件的所有繁瑣處理。
我的下一個(gè)博客通過查看與新的@Profile注釋結(jié)合使用的@Configuration注釋,總結(jié)了我對(duì)Spring概要文件的了解,因此,稍后會(huì)有更多介紹。
參考:來自Captain Debug博客博客的JCG合作伙伴 Roger Hughes提供的在XML Config中使用Spring Profiles 。
翻譯自: https://www.javacodegeeks.com/2012/08/spring-profiles-in-xml-config-files.html
spring配置xml文件
總結(jié)
以上是生活随笔為你收集整理的spring配置xml文件_XML配置文件中的Spring配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 带ddos防御的国外服务器(带ddos防
- 下一篇: 人行账户超期备案罚款怎么办(人行账户超期