java profile_spring @profile注解的使用方法

本文主要介绍spring中@profile的使用方法以及在什么情况下使用。

首先说一下为什么要使用这个@profile注解。@profile注解是spring提供的一个用来标明当前运行环境的注解。我们正常开发的过程中经常遇到的问题是,开发环境是一套环境,qa测试是一套环境,线上部署又是一套环境。这样从开发到测试再到部署,会对程序中的配置修改多次,尤其是从qa到上线这个环节,让qa的也不敢保证改了哪个配置之后能不能在线上运行。

为了解决上面的问题,我们一般会使用一种方法,就是配置文件,然后通过不同的环境读取不同的配置文件,从而在不同的场景中跑我们的程序。

那么,spring中的@profile注解的作用就体现在这里。在spring使用DI来依赖注入的时候,能够根据当前制定的运行环境来注入相应的bean。最常见的就是使用不同的DataSource了。

下面详细的介绍一下,如何通过spring的@profile注解实现上面的功能。

首先是新建maven工程

mvn archetype:generate -DarchetypeCatalog=internal

下面是pom文件:

UTF-8

4.3.7.RELEASE

junit

junit

4.12

test

org.springframework

spring-context

${springframework.version}

org.springframework

spring-test

${springframework.version}

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

utf-8

maven-assembly-plugin

3.0.0

com.xueyou.demo

jar-with-dependencies

make-assembly

package

single

整体看一下工程中的类和接口:

63a734310454231bff9175f305c6d377.png

首先是Person类中有一个speak的方法,这个方法是MoveFactor这个借口提供的。Chinese、English和German都实现了这个接口。但是这三个类的@profile中的值是不同的。通过SpringTest中分配不同的activeprofile就能够实现调用不同的speak方法。

下面看代码:

MoveFactor.interface

package com.xueyou.demo;

public interface MoveFactor {

void speak();

}

Person.java

package com.xueyou.demo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

@Component

public class Person {

@Autowired

private MoveFactor moveFactor;

public void speak(){

moveFactor.speak();

}

}

Chinese.java

package com.xueyou.demo;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Profile;

import org.springframework.stereotype.Component;

@Configuration

@Profile(value = "dev")

@Component

public class Chinese implements MoveFactor {

@Override

public void speak() {

System.out.println("我是中国人");

}

}

English.java

package com.xueyou.demo;

import org.springframework.context.annotation.Profile;

import org.springframework.stereotype.Component;

@Component

@Profile("qa")

public class English implements MoveFactor{

@Override

public void speak() {

System.out.println("i am an English");

}

}

German.java

package com.xueyou.demo;

import org.springframework.context.annotation.Profile;

import org.springframework.stereotype.Component;

@Component

@Profile("prod")

public class German implements MoveFactor{

@Override

public void speak() {

System.out.println("i am a German");

}

}

使用springtest进行测试

package com.xueyou.demo;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ActiveProfiles;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = App.class)

@ActiveProfiles("dev")

public class SpringTest {

@Autowired

Person p;

@Test

public void testProfile(){

p.speak();

}

}

运行结果:

1d925b463aa791dad1586748b8af2039.png

当修改@ActiveProfile中的值时,输出的内容也会随之改变。

如果使用的是main函数进行真正的开发、测试和上线时,我们需要设置一下运行参数:

d36e80c081202af7398f31f761525ed3.png

-D 后面加上需要设置的spring的属性,就能够在main函数中使用了。

App.java

package com.xueyou.demo;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

/**

* Hello world!

// */

@Configuration

@ComponentScan(basePackages = {"com.xueyou.demo"})

public class App {

public static void main(String[] args) {

ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(com.xueyou.demo.App.class);

Person p = context.getBean(Person.class);

p.speak();

}

}

运行结果:

199e6ed5139c0fe36e15339189875798.png

如果需要得到当前的activeprofile可以通过ConfigurableApplicationContext的实例来的到。

ce6ff47d3733691632070d9784fa5b92.png

最后提一下,如果是在web的后台项目中如何进行设置。这个时候我们通过xml的方式进行设置:

spring.profiles.active

DOUBLEUPMINT

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值