spring @profile注解的使用方法

 更新时间:2017年10月31日 14:58:46   作者:0day__  
本篇文章主要介绍了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文件:

<properties> 
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
  <springframework.version>4.3.7.RELEASE</springframework.version> 
 </properties> 
 
 <dependencies> 
  <dependency> 
   <groupId>junit</groupId> 
   <artifactId>junit</artifactId> 
   <version>4.12</version> 
   <scope>test</scope> 
  </dependency> 
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 
  <dependency> 
   <groupId>org.springframework</groupId> 
   <artifactId>spring-context</artifactId> 
   <version>${springframework.version}</version> 
  </dependency> 
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> 
  <dependency> 
   <groupId>org.springframework</groupId> 
   <artifactId>spring-test</artifactId> 
   <version>${springframework.version}</version> 
  </dependency> 
 
 </dependencies> 
 <build> 
  <plugins> 
   <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <configuration> 
     <source>1.8</source> 
     <target>1.8</target> 
     <encoding>utf-8</encoding> 
    </configuration> 
   </plugin> 
   <plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>3.0.0</version> 
    <configuration> 
     <archive> 
      <manifest> 
       <mainClass>com.xueyou.demo</mainClass> 
      </manifest> 
     </archive> 
     <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
    </configuration> 
    <executions> 
     <execution> 
      <id>make-assembly</id> <!-- this is used for inheritance merges --> 
      <phase>package</phase> <!-- bind to the packaging phase --> 
      <goals> 
       <goal>single</goal> 
      </goals> 
     </execution> 
    </executions> 
   </plugin> 
  </plugins> 
 </build> 

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

首先是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(); 
  } 
 
} 

运行结果:

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

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

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

运行结果:

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

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

<context-param> 
 <param-name>spring.profiles.active</param-name> 
 <param-value>DOUBLEUPMINT</param-value> 
</context-param> 

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

相关文章

  • springboot结合全局异常处理实现登录注册验证

    springboot结合全局异常处理实现登录注册验证

    这篇文章主要介绍了springboot结合全局异常处理实现登录注册验证,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • SpringBoot Knife4j在线API文档框架基本使用

    SpringBoot Knife4j在线API文档框架基本使用

    knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,这篇文章主要介绍了SpringBoot中使用Knife4J在线API文档框架,需要的朋友可以参考下
    2022-12-12
  • 使用Java实现接口拦截器来监控接口的执行情况

    使用Java实现接口拦截器来监控接口的执行情况

    在排查问题的时候,由于没有对接口的执行情况,以及入参进行监控,所以排查起问题就特别费劲,今天我们就一起来写一个接口的拦截器来监控接口的执行情况吧
    2024-01-01
  • Spring整合Junit的使用详解

    Spring整合Junit的使用详解

    这篇文章主要介绍了Spring整合Junit的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • 简单了解Spring Boot及idea整合jsp过程解析

    简单了解Spring Boot及idea整合jsp过程解析

    这篇文章主要介绍了简单了解Spring Boot及idea整合jsp过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 自定义@RequestBody注解如何获取JSON数据

    自定义@RequestBody注解如何获取JSON数据

    这篇文章主要介绍了自定义@RequestBody注解如何获取JSON数据问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • 教你使用springboot配置多数据源

    教你使用springboot配置多数据源

    发现有很多小伙伴还不会用springboot配置多数据源,今天特地给大家整理了本篇文章,文中有非常详细的图文介绍及代码示例,对正在学习java的小伙伴很有帮助,需要的朋友可以参考下
    2021-05-05
  • springboot框架各个层次基础详解

    springboot框架各个层次基础详解

    这篇文章主要介绍了springboot框架各个层次基础,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • java字符串相加时的内存表现和原理分析

    java字符串相加时的内存表现和原理分析

    这篇文章主要介绍了java字符串相加时的内存表现和原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Springboot logback-spring.xml无法加载问题

    Springboot logback-spring.xml无法加载问题

    这篇文章主要介绍了Springboot logback-spring.xml无法加载问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05

最新评论