一、

Spring honors two separate properties when determining which profiles are active:
spring.profiles.active and spring.profiles.default . If spring.profiles.active
is set, then its value determines which profiles are active. But if spring
.profiles.active isn’t set, then Spring looks to spring.profiles.default . If neither
spring.profiles.active nor spring.profiles.default is set, then there are no
active profiles, and only those beans that aren’t defined as being in a profile are created.
There are several ways to set these properties:
 As initialization parameters on DispatcherServlet
 As context parameters of a web application
 As JNDI entries
 As environment variables
 As JVM system properties
 Using the @ActiveProfiles annotation on an integration test class

二、

1.在web.xml中设置PROFILE的默认值

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  3. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>/WEB-INF/spring/root-context.xml</param-value>
  7. </context-param>
  8. <context-param>
  9. <param-name>spring.profiles.default</param-name>
  10. <param-value>dev</param-value>
  11. </context-param>
  12. <listener>
  13. <listener-class>
  14. org.springframework.web.context.ContextLoaderListener
  15. </listener-class>
  16. </listener>
  17. <servlet>
  18. <servlet-name>appServlet</servlet-name>
  19. <servlet-class>
  20. org.springframework.web.servlet.DispatcherServlet
  21. </servlet-class>
  22. <init-param>
  23. <param-name>spring.profiles.default</param-name>
  24. <param-value>dev</param-value>
  25. </init-param>
  26. <load-on-startup>1</load-on-startup>
  27. </servlet>
  28. <servlet-mapping>
  29. <servlet-name>appServlet</servlet-name>
  30. <url-pattern>/</url-pattern>
  31. </servlet-mapping>
  32. </web-app>

2.在测试时可以用@ActiveProfiles来切换profile

  1. package profiles;
  2.  
  3. import static org.junit.Assert.assertNotNull;
  4.  
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.List;
  8.  
  9. import javax.sql.DataSource;
  10. import static org.junit.Assert.*;
  11.  
  12. import org.junit.Test;
  13. import org.junit.runner.RunWith;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.jdbc.core.JdbcTemplate;
  16. import org.springframework.jdbc.core.RowMapper;
  17. import org.springframework.test.context.ActiveProfiles;
  18. import org.springframework.test.context.ContextConfiguration;
  19. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  20.  
  21. import com.myapp.DataSourceConfig;
  22.  
  23. public class DataSourceConfigTest {
  24.  
  25. @RunWith(SpringJUnit4ClassRunner.class)
  26. @ContextConfiguration(classes=DataSourceConfig.class)
  27. @ActiveProfiles("dev")
  28. public static class DevDataSourceTest {
  29. @Autowired
  30. private DataSource dataSource;
  31.  
  32. @Test
  33. public void shouldBeEmbeddedDatasource() {
  34. assertNotNull(dataSource);
  35. JdbcTemplate jdbc = new JdbcTemplate(dataSource);
  36. List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {
  37. @Override
  38. public String mapRow(ResultSet rs, int rowNum) throws SQLException {
  39. return rs.getLong("id") + ":" + rs.getString("name");
  40. }
  41. });
  42.  
  43. assertEquals(1, results.size());
  44. assertEquals("1:A", results.get(0));
  45. }
  46. }
  47.  
  48. @RunWith(SpringJUnit4ClassRunner.class)
  49. @ContextConfiguration(classes=DataSourceConfig.class)
  50. @ActiveProfiles("prod")
  51. public static class ProductionDataSourceTest {
  52. @Autowired
  53. private DataSource dataSource;
  54.  
  55. @Test
  56. public void shouldBeEmbeddedDatasource() {
  57. // should be null, because there isn't a datasource configured in JNDI
  58. assertNull(dataSource);
  59. }
  60. }
  61.  
  62. @RunWith(SpringJUnit4ClassRunner.class)
  63. @ContextConfiguration("classpath:datasource-config.xml")
  64. @ActiveProfiles("dev")
  65. public static class DevDataSourceTest_XMLConfig {
  66. @Autowired
  67. private DataSource dataSource;
  68.  
  69. @Test
  70. public void shouldBeEmbeddedDatasource() {
  71. assertNotNull(dataSource);
  72. JdbcTemplate jdbc = new JdbcTemplate(dataSource);
  73. List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {
  74. @Override
  75. public String mapRow(ResultSet rs, int rowNum) throws SQLException {
  76. return rs.getLong("id") + ":" + rs.getString("name");
  77. }
  78. });
  79.  
  80. assertEquals(1, results.size());
  81. assertEquals("1:A", results.get(0));
  82. }
  83. }
  84.  
  85. @RunWith(SpringJUnit4ClassRunner.class)
  86. @ContextConfiguration("classpath:datasource-config.xml")
  87. @ActiveProfiles("prod")
  88. public static class ProductionDataSourceTest_XMLConfig {
  89. @Autowired(required=false)
  90. private DataSource dataSource;
  91.  
  92. @Test
  93. public void shouldBeEmbeddedDatasource() {
  94. // should be null, because there isn't a datasource configured in JNDI
  95. assertNull(dataSource);
  96. }
  97. }
  98.  
  99. }

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

    一. 1.SpEL expressions are framed with  #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, ...

  2. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

    一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...

  3. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value

    一.用placeholder给bean运行时注入值的步骤 Spring取得placeholder的值是用${...} 1.声明placeholder bean (1)java方式 In order t ...

  4. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode

    一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main ...

  5. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary

    一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...

  6. SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile

    一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...

  7. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值

    1.When injecting properties and constructor arguments on beans that are created via component-scanni ...

  8. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解

    一. The @Qualifier annotation is the main way to work with qualifiers. It can beapplied alongside @Au ...

  9. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile

    一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...

