网站开发提供图片加载速度,wordpress速度,电子商务目前就业形势,深圳域名服务器地址我的上一个博客介绍了Spring 3.1的配置文件#xff0c;并解释了使用它们的业务案例#xff0c;并演示了它们在Spring XML配置文件中的用法。 但是#xff0c;似乎很多开发人员更喜欢使用Spring的基于Java的应用程序配置#xff0c;因此Spring设计了一种使用带有现有Configu… 我的上一个博客介绍了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; }
} 请记住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(valueTYPE) …这意味着只能将其放在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.activetest1 …或通过添加 # Setting a property value
spring.profiles.activetest1 到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