@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的运用示例的更多相关文章

  1. 使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换

    最近在做WebMagic的后台,遇到一个问题:后台用到了数据库,本来理想情况下是用Mysql,但是为了做到开箱即用,也整合了一个嵌入式 数据库H2.这里面就有个问题了,如何用一套代码,提供对Mysql ...

  2. Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换

    前两篇不错 Spring.profile实现开发.测试和生产环境的配置和切换 - Strugglion - 博客园https://www.cnblogs.com/strugglion/p/709102 ...

  3. Spring 3.1新特性之一:使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换

    最近在做WebMagic的后台,遇到一个问题:后台用到了数据库,本来理想情况下是用Mysql,但是为了做到开箱即用,也整合了一个嵌入式 数据库H2.这里面就有个问题了,如何用一套代码,提供对Mysql ...

  4. Spring入门(七):Spring Profile使用讲解

    1. 使用场景 在日常的开发工作中,我们经常需要将程序部署到不同的环境,比如Dev开发环境,QA测试环境,Prod生产环境,这些环境下的一些配置肯定是不一样的,比如数据库配置,Redis配置,Rabb ...

  5. Spring profile配置应用

    spring配置文件中可以配置多套不同环境配置,如下: <beans xml.....>     <beans profile="dev">     < ...

  6. Maven 整合 spring profile实现多环境自动切换

    Maven 整合 spring profile实现多环境自动切换 时间:2014-03-19 15:32来源:Internet 作者:Internet 点击:525次 profile主要用在项目多环境 ...

  7. maven spring profile 协同

    请在网上查相关的使用情景,这里直接上要点.另外,可能不只一种方法,但这里只有一种. 1.POM.XML片段,使web.xml文件中有关活跃spring profile的内容可以被maven自动替换 & ...

  8. Spring @Transactional使用的示例

    Spring @Transactional使用的示例: 参考: http://blog.csdn.net/seng3018/article/details/6690527 http://blog.si ...

  9. spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

    下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...

随机推荐

  1. JAVA 反射之Method

    ★ Method没有构造器,只能通过Class获取. 重点方法: class.getDeclaredMethods():获取所有方法. class.getDeclaredMethod(String n ...

  2. .Net 面试题 汇总(一)

    1.@page指令只能在_aspx___文件(填写扩展名)中使用,而@Control指令只能用在_ascx___文件(填写扩展名)中使用. 2.说明控件DataGrid,DataTable,DataV ...

  3. python2.7练习小例子(十七)

        17):题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制.     程序分析: ...

  4. MUI:字符串和json数据的相互转换

    JSON.parse()--字符串转换json.JSON.stringify()--json转换成字符串 如:收到Json对象:response,则: {"result":&quo ...

  5. Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...

  6. Android ImageSwitcher 配合Picasso解决内存溢出(OOM)问题

    最近项目中用到了 ImageSwitcher 来实现图片切换,使用起来很简单,但发现当图片比较大(超过了3M)时,程序出现了内存溢出(OOM)问题而崩溃了. 原因就是图片太大了,显示到 ImageVi ...

  7. adb 命令模拟按键事件

    转自:http://blog.csdn.net/jlminghui/article/details/39268419 例子:adb shell input keyevent 4 #这条命令相当于按了设 ...

  8. ES5新增数组方法(4):every

    检查数组元素的每个元素是否符合条件. // 数组中的元素全部满足指定条件返回true let arr = [1, 3, 5, 7, 9]; console.log(arr.every((value, ...

  9. 第六篇 常用请求协议之post put patch 总结

    [转]https://blog.csdn.net/sshfl_csdn     感谢愿意总结分享的人,thanks idempotent 幂等的 如果一个方法重复执行多次,产生的效果是一样的,那就是i ...

  10. 【Java】Map转换器

    描述: 在控制层接收参数时候, 往往会出现Json格式需要转换为Bean. 通常一两个字段可以用new去save pojo, 但字段多的情况呢? 以下就是为了解决这个尴尬情况,  自己写一个转换工具类 ...