08点睛Spring4.1-Profile
8.1 Profile
- Profile让在不同环境下使用不同的配置提供了支持(如开发环境下的配置和生产环境下的配置肯定是不同的,如:数据库的配置);
- 通过设定Environment的ActiveProfiles来设定当前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的更多相关文章
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 08点睛Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)
8.1 配置 Spring MVC的配置是通过继承WebMvcConfigurerAdapter类并重载其方法实现的; 前几个教程已做了得配置包括 01点睛Spring MVC 4.1-搭建环境 配置 ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 17点睛Spring4.1-@Conditional
17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...
- 16点睛Spring4.1-TaskScheduler
转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...
- 15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...
- 13点睛Spring4.1-Spring EL
13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...
随机推荐
- Linux 一款免费的shell工具 MobaXterm_Personal
一款免费的shell工具 MobaXterm_Personal
- BCB key事件中判断Shift、Alt、Ctrl状态
BCB key事件中判断Shift.Alt.Ctrl状态: 类似此事件中 void __fastcall TForm1::keydown(TObject *Sender, WORD &Key, ...
- 怎么通过django模板输出双花括号{{}}
https://segmentfault.com/q/1010000000685399
- ehcache.xml 配置文件备忘录(不建议出现中文注释,此处备忘)
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...
- 页面上有tab,如何点击加载更多?
加载更多是一个很简单的东西.但是也有几点需要注意: 1.首先在你切换tab的时候,要么在调用这个函数的时候将这个的thispage设为1,要么在切换tab的时候将这个thispage设为1,当你将这个 ...
- Zk单机多实例部署
一.环境准备 当前环境:centos7.3一台软件版本:zookeeper-3.5.2部署目录:/usr/local/zookeeper启动端口:2181,2182,2183配置文件:/usr/loc ...
- 关于C#的DataGridView设置了DataSource后Rows无值问题
前言 今天写一个导出到Excel表的东西,以前也写过,之前导出都是将界面上的DataGridView中数据导出,没有任何问题,然而今天的导出场景是在界面是点击导出按钮,直接在数据库中查询符合条件的数据 ...
- java实现获取当前年、月、日 、小时 、分钟、 秒、 毫秒
转载 : https://blog.csdn.net/qq_36652619/article/details/85621020 package com.app.test; import java.te ...
- 范仁义web前端介绍课程---5、webstorm的下载安装
范仁义web前端介绍课程---5.webstorm的下载安装 一.总结 一句话总结: webstorm破解版搜索:webstorm破解 site:52pojie.cn 编辑器随便选用,功能都差不多,哪 ...
- OpenCV 3.4.2 环境搭建(适用于Ubuntu 一键安装)
前面的话 最近决定要好好地学习一下OpenCV,Ubuntu系统上简单地搭建了OpenCV环境,(Windows的搭建方法移步到window10的搭建方法),千里之行始于足下,不积跬步无以至千里,在这 ...