@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. 清华大学《C++语言程序设计基础》线上课程笔记02---类与对象

    类与对象 public是类的对外访问接口: 类内初始值 在定义类时对数据成员写初始值,在创建对象的时候,会使用类内初始值初始化数据成员: class Clock { public: void show ...

  2. HM16.0帧内预测重要函数笔记

    Void TEncSearch::estIntraPredQT   亮度块的帧内预测入口函数 Void TComPrediction::initAdiPatternChType 获取参考样本点并滤波 ...

  3. xpath简单入门

    语法: 选取节点: 实例: (贴图转载自w3school) 补充: /a/@href        #获取a标签的href属性 当<div class="demo">& ...

  4. python2.7练习小例子(六)

        6):题目:斐波那契数列.     程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……. ...

  5. 通过圆形按钮的绘制熟悉Qt的绘图机制,掌握这种终极方法

    基本上用QPainter就可以实现 1. QPainter painter(this); //开始的标志(可以不用) painter.begin(this); //保存最初的设置 painter.sa ...

  6. 时屏蔽ios和android下点击元素时出现的阴影

    -webkit-tap-highlight-color -webkit-tap-highlight-color:rgba(255,255,255,0)

  7. PowerDesigner 使用记录

    使用PowerDesigner 16.5 设计数据库表结构的操作过程: 第一步:打开PowerDesigner工具 第二步:创建一个新的数据模型,选择 File -> New Model -&g ...

  8. npm命令 VS yarn命令

    npm yarn 说明 npm init yarn init  在项目中引导创建一个package.json文件 npm install yarn install/yarn  安装所有依赖包(依据pa ...

  9. Asp.net core中Migration工具使用的交流分享

    a,ul>li,em{ color:deeppink !important; } h2>a{ text-decoration:none; } ul>li{ padding:3px; ...

  10. 命令行编译 WPF

    在开发调试代码 WPF 时,经常需要在修改完成代码后,点击 Rebuild,然后到指定文件夹下点击打开对应的 .exe 验证程序是否正确, 可以通过以下命名实现修改程序后,点击一个 .bat 文件,直 ...