使用示例

//注解方式
public class DataSourceConfig {
@Bean
@Profile("prod")
public DataSource dataSource(){
return null;
}
}
//xml方式
<beans profile="prod">
.............................
</beans>

属性含义

通过profile标记不同的环境,可以通过设置spring.profiles.active和spring.profiles.default激活指定profile环境。如果设置了active,default便失去了作用。如果两个都没有设置,那么带有profiles的bean都不会生成。

有多种方式来设置这两个属性:

  • 作为DispatcherServlet的初始化参数;
  • 作为web应用的上下文参数;
  • 作为JNDI条目;
  • 作为环境变量; System.set("spring.profiles.active","prod")
  • 作为JVM的系统属性; -Dspring.profiles.active="prod"
  • 在集成测试类上,使用@ActiveProfiles注解配置。

以前两种方式举例,它们都可以在web.xml中设置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>applicationContext</param-name>
<param-value>/applicationContext.xml</param-value>
</context-param>
<!-- 在上下文中设置profile的默认值 -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 在servlet中设置profile的默认值 -->
<init-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Spring的profile属性的更多相关文章

  1. 【译】Spring 4 @Profile注解示例

    前言 译文链接:http://websystique.com/spring/spring-profile-example/ 本文将探索Spring中的@Profile注解,可以实现不同环境(开发.测试 ...

  2. Spring读取外部属性-properties

    概述 在Spring中处理外部值最简常用的方法就是外部创建name.properties文件,并在其中声明变量值,供Java进行读取.比如数据源信息配置,Java固定属性位置等.读取的方式一般由三种: ...

  3. Spring Boot的属性加载顺序

        伴随着团队的不断壮大,往往不需要开发人员知道测试或者生产环境的全部配置细节,比如数据库密码,帐号信息等.而是希望由运维或者指定的人员去维护配置信息,那么如果要修改某项配置信息,就不得不去修改项 ...

  4. 【转】Spring Boot Profile使用

    http://blog.csdn.net/he90227/article/details/52981747 摘要: spring Boot使用@Profile注解可以实现不同环境下配置参数的切换,任何 ...

  5. 【Java Web开发学习】Spring环境profile

    [Java Web开发学习]Spring 环境profile 转载:http://www.cnblogs.com/yangchongxing/p/8890702.html 开发.测试.生产环境往往是不 ...

  6. Spring激活profile的方式

    Spring中激活profile的方法:设置spring.profiles.active和spring.profiles.default这两个属性 设置激活profile属性的地方(优先级由高到底) ...

  7. 1-3 Spring Bean 的属性值设置

    详见http://www.cnblogs.com/chenssy/archive/2013/03/17/2964593.html 1.注入普通的属性值 <bean id="Cat&qu ...

  8. 使用Spring注解注入属性

    本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...

  9. Spring中的属性编辑器的使用

    Spring中的属性编辑器的使用 转载自 http://blog.sina.com.cn/s/blog_59ca2c2a0100jxwh.html Struts中用一个类型转换器,在Spring中也有 ...

随机推荐

  1. odbc错误信息一览表

    ODBC 错误信息 根据 X/Open 和 SQL Access Group SQL CAE 规范 (1992) 所进行的定义,SQLERROR 返回 SQLSTATE 值.SQLSTATE 值是包含 ...

  2. 通用js类库

    /* 其它通用函数 */$(function() { // var General = function() { var _self = this; /* 写 cookie 操作 */ _self.S ...

  3. Altera的几个常用的Synthesis attributes

    各厂商综合工具,对HDL综合时都定义了一些综合属性这些属性可指定a declaration,a module item,a statement, or a port connection 不同的综合方 ...

  4. sql注入学习小结

    /* 转载请注明出处,By:珍惜少年时 小知识,只是放在博客吃饭时无聊看看,大牛勿喷. */ 珍惜少年时博客,专注网络安全 web渗透测试 00x1爆所有库: mysql> select sch ...

  5. 谷歌、百度、1万ip能赚多少钱?1000IP能够值多少钱呢?

    谷歌.百度.1万ip能赚多少钱?1000IP能够值多少钱呢? (2014-04-03 11:50:52) 转载▼ 标签: 广告联盟 百度联盟 谷歌联盟 ip赚钱       很多在人问:谷歌.百度:1 ...

  6. CentOS 初始化时的分区

    /            ext3    8189   固定大小 空          swap    509   固定大小 /boot     ext3     100   固定大小 /home   ...

  7. Java 7 的7个新特性

    1.对集合类的语言支持:(??) 2.自动资源管理: 3.改进的通用实例创建类型推断:(??) 4.数字字面量下划线支持:(√) 5.switch中使用string:(√) 6.二进制字面量:(√) ...

  8. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  9. Android读写assets、raw、sdard和工程文件的方法

    Android开发离不开对文件的操作,前面的文章“Android简易数据存储之SharedPreferences”和“Android数据存储之SQLite的操作”,分别讲解了简单的数据的存储和数据库数 ...

  10. Java for LeetCode 027 Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...