一、配置环境

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. Aspose.Words 开发时遇到的问题

    问题一 Document doc.Save(Response, "学员报名表.pdf", ContentDisposition.Inline, null); 执行后没有效果,因为异 ...

  2. SET Statements (Transact-SQL)

    The Transact-SQL programming language provides several SET statements that change the current sessio ...

  3. Python-S13作业-day3-之编辑ha.conf配置文件

    Python-S13作业-day3-之编辑ha.conf配置文件 需求: 让用户输入字符串类型的字典,实现对配置文件指定,backend  www.oldboy.org下的内容进行 * 查询 * 增加 ...

  4. iOS:详细的正则表达式

    1.简介: 在项目中,正则的使用是很普遍的,例如登录账号和密码(手机号.邮箱等).用到的方法就是谓词对象过滤:NSPredicate. 2.什么是正则表达式: 正则表达式,又称正规表示法,是对字符串操 ...

  5. PHP受保护的类和私有类什么区别

    受保护的继承后可以访问,私有的只能在该类中访问,不会被继承访问class Man{ protected $name='lee';//受保护 private $age=123;//私有 function ...

  6. iOS - (TableView中利用系统的 cell 设置 cell.textlabel 位置和大小)

    今天工作稍微的遇到了一点小小的难题,需求效果中 TableView cell 中的 Label 字体大小比原先系统中的要大些且 Label 位置不是在前面,而是在中间往后,对于这个问题我第一时间也是想 ...

  7. Latex技巧

    文件.tex 文件打开 error reading 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/jzhao-logs/27226136 ...

  8. 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators

    backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...

  9. POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)

    Description Given a big integer number, you are required to find out whether it's a prime number. In ...

  10. 动画--过渡所需时间 transition-duration

    transition-duration属性主要用来设置一个属性过渡到另一个属性所需的时间,也就是从旧属性过渡到新属性花费的时间长度,俗称持续时间. 案例演示: 在鼠标悬停(hover)状态下,让容器从 ...