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. php 程序执行时间检测

    我们有的时经常需要做程序的执行时间执行效率判断.大理石平台检定规程 实现的思路如下: <?php //记录开始时间 //记录结整时 // 开始时间  减去(-)  结束时间  得到程序的运行时间 ...

  2. (尚006)Vue计算属性之set与get

    test004.html <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  3. luogu 2312 解方程 乱搞+取模

    思路非常好想,但是你很难想到去用这个算法,因为这个几乎就是个乱搞~ 我们发现多项式中每一个系数都很大,但是 $m$ 却很小,即最多只用 $10^6$ 个整数需要验证. 我们知道,如果一个数等于 $0$ ...

  4. Luogu P1903 BZOJ 2120 数颜色 带修改的莫队

    https://www.luogu.org/problemnew/show/P1903 之前切过这道题,复习莫队再切一遍,不过我之前写的是主席树和树状数组,也不知道我当时怎么想的…… 这个题卡常我没写 ...

  5. Matlab中画图数学公式的输出格式

    1.可以在输出参数中选择 ('Interpreter', 'latex'). 2.字符序列中的数学表达式需用$$等形式括起来. 例子: text(6, 0.3, '$\leftarrow  y= 2^ ...

  6. HTTP请求响应的过程

    1. TCP/IP协议分层结构 应用层(含括了OSI七层中的上三层,分别为应用层,表示层, 会话层):DNS,  URI,  HTML,  HTTP,  TLS/SSL,  SMTP,   POP,  ...

  7. Python中_,__,__xx__方法区别

    _xx 单下划线开头 Python中没有真正的私有属性或方法,可以在你想声明为私有的方法和属性前加上单下划线,以提示该属性和方法不应在外部调用.如果真的调用了也不会出错,但不符合规范. 方法就是以单下 ...

  8. Linux系统配置静态ip

    一.工具/原料 redhat6.2 二.步骤 1."网络适配器"选择"桥接模式" 右键虚拟机,选择"设置"->"网络适配器& ...

  9. pythonw.exe不能用

    其实可以直接执行python目录下的Lib/idlelib/idle.bat即可,对于非安装版的python来说开始菜单是找不到启动快捷方式的.

  10. RK3399 pro 开发记录

    RK3399有三种启动模式:1.Normal模式:2.Loader模式:3.MaskRom模式.      Normal模式是正常的启动过程,各个组件依次加载,直到正常进入系统.      Loade ...