随机推荐

  1. 别了 oi——一篇高三狗的滚粗遗言

    /* 开始于2015年12月 结束于2016年11月 一年的oi生涯有很多值得怀念的事 还记得去年旺哥找我学oi 当时是一脸的蒙逼 要知道 高二才开始搞是很晚了 然而 也就是那一晚之后 许多事情都变了 ...

  2. MySQL索引视图

    一.索引 索引是存放在模式(schema)中的一个数据库对象,索引的作用就是提高对表的检索查询速度, 索引是通过快速访问的方法来进行快速定位数据,从而减少了对磁盘的读写操作. 索引是数据库的一个对象, ...

  3. 最近整理的一些行列转换sql(有自己的,有别人的),留作记录

    --case when 经典用法SELECT * FROM        (SELECT 1 NUM,              '奖项金额',              SUM(CASE WHEN ...

  4. javascript 基础3第13节

    <html> <head> <title>javascript基础</title> </head> <body> 1.流程控制 ...

  5. 【原创】QT编程 多线程

    请先保证已安装QT,没有请参考 http://www.cnblogs.com/kavs/p/4608926.html  安装QT. 新建threads文件夹存放项目:mkdir threads sud ...

  6. c#中创建类(更新中)

    类是最常见的一种引用类型,最简单的定义如下 class YouClassNam {} 复杂的类可能包含一下内容 类属性 类属性以及类修饰符.  非嵌套的类修饰符有:public,internal,ab ...

  7. CI源码学习 一步一步重写 CodeIgniter 框架

    文章:http://www.cnblogs.com/zhenyu-whu/archive/2013/08.html

  8. 互联网HTTP连接等出错代码大全

    100 - Continue  101 - Switching Protocols Success Codes  200 - OK  201 - Created  202 - Accepted  20 ...

  9. Homebrew安装php5及composer for mac教程

    安装brew 可以查看教程:mac os x 10.9.1 安装 Homebrew软件包管理工具及brew安装maven3.1.1 首先更新下brew软件库 brew update brew tap ...

  10. 禁止生产pyc

    sys.dont_write_bytecode = 1 来自为知笔记(Wiz)