我的上一个博客介绍了Spring 3.1的配置文件,并解释了使用它们的业务案例,并演示了它们在Spring XML配置文件中的用法。 但是,似乎很多开发人员更喜欢使用Spring的基于Java的应用程序配置,因此Spring设计了一种使用带有现有@Configuration批注的配置文件的方法。
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;
}
}
PropertyPlaceholderConfigurer
@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,其有效的雇员
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
catalina.properties
@Profile注释对@Configuration进行注释来创建Spring配置文件,然后通过将spring.profiles.active系统属性设置为配置文件的名称来打开要使用的配置文件
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”个人资料中。