一、配置环境

applicationContext.xml中添加下边的内容(develop:开发环境,production:生产环境,test:测试环境)

注意:profile的定义一定要在文档的最下边

<beans profile="develop">  

<context:property-placeholder location="classpath*:jdbc-develop.properties"/>  

</beans>  

<beans profile="production">  

<context:property-placeholder location="classpath*:jdbc-production.properties"/>  

</beans>  

<beans profile="test">  

<context:property-placeholder location="classpath*:jdbc-test.properties"/>  

</beans> 

二、切换环境

  在web.xml中添加一个context-param来切换当前环境:

  <context-param>  

    <param-name>spring.profiles.active</param-name>  

    <param-value>develop</param-value>  

  </context-param> 

  java代码可以这样做

  import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.hmkcode.vo.Person;

public class App 

{

    public static void main( String[] args )

    {

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

        ctx.getEnvironment().setActiveProfiles("develop"); //or production

        ctx.scan("com.hmkcode.spring"); // register all @Configuration classes

        //ctx.register(SpringConfigDev.class);

        ctx.refresh();

        Person person = (Person)ctx.getBean("person");

        System.out.println("Person: "+person);

    }

}

三、java代码

package com.myapp;

import javax.activation.DataSource;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Profile;

import

org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;

import

org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import org.springframework.jndi.JndiObjectFactoryBean;

@Configuration

public class DataSourceConfig {

@Bean(destroyMethod="shutdown")

@Profile("develop")

public DataSource embeddedDataSource() {

return new EmbeddedDatabaseBuilder()

.setType(EmbeddedDatabaseType.H2)

.addScript("classpath:schema.sql")

.addScript("classpath:test-data.sql")

.build();

}

@Bean

@Profile("production")

public DataSource jndiDataSource() {

JndiObjectFactoryBean jndiObjectFactoryBean =

new JndiObjectFactoryBean();

jndiObjectFactoryBean.setJndiName("jdbc/myDS");

jndiObjectFactoryBean.setResourceRef(true);

jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);

return (DataSource) jndiObjectFactoryBean.getObject();

}

}

160922、配置:spring通过profile或@profile配置不同的环境(测试、开发、生产)的更多相关文章

  1. 更加优雅地配置Spring Securiy(使用Java配置和注解)

    Spring Security 借助一系列Servlet Filter 来提供安全性功能,但是借助Spring的小技巧,我们只需要配置一个Filer就可以了,DelegatingFilterProxy ...

  2. Spring Boot系列之-profile

    Spring Boot profile用于分离不同环境的参数配置,通过spring.profile.active参数设置使用指定的profile. 在Spring Boot中应用程序配置可以使用2种格 ...

  3. Spring装配Bean---使用xml配置

    声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...

  4. Spring 快速开始 配置Spring Framework

    [配置Spring Framework] 1.XML配置依赖关系 bean是由Springframework管理,我们自己编写bean,Spring也内建了bean,比如ApplicationCont ...

  5. Spring Cloud 基于Consul 实现配置服务

    Spring Cloud体系中提供了Config组件来进行配置服务管理.而Consul除了提供服务注册与发现功能外,同时也提供配置管理功能.本位将介绍如何结合Spring Cloud + Consul ...

  6. 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置

    引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...

  7. 【Spring】使用@Profile注解实现开发、测试和生产环境的配置和切换,看完这篇我彻底会了!!

    写在前面 在实际的企业开发环境中,往往都会将环境分为:开发环境.测试环境和生产环境,而每个环境基本上都是互相隔离的,也就是说,开发环境.测试环境和生产环境是互不相通的.在以前的开发过程中,如果开发人员 ...

  8. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  9. 【maven】Maven根据Profile读取不同配置环境配置文件

    开发需求:在日常开发中,我们大多都会有开发环境(dev).测试环境(test).生产环境(product),不同环境的参数肯定不一样,我们需要在打包的时候,不同环境打不同当包,如果手动改,一方面效率低 ...

  10. SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...

随机推荐

  1. js基础的总结

    js中的每个函数都含有一个内建的arguments数组,能够返回函数接受的所有参数,不管函数有没有定义参数. function add() { var sum = 0; for (var i = 0; ...

  2. ifconfig命令(转)

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  3. EncodingAESKey

    关键字:EncodingAESKey 公众平台消息体签名及加解密方案概述 1.新增消息体签名验证,用于公众平台和公众账号验证消息体的正确性 2.针对推送给微信公众账号的普通消息和事件消息,以及推送给设 ...

  4. 利用NSUserdefaults来存储自定义的NSObject类及自定义类数组

    利用NSUserdefaults来存储自定义的NSObject类及自定义类数组 1.利用NSUserdefaults来存储自定义的NSObject类 利用NSUserdefaults也可以来存储及获取 ...

  5. .NET Framework Execution Was Aborted By Escalation Policy

    错误#1 09:31 2015/1/26上班查看ERRORLOG发现下面错误信息字面上理解是有内存压力,中午的时候ERRORLOG频繁报下面错误问题核实,一台服务器上安装两个实例,其中一个实例设置了最 ...

  6. AngularJS form表单验证(非常全面)

    构建一个ng表单 novalidate=”novalidate” 2.form中不能有action属性.提交交由ng-submit处理 3.每个input一定要有ng-model,最好有一个name方 ...

  7. 利用DescriptionAttribute实现枚举字符串

    我们知道定义枚举时是不允许带空格等符号的,这样就不利于进行字符串对比.当然可以通过给枚举添加DescriptionAttribute,然后通过fieldinfo读取DescriptionAttribu ...

  8. PAT 解题报告 1050. String Subtraction (20)

    1050. String Subtraction (20) Given two strings S1 and S2, S = S1 - S2 is defined to be the remainin ...

  9. Greenplum:学习资料

    Greenplum技术浅析:http://www.cnblogs.com/end/archive/2012/08/17/2644290.html Greenplum 数据库架构分析:http://ww ...

  10. SQL 数据库 子查询、主外键

    子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在 ...