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. SignalR2实时聊天

    SignalR2实时聊天 NuGet包中搜索SignalR添加引用 using Microsoft.AspNet.SignalR; 创建OWIN启动类 namespace SignalRChat { ...

  2. GDB core命令的使用调试段错误

    #include <stdio.h> void func(){ int *p = NULL; printf("*p:%d\n", *p);//断错误 } int mai ...

  3. Uoj #35. 后缀排序(后缀数组)

    35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在 ...

  4. Codevs 2185【模板】最长公共上升子序列

    题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了.小沐沐说,对于 ...

  5. docker使(二)—发布node应用镜像和容器

    应用在本地是已经ok的了,现在将node应用放进docker容器里面 获取node镜像 docker pull node 编写Dokerfile # 根据node镜像开始创建新的镜像(可以加上:tag ...

  6. ModuleNotFoundError: No module named 'cv2'

    pip3 install opencv-python

  7. js off 缓动动画

    动画也有很多种,一起来学习,缓动动画吧 缓动动画 1.缓动动画原理=盒子位置+(目标盒子位置-现在盒子位置)/10 2.步长越来越小 3.让步长越来越小的公式      步长=(目标位置-本身位置)/ ...

  8. LocalStorage 的 具体操作 与 设置有效期

    读取与存储 的使用方法 //存储 (名称 , 内容 , 有效期<小时> ) // 如果想要自己改变 有效期的单位 可以在 set 方法中 // 找到 这两个 setHours getHou ...

  9. python模块中__init__.py的作用

    基本概念先上结论举例解释实验一:不包含__init__.py实验二:A中包含__init__.py实验三:A.A_A中也包含__init__.py进阶基本概念概念 解释import 即导入,方式就是在 ...

  10. Linux静态路由配置

    配置了多块网卡后,需要指定数据包由哪块网卡发送,否则可能无法访问内网,这就要用到静态路由了. 配置静态路由有多种方式: 1.修改 /etc/rc.local 文件,这样每次重启后就会自动添加,如: e ...