我將使用我以前的博客中的Person類來演示配置文件和@Configuration批注。 這是一個簡單的bean類,其屬性取決于激活的概要文件而有所不同。
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; }
}
請記住,Spring專家建議僅在需要加載不同類型或類集的情況下使用Spring配置文件,并且在設置屬性時應繼續使用PropertyPlaceholderConfigurer 。 我違反規則的原因是我想嘗試編寫最簡單的代碼來演示配置文件和Java配置。
使用Spring配置文件和Java配置的核心是Spring的新@Profile注釋。 @Profile批注用于將配置文件名稱附加到@Configuration批注。 它采用一個可以以兩種方式使用的參數。 首先,將單個配置文件附加到@Configuration批注:
@Profile("test1")
其次,附加多個配置文件:
@Profile({ "test1", "test2" })
同樣,我將定義兩個配置文件“ test1”和“ test2”,并將每個配置文件與一個配置文件相關聯。 首先是“ test1”:
@Configuration
@Profile("test1")
public class Test1ProfileConfig { @Bean public Person employee() { return new Person("John", "Smith", 55); }
}
…然后是“ test2”:
@Configuration
@Profile("test2")
public class Test2ProfileConfig { @Bean public Person employee() { return new Person("Fred", "Williams", 22); }
}
在上面的代碼中,您可以看到我正在創建一個Person Bean,其有效的雇員 ID(來自方法名)在每個概要文件中返回不同的屬性值。
另請注意,@ Profile被標記為:
@Target(value=TYPE)
…這意味著只能將其放在@Configuration批注旁邊。
將@Profile附加到@Configuration后 ,下一步是激活您選擇的@Profile 。 這使用了與我在上一個博客中描述的原理和技術完全相同的方法,并且在我看來,最有用的激活技術是使用“ spring.profiles.active”系統屬性。
@Test public void testProfileActiveUsingSystemProperties() { System.setProperty("spring.profiles.active", "test1"); ApplicationContext ctx = new ClassPathXmlApplicationContext("profiles-config.xml"); Person person = ctx.getBean("employee", Person.class); String firstName = person.getFirstName(); assertEquals("John", firstName); }
顯然,您不想像我上面那樣對事情進行硬編碼,最佳實踐通常意味著將系統屬性配置與應用程序分開。 這使您可以選擇使用簡單的命令行參數,例如:
-Dspring.profiles.active="test1"
…或通過添加
# Setting a property value
spring.profiles.active=test1
到Tomcat的catalina.properties
所以,這就是全部:您可以通過使用@Profile注釋對@Configuration進行注釋來創建Spring配置文件,然后通過將spring.profiles.active系統屬性設置為配置文件的名稱來打開要使用的配置文件 。
像往常一樣,Spring的伙計們不僅將您限制在使用系統屬性來激活配置文件中,還可以以編程方式進行操作。 例如,下面的代碼創建AnnotationConfigApplicationContext ,然后在注冊我們的@Configuration類之前,使用Environment對象激活“ test1”配置文件。
@Test public void testAnnotationConfigApplicationContextThatWorks() { // Can register a list of config classes AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("test1"); ctx.register(Test1ProfileConfig.class, Test2ProfileConfig.class); ctx.refresh(); Person person = ctx.getBean("employee", Person.class); String firstName = person.getFirstName(); assertEquals("John", firstName); }
一切都很好,但是請注意,您需要以正確的順序調用AnnotationConfigApplicationContext的方法。 例如,如果您在指定配置文件之前注冊@Configuration類,則將收到IllegalStateException 。
@Test(expected = IllegalStateException.class) public void testAnnotationConfigApplicationContextThatFails() { // Can register a list of config classes AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( Test1ProfileConfig.class, Test2ProfileConfig.class); ctx.getEnvironment().setActiveProfiles("test1"); ctx.refresh(); Person person = ctx.getBean("employee", Person.class); String firstName = person.getFirstName(); assertEquals("John", firstName); }
在關閉今天的博客之前,下面的代碼演示了將多個@Profiles附加到@Configuration批注的功能。
@Configuration
@Profile({ "test1", "test2" })
public class MulitpleProfileConfig { @Bean public Person tourDeFranceWinner() { return new Person("Bradley", "Wiggins", 32); }
}
@Test public void testMulipleAssignedProfilesUsingSystemProperties() { System.setProperty("spring.profiles.active", "test1"); ApplicationContext ctx = new ClassPathXmlApplicationContext("profiles-config.xml"); Person person = ctx.getBean("tourDeFranceWinner", Person.class); String firstName = person.getFirstName(); assertEquals("Bradley", firstName); System.setProperty("spring.profiles.active", "test2"); ctx = new ClassPathXmlApplicationContext("profiles-config.xml"); person = ctx.getBean("tourDeFranceWinner", Person.class); firstName = person.getFirstName(); assertEquals("Bradley", firstName); }
在上面的代碼中,2012年環法自行車賽冠軍布拉德利·威金斯同時出現在“ test1”和“ test2”個人資料中。 ?
參考: Spring,來自JCG合作伙伴 Roger Hughes的Enterprise Java ,在Captain Debug的Blog博客中。
翻譯自: https://www.javacodegeeks.com/2012/08/spring-profiles-and-java-configuration.html