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 ...
随机推荐
- 014---Django的中间件
前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装 ...
- HDOJ 1176 免费馅饼(完全背包)
参考:https://blog.csdn.net/hhu1506010220/article/details/52369785 https://blog.csdn.net/enjoying_scien ...
- 最短路径算法 4.SPFA算法(1)
今天所说的就是常用的解决最短路径问题最后一个算法,这个算法同样是求连通图中单源点到其他结点的最短路径,功能和Bellman-Ford算法大致相同,可以求有负权的边的图,但不能出现负回路.但是SPFA算 ...
- 1321. [ZJOI2012] 灾难
1321. [ZJOI2012] 灾难 ★★☆ 输入文件:catas.in 输出文件:catas.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 阿米巴是小强的 ...
- 深度学习之卷积神经网络CNN
转自:https://blog.csdn.net/cxmscb/article/details/71023576 一.CNN的引入 在人工的全连接神经网络中,每相邻两层之间的每个神经元之间都是有边相连 ...
- Java常考面试题
Java常考面试题 1. 什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? 答:Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行 ...
- 【数据库】 SQL SERVER 2014 实用新特性
[数据库] SQL SERVER 2014 实用新特性 官方链接 一. 内存优化表 大幅提高数据库性能,不过目前没有窗口化设计只能写语句 二. 索引增强
- DDD领域驱动设计基本理论知识总结(转)
领域驱动设计之领域模型 为什么建立一个领域模型是重要的 领域通用语言(UBIQUITOUS LANGUAGE) 将领域模型转换为代码实现的最佳实践 领域建模时思考问题的角度 领域驱动设计的经典分层架构 ...
- Appium如何获取appPackage和appActivity
基本概念: appPackage:简单来说是App开发者提供的名称. appActivity:简单来说是App提供的各种不同的功能.每个程序都有个MainActivity,就是打开程序时显示在屏幕的活 ...
- LuffyCity-CMDB实战
第1章 章节一 课时01-ITIL介绍 课时02-CMDB介绍 课时03-CMDB需求讨论 课时04-CMDB需求讨论2 课时05-CMDB表结构设计 课时06-CMDB表结构设计2 课时07-CMD ...