spring @Profile的运用示例
@Profile的作用是把一些meta-data进行分类,分成Active和InActive这两种状态,然后你可以选择在active 和在Inactive这两种状态 下配置bean,
在Inactive状态通常的注解有一个!操作符,通常写为:@Profile("!p"),这里的p是Profile的名字。
下面demo中AppProfileConfig的bean在active状态下被IOC容器创建,而AppProfileConfig2是在Inactive状态下被IOC容器创建:
demo的思路是:先定义两个domain类,再写两个配置类即上面提的AppProfileConfig和AppProfileConfig2这两个类,最后写一个测试类:
示例代码如下:
第一个domain类:Alarm类的代码如下:
package com.timo.profile.domain;
public class Alarm {
private String name;
private Integer alarmSeverity;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAlarmSeverity() {
return alarmSeverity;
}
public void setAlarmSeverity(Integer alarmSeverity) {
this.alarmSeverity = alarmSeverity;
}
}
第二个domain类:ouyangfeng的代码如下:
package com.timo.profile.domain;
public class Ouyangfeng {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
第一个配置类:AppProfileConfig的代码如下:
package com.timo.profile; import com.timo.profile.domain.Alarm;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; @Configuration
@Profile("sixi")
public class AppProfileConfig {
@Bean
public Alarm alarm(){
Alarm alarm = new Alarm();
alarm.setAlarmSeverity();
alarm.setName("历史告警");
return alarm;
}
}
第二个配置类AppProfileConfig2的代码如下:
package com.timo.profile; import com.timo.profile.domain.Ouyangfeng;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; @Configuration
@Profile("!flower")
public class AppProfileConfig2 {
@Bean
public Ouyangfeng ouyangfeng(){
Ouyangfeng ouyangfeng = new Ouyangfeng();
ouyangfeng.setAge();
ouyangfeng.setName("欧阳");
return ouyangfeng;
}
}
测试类的代码如下:
package com.timo.profile; import com.timo.profile.domain.Alarm;
import com.timo.profile.domain.Ouyangfeng;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
//激活@Profile中name为sixi的类:
ctx.getEnvironment().setActiveProfiles("sixi");
ctx.register(AppProfileConfig.class,AppProfileConfig2.class);
ctx.refresh();
Alarm alarm = ctx.getBean(Alarm.class);
Ouyangfeng ouyangfeng = ctx.getBean(Ouyangfeng.class);
System.out.println("alarm="+alarm);
System.out.println("ouyangfeng="+ouyangfeng); }
}
spring @Profile的运用示例的更多相关文章
- 使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换
最近在做WebMagic的后台,遇到一个问题:后台用到了数据库,本来理想情况下是用Mysql,但是为了做到开箱即用,也整合了一个嵌入式 数据库H2.这里面就有个问题了,如何用一套代码,提供对Mysql ...
- Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换
前两篇不错 Spring.profile实现开发.测试和生产环境的配置和切换 - Strugglion - 博客园https://www.cnblogs.com/strugglion/p/709102 ...
- Spring 3.1新特性之一:使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换
最近在做WebMagic的后台,遇到一个问题:后台用到了数据库,本来理想情况下是用Mysql,但是为了做到开箱即用,也整合了一个嵌入式 数据库H2.这里面就有个问题了,如何用一套代码,提供对Mysql ...
- Spring入门(七):Spring Profile使用讲解
1. 使用场景 在日常的开发工作中,我们经常需要将程序部署到不同的环境,比如Dev开发环境,QA测试环境,Prod生产环境,这些环境下的一些配置肯定是不一样的,比如数据库配置,Redis配置,Rabb ...
- Spring profile配置应用
spring配置文件中可以配置多套不同环境配置,如下: <beans xml.....> <beans profile="dev"> < ...
- Maven 整合 spring profile实现多环境自动切换
Maven 整合 spring profile实现多环境自动切换 时间:2014-03-19 15:32来源:Internet 作者:Internet 点击:525次 profile主要用在项目多环境 ...
- maven spring profile 协同
请在网上查相关的使用情景,这里直接上要点.另外,可能不只一种方法,但这里只有一种. 1.POM.XML片段,使web.xml文件中有关活跃spring profile的内容可以被maven自动替换 & ...
- Spring @Transactional使用的示例
Spring @Transactional使用的示例: 参考: http://blog.csdn.net/seng3018/article/details/6690527 http://blog.si ...
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例
下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...
随机推荐
- Python自动化运维——系统性能信息模块
Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:psutil psutil是一个跨平台库,可以很轻松的为我们实现获取系统运行的进程和资源利用率等信息. 功能 ...
- Python3 模块、包调用&路径
''' 以下代码均为讲解,不能实际操作 ''' ''' 博客园 Infi_chu ''' ''' 模块的优点: 1.高可维护性 2.可以大大减少编写的代码量 模块一共有三种: 1.Python标准库 ...
- Java-Swing中使用Web富文本编辑器
资料下载 (截取出了邮件发送的功能.) 2018/11/10 因为要 win7 电脑 IE 8 的原因,使用了 jxBrower 拓展,更容易使用,参考链接(推荐) 问题介绍 window客户端软件的 ...
- ORB-SLAM 代码笔记(二)
ORB-SLAM中除了第三方库,基本没有看到使用c++11的新特性(例如别的SLAM框架中常用的智能指针,拷贝控制,泛型算法等,基本没有使用动态内存,栈内存读取速度较快),因此非常适合初学,代码很清晰 ...
- spring读取properties和其他配置文件的几种方式
1.因为spring容器的一些机制,在读取配置文件进行数据库的配置等等是很有必要的,所以我们要考虑配置文件的的读取方式以及各个方式的实用性 2.配置文件的读取方式我这里介绍2种,目的是掌握这2种就可以 ...
- 自定义T4模板去掉实体对象中的下划线
在EF Power Tool 默认使用的T4模板中,如果数据库表有下划线,那么生成的实体也有下划线,但是我们实际使用的过程中,是不希望有下划线的,要解决这个问题,可以自定义这个T4模板 ...
- js鼠标事件相关知识
1.mousedown->mouseup依次触发后相当于click事件 2.除了mouseenter和mouseleave外,其它的鼠标事件都是冒泡的 3.mouseover和mouseout事 ...
- 自动化测试--封装getDriver的方法
在自动化测试的时候,通常都会把最常用的功能封装起来,实现通用性. 该篇博客是实现了getDriver方法的封装. 第一次封装的时候,是使用的传参. @Parameters(value = {" ...
- winform 根据两点求出线上所有点及画出这条线
找出所有点: 根据斜率按照一个方向递增,求出对应的另一个方向的整数值. Point pStart = new Point(0, 2); Point pEnd = new Point(8, 2); // ...
- phpStoram破解方法