我的上一个博客介绍了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(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

Spring配置文件和Java配置相关推荐

  1. 具有Spring Boot和Java配置的Spring Batch教程

    我一直在努力将Podcastpedia.org的一些批处理作业迁移到Spring Batch. 以前,这些工作是以我自己的方式开发的,我认为现在是时候使用一种更"标准化"的方法了. ...

  2. java spring 配置文件_[Java教程]Spring配置文件

    [Java教程]Spring配置文件 0 2016-03-19 00:00:08 Spring配置文件是集成了Spring框架的项目的核心,引擎从哪里开始,中间都执行了哪些操作,小谈一下它的执行流程. ...

  3. Spring idea中spring配置文件自动检查配置应用程序上下文的操作

    IDEA中 Spring配置文件默认会被检查是否配置应用程序上下文 idea中默认会自动检查是否配置上下文 1.点击蓝色字体 2.点击创建新的应用程序上下文 3.点击确定就配置完成了 但是!开始不知道 ...

  4. Spring配置文件中关于约束配置详解

    一.Spring配置文件常见的配置头 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  5. spring配置xml文件_XML配置文件中的Spring配置文件

    spring配置xml文件 我的上一个博客非常简单,因为它涵盖了我从Spring 3.0.x到Spring 3.1.x的轻松升级,最后我提到可以将Spring模式升级到3.1,以利用Spring的最新 ...

  6. XML配置文件的命名空间与Spring配置文件中的头

    一直以来,写Spring配置文件,都是把其他配置文件的头拷贝过来,最多改改版本号,也不清楚哪些是需要的,到底是干嘛的.今天整理一下,拒绝再无脑copy. 一.Spring配置文件常见的配置头 < ...

  7. Spring配置文件详解三:Spring声明式事务管理

    1.声明式事务管理 Spring提供了声明式事务管理,这是通过Spring AOP实现的. 原理:Spring中进行事务管理的通常方式是利用AOP(面向切片编程)的方式,为普通java类封装事务控制, ...

  8. Spring MVC 无XML配置入门示例

    Spring MVC 无XML(纯 Java)配置入门示例 本示例是从<Spring in Action, Fourth Edition>一书而来,涉及的是书中5.1节部分内容,书中其实说 ...

  9. spring中的注解配置

    步骤: 1.为主配置文件引入新的命名空间(引入约束) 2.开启使用注解代替配置文件 在spring配置文件applicationContext中配置 <!-- 指定扫描com.lsz.sprin ...

最新文章

  1. 细说Django的admin
  2. 中国的数据科学家阶层正在形成
  3. Transformer再下一城!low-level多个任务榜首被占领,北大华为等联合提出预训练模型IPT
  4. CocoaPods was not found 解决
  5. springboot获取多个请求参数_springboot获取URL请求参数的多种方式
  6. 前端学习(2186):知识回顾
  7. Python《wallpaper abyss壁纸》
  8. web.py框架入门
  9. 6月国产网络游戏审批信息公布 共计86款游戏过审
  10. EasyUI:获取某个dategrid的所有行数据
  11. linux 下的文件搜索、可执行文件搜索
  12. 用cmd命令行在windows系统中进行分区操作
  13. QQ企业邮箱发送邮件
  14. 服务器网卡信息BIOS中设置,bios设置如何关闭网卡
  15. 利用Autolisp提取天正墙体位置坐标
  16. 如何让linux时间与internet时间同步(centos)
  17. h5 修改title 微信_微信分享H5自定义标题描述和图片
  18. 计算机网络-传输层:TCP协议
  19. Python数据收集入门
  20. Perfect Office Manner for Secretary 完美文秘办公礼仪

热门文章

  1. java ee打印功能_Java EE 8的前5个新功能
  2. 空调吸气和排气_吸气剂和二传手被认为有害
  3. dc/os_DC / OS中具有Java和数据库应用程序的服务发现
  4. java中精确地小数_在Java等于方法中进行精确比较
  5. hpcc_使用Java将数据流式传输到HPCC
  6. jms mdb_MDB!= JMS,反之亦然
  7. sts集成jboss_如何为JBoss Developer Studio 8设置集成和SOA工具
  8. 使用Selenium WebDriver测试自动化的22条实用技巧
  9. PIT,JUnit 5和Gradle –仅需额外的一行配置
  10. tomee_一罐将其全部统治:Apache TomEE + Shrinkwrap == JavaEE引导