我的上一个博客非常简单,因为它涵盖了我从Spring 3.0.x到Spring 3.1.x的轻松升级,最后我提到可以将Spring模式升级到3.1,以利用Spring的最新功能。 在今天的博客中,我将介绍这些功能中最酷的功能之一:Spring配置文件。 但是,在谈论如何实现Spring配置文件之前,我认为探索它们正在解决的问题是一个好主意,这是需要为不同的环境创建不同的Spring配置。 之所以会出现这种情况,通常是因为您的应用程序在其开发生命周期中需要连接到多个类似的外部资源,并且连接频率更高,而且这些“外部资源”通常不是数据库,尽管它们可以是JMS队列,Web服务,远程EJB等。

您的应用程序在运行之前必须工作的环境数量通常取决于几件事,包括您组织的业务流程,应用程序的规模以及它的“重要性”(即,如果您正在编写税收表)系统为您的国家/地区提供税收服务,那么测试过程可能比为本地商店编写电子商务应用程序时更为严格。 为使您有所了解,以下是想到的所有不同环境的快速列表(可能不完整):

  1. 本地开发者机器
  2. 开发测试机
  3. 测试团队功能测试机
  4. 集成测试机
  5. 克隆环境(实时副本)
  6. 生活

这不是一个新问题,通常可以通过为每个环境创建一组Spring XML和属性文件来解决。 XML文件通常包含一个导入其他环境特定文件的主文件。 然后在编译时将它们耦合在一起以创建不同的WAR或EAR文件。 这种方法已经使用了多年,但确实存在一些问题:

  1. 这是非标准的。 每个组织通常都有自己的解决方法,没有两种方法可以完全相同/
  2. 很难实现,为错误留出了很大的空间。
  3. 必须为每个环境创建一个不同的WAR / EAR文件并将其部署在每个环境上,这需要花费时间和精力,而花更多的时间编写代码可能更好。

Spring bean配置中的差异通常可以分为两部分。 首先,存在特定于环境的属性,例如URL和数据库名称。 通常使用PropertyPlaceholderConfigurer类和相关的$ {}标记将它们注入到Spring XML文件中。

<bean id='propertyConfigurer' class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'><property name='locations'><list><value>db.properties</value></list></property></bean>

其次,有特定于环境的bean类,例如数据源,它们通常根据您连接数据库的方式而有所不同。

例如,在开发中,您可能具有:

<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>

…无论是测试还是现场直播,您都可以简单地写:

<jee:jndi-lookup id='dataSource' jndi-name='jdbc/LiveDataSource'/>

Spring准则指出,仅应在上面的第二个示例中使用Spring概要文件:特定于Bean的类,并且您应继续使用PropertyPlaceholderConfigurer初始化简单的Bean属性; 但是,您可能希望使用Spring配置文件将特定于环境的PropertyPlaceholderConfigurer注入到Spring上下文中。

话虽如此,我将在示例代码中打破这一约定,因为我想用最简单的代码来演示Spring配置文件的功能。

Spring配置文件和XML配置
在XML配置方面,Spring 3.1在spring-beans模式的bean元素中引入了新的profile属性:

<beans profile='dev'>

当在不同环境中启用和禁用配置文件时,此配置文件属性充当开关。

为了进一步解释所有这些,我将使用一个简单的想法,即您的应用程序需要加载一个人员类,并且该人员类包含不同的属性,具体取决于您的程序在其上运行的环境。

Person类非常简单,看起来像这样:

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.xmltest-2-profile.xml (请记住这些名称,它们在以后很重要)。 如您所见,配置的唯一区别是名字,姓氏和年龄属性。

不幸的是,仅仅定义个人资料还不够,您必须告诉Spring您正在加载哪个个人资料。 这意味着遵循旧的“标准”代码现在将失败:

@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);}

幸运的是,有几种选择配置文件的方法,在我看来,最有用的方法是使用“ spring.profiles.active”系统属性。 例如,以下测试现在将通过:

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);

显然,您不希望像我上面那样对事情进行硬编码,最佳实践通常意味着将系统属性定义与应用程序分开。 这使您可以选择使用简单的命令行参数,例如:

-Dspring.profiles.active='test1'

…或通过添加

# Setting a property value
spring.profiles.active=test1

到Tomcat的catalina.properties

因此,仅此而已:您可以使用bean元素配置文件属性创建Spring XML配置文件,并通过将spring.profiles.active系统属性设置为配置文件的名称来打开要使用的配置文件

获得一些额外的灵活性

但是,这还不是故事的结局,因为Spring的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类而不是ClassPathXmlApplicationContextFileSystemXmlApplicationContext,因为这样可以提供更大的灵活性。 例如,在上面的代码中,我已经使用GenericApplicationContextload(...)方法使用通配符来加载许多配置文件:

ctx.load('*-profile.xml');

还记得以前的文件名吗? 这将同时加载test-1-profile.xmltest-2-profile.xml

