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(1);
        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(25);
        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); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM