8.1 Profile

  • Profile让在不同环境下使用不同的配置提供了支持(如开发环境下的配置和生产环境下的配置肯定是不同的,如:数据库的配置);

    • 通过设定EnvironmentActiveProfiles来设定当前context需要使用的配置环境
    • 通过设定jvm的spring.profiles.active参数来设置配置环境(web项目中设置在servlet的context parameter中)

8.2 示例

8.2.1 新建测试bean

package com.wisely.profile;

public class DemoBean {
private String url; public DemoBean(String url) {
super();
this.url = url;
System.out.println("地址为:"+url);
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} }

8.2.2 编写配置文件

package com.wisely.profile;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; @Configuration
public class DemoConfig { @Bean
@Profile("dev")
public DemoBean devDemoBean(){
return new DemoBean("http://www.baidu.com");
} @Bean
@Profile("prod")
public DemoBean prodDemoBean(){
return new DemoBean("http://www.qq.com");
} }

8.2.3 测试

8.2.3.1 使用Environment选择配置

package com.wisely.profile;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("dev");
context.scan("com.wisely.profile");
context.refresh();
context.close(); } }

输出结果

地址为:http://www.baidu.com

8.2.3.2 使用JVM参数选择配置

package com.wisely.profile;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.wisely.profile");
context.refresh();
context.close(); } }

  输出结果:

地址为:http://www.qq.com

8.2.3.2 在web项目中的配置

  • web.xml(servlet 2.5及以下)
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
  • java config(servlet 3.0及以上)
public class WebInit implements WebApplicationInitializer {

    @Override
public void onStartup(ServletContext container) throws ServletException {
container.setInitParameter("spring.profiles.default", "dev"); } }

08点睛Spring4.1-Profile的更多相关文章

  1. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  2. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  3. 08点睛Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)

    8.1 配置 Spring MVC的配置是通过继承WebMvcConfigurerAdapter类并重载其方法实现的; 前几个教程已做了得配置包括 01点睛Spring MVC 4.1-搭建环境 配置 ...

  4. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  5. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  6. 17点睛Spring4.1-@Conditional

    17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...

  7. 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...

  8. 15点睛Spring4.1-TaskExecutor

    转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...

  9. 13点睛Spring4.1-Spring EL

    13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...

随机推荐

  1. Oracle NVL 函数 nvl nvl2

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

  2. Intel 8086 CPU

    一.8086概述 Intel8086拥有四个16位的通用寄存器,也能够当作八个8位寄存器来存取,以及四个16位索引寄存器(包含了堆栈指标).资料寄存器通常由指令隐含地使用,针对暂存值需要复杂的寄存器配 ...

  3. git 查看项目代码统计命令

    git log --author="xxxxxxxx" --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; ...

  4. (23)打鸡儿教你Vue.js

    实例: 模板语法 vue-router,vuex以及调式方法介绍 打包部署: npm run build Webpack 目前无论在求职还是工作中,使用越来越普及.而想要学懂,学会Webpack更绝非 ...

  5. 在Android Studio中找不到AppCompatActivity解决方案

    在创建新的.java文件时,要导入父类中的 AppCompatActivity,报错,无法找到这个父类. 解决方案:   1.先找到“project structure”,然后app--Depende ...

  6. 记一次vue+vuex+vue-router+axios+elementUI开发(一)

    记录自己的vue开发之旅,方便之后查询 一.开发环境 1.安装node.js  自带npm https://nodejs.org/en/ 2. 全局安装vue-cli脚手架 npm install v ...

  7. GAN 原理及公式推导

    Generative Adversarial Network,就是大家耳熟能详的 GAN,由 Ian Goodfellow 首先提出,在这两年更是深度学习中最热门的东西,仿佛什么东西都能由 GAN 做 ...

  8. MySQL Online DDL导致全局锁表案例分析

    MySQL Online DDL导致全局锁表案例分析 我这边遇到了什么问题? 线上给某个表执行新增索引SQL, 然后整个数据CPU打到100%, 连接数暴增到极限, 最后导致所有访问数据库的应用都奔溃 ...

  9. QT中显示动画

    在QT中要显示GIF图片,不能通过单单的添加部件来完成.还需要手动的编写程序.工具:QT Creator新建一个工程,我们先在designer中,添加一个QLabel部件. 将QLabel拉成适当大小 ...

  10. PCR | RT-PCR 的原理及应用

    生物的东西必须要主动去了解,否则视野容易受到限制,尤其是分子生物学的核心技术. PCR - 聚合酶链式反应 The Complete Guide to PCR (How it Works, Prime ...