配置文件还具有额外的灵活性,可让您一次激活多个。 如果看下面的代码,您会看到我正在激活我的test1test2配置文件:

@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);}

当心,在本示例中,我有两个标识为“ employee”的bean,并且无法分辨哪个是有效的,并且应该优先。 从我的测试中,我猜测读取的第二个覆盖或掩盖了对第一个的访问。 没关系,因为您不应该拥有多个具有相同名称的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' 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>

这消除了通配符和加载多个文件的所有繁琐处理,尽管是以最小的灵活性为代价的。

我的下一个博客通过查看与新的@Profile注释结合使用的@Configuration注释,总结了我对Spring概要文件的了解,等等。

参考:来自Captain Debug博客博客的JCG合作伙伴 Roger Hughes提供的在XML Config中使用Spring Profiles 。

翻译自: https://www.javacodegeeks.com/2012/08/spring-profiles-in-xml-config-files.html

XML配置文件中的Spring配置文件相关推荐

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

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

  2. java如何取到配置文件中值,从配置文件中获取配置信息的几种方法

    1.利用ResourceBundle类从properties文件中获取配置信息 创建配置文件并写入配置信息 image.png 使用以下代码可以轻松获取配置信息 package com.course. ...

  3. java spring web配置文件路径_java – spring配置文件和web.xml的确切位置在哪里?

    从文档: Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-se ...

  4. Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式

    Spring  EL 一:在Spring xml 配置文件中运用   Spring EL Spring EL 采用 #{Sp Expression  Language} 即 #{spring表达式} ...

  5. Spring Boot 配置文件中的花样,看这一篇足矣!

    点击蓝色"程序猿DD"关注我哟 加个"星标",不忘签到哦 关注我,回复口令获取可获取独家整理的学习资料: - 001 :领取<Spring Boot基础教 ...

  6. Spring的jdbcTemplate 与原始jdbc 整合c3p0的DBUtils 及Hibernate 对比 Spring配置文件生成约束的菜单方法...

    以User为操作对象 package com.swift.jdbc;public class User {private Long user_id; private String user_code; ...

  7. Spring自学日志00(Spring配置文件头及xsd文件版本浅析)

    来自该博客的技术 一.Spring配置文件头 最初Spring配置文件的头部声明如下: <?xml version="1.0" encoding="UTF-8&qu ...

  8. Python中logging.config配置文件解读

    Python中logging.config配置文件解读 下面的函数用于配置logging模块,它们位于logging.config模块中.你可以使用这些函数来配置,也可以在logging或是loggi ...

  9. jta atomikos_带有Atomikos示例的Tomcat中的Spring JTA多个资源事务

    jta atomikos 在本教程中,我们将向您展示如何使用Atomikos Transaction Manager在Tomcat服务器中实现JTA多个资源事务. Atomicos事务管理器为分布式事 ...

最新文章

  1. 春泥棒(偷春人) — ヨルシカ(MV + 歌词、汉译、罗马音)
  2. spring security加载原理(基于springboot)
  3. Win32程序中使用Combo box控件
  4. 实时监听input输入框value的变化:
  5. java工具类应该抛异常吗,java学习阶段一 工具类(异常)
  6. IIS无法启动——郁闷的KB939373补丁
  7. java fileinputstream 安全管理器校验失败_Spring Boot 如何做参数校验?
  8. vscode配置python2和python3_VS Code中配置python版本以及Python多版本
  9. 版本控制工具(svn)
  10. KindEditor上传图片
  11. python语言在ansys的应用_Python语言在ANSYS的应用52讲-掌握SCDM脚本建模及ANSYS二次开发能力...
  12. 代码时间换空间以及空间换时间
  13. Latex 字体调整-斜体-下划线-加粗-罗马数字
  14. 深度学习 | 误差反向传播法
  15. Linux Vmware CentOS 制作yum本地资源库和局域网资源库
  16. java官网教程(服务器篇)—— 并发
  17. angular中自定义组件实现双向绑定
  18. vue3缓存页面keep-alive+路由统一处理
  19. Project 的简单使用
  20. Nginx代理webSocket时60s自动断开, 怎么保持长连接

热门文章

  1. (转)web.xml 中的listener、 filter、servlet 加载顺序及其详解
  2. Spring MVC+layui(基于bootstrap)+t 新增功能(页面和数据传递)
  3. aws生态系统集成商_通过通用数据访问扩展AWS生态系统
  4. exec su-exec_WildFly Kubernetes exec探针
  5. javaone_JavaOne 2016 Essentials:您不应该错过的7个事件和会话
  6. 安装openstack_午餐前如何安装OpenStack Cloud
  7. javaone_JavaOne演讲者选择了您不容错过的10个会话
  8. log4j 程序日志_使用log4j监视和筛选应用程序日志到邮件
  9. Java 14:记录
  10. builder 模式_Builder模式和Spring框架