@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性能测试工具 jprofiler

    1.下载地址 官方网址:http://www.ej-technologies.com/products/jprofiler/overview.html 2.Eclipse集成 该文(http://ji ...

  2. 【转】Django添加静态文件设置

    STATIC_URL = '/statics/'STATIC_ROOT= os.path.join(BASE_DIR, 'statics')STATICFILES_DIRS = ( os.path.j ...

  3. CSS3实现加载数据动画2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Returning Values from Bash Functions

    转自:https://www.linuxjournal.com/content/return-values-bash-functions Bash functions, unlike function ...

  5. Jmeter——小性能用例

    1.添加默认值,将代理服务器写入 2.添加HTTP请求头,将域名部分用变量形式写入:${__CSVRead(D:/number.txt,0)},这是为了查询不同页面,在D:/number.txt路径下 ...

  6. 九度OJ--1165(C++)

    #include <iostream>#include <string>#include <vector> using namespace std; int mai ...

  7. js/jquery 获取本地文件的文件路劲 获取input框中type=‘file’ 中的文件路径(转载)

     原文:http://blog.csdn.net/niyingxunzong/article/details/16989947 js/jquery 获取本地文件的文件路劲 获取input框中type= ...

  8. windowsserver2008 重新启动计算机命令

    在windowsserver2008中,若要重新启动计算机,可以输入以下命令即可立即重启计算机shutdown -r -t 0命令意义:shutdown在英文中意为关掉,在计算机中即为关机参数意义:- ...

  9. 【集训试题】exam 信心考 最小割

    题意概述: 有N个人,A,B两个考场.如果学生i在A考场,总信心值增加xi:如果学生i在B考场,总信心值增加yi.其中还有m对好友,当第i对好友的两个人都在A考场时,总信心值增加ai:如果两人都在B考 ...

  10. HDU 1271 整数对(思路题)

    假设删除第k位,把整数A表示成如下形式: A = a * 10^(k+1) + b * 10 ^k + c; 则: B = a * 10^k + c; N = A + B = (11*a+b)*10^ ...