最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring @profile注解的使用方法

 更新時間:2017年10月31日 14:58:46   作者:0day__  
本篇文章主要介紹了spring @profile注解的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

首先說一下為什么要使用這個@profile注解。@profile注解是spring提供的一個用來標(biāo)明當(dāng)前運行環(huán)境的注解。我們正常開發(fā)的過程中經(jīng)常遇到的問題是,開發(fā)環(huán)境是一套環(huán)境,qa測試是一套環(huán)境,線上部署又是一套環(huán)境。這樣從開發(fā)到測試再到部署,會對程序中的配置修改多次,尤其是從qa到上線這個環(huán)節(jié),讓qa的也不敢保證改了哪個配置之后能不能在線上運行。
為了解決上面的問題,我們一般會使用一種方法,就是配置文件,然后通過不同的環(huán)境讀取不同的配置文件,從而在不同的場景中跑我們的程序。

那么,spring中的@profile注解的作用就體現(xiàn)在這里。在spring使用DI來依賴注入的時候,能夠根據(jù)當(dāng)前制定的運行環(huán)境來注入相應(yīng)的bean。最常見的就是使用不同的DataSource了。

下面詳細(xì)的介紹一下,如何通過spring的@profile注解實現(xiàn)上面的功能。

首先是新建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都實現(xiàn)了這個接口。但是這三個類的@profile中的值是不同的。通過SpringTest中分配不同的activeprofile就能夠?qū)崿F(xiàn)調(diào)用不同的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(); 
  } 
 
} 

運行結(jié)果:

當(dāng)修改@ActiveProfile中的值時,輸出的內(nèi)容也會隨之改變。

如果使用的是main函數(shù)進行真正的開發(fā)、測試和上線時,我們需要設(shè)置一下運行參數(shù):

-D 后面加上需要設(shè)置的spring的屬性,就能夠在main函數(shù)中使用了。

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

運行結(jié)果:

如果需要得到當(dāng)前的activeprofile可以通過ConfigurableApplicationContext的實例來的到。

最后提一下,如果是在web的后臺項目中如何進行設(shè)置。這個時候我們通過xml的方式進行設(shè)置:

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot結(jié)合全局異常處理實現(xiàn)登錄注冊驗證

    springboot結(jié)合全局異常處理實現(xiàn)登錄注冊驗證

    這篇文章主要介紹了springboot結(jié)合全局異常處理實現(xiàn)登錄注冊驗證,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • SpringBoot Knife4j在線API文檔框架基本使用

    SpringBoot Knife4j在線API文檔框架基本使用

    knife4j是為Java MVC框架集成Swagger生成Api文檔的增強解決方案,這篇文章主要介紹了SpringBoot中使用Knife4J在線API文檔框架,需要的朋友可以參考下
    2022-12-12
  • 使用Java實現(xiàn)接口攔截器來監(jiān)控接口的執(zhí)行情況

    使用Java實現(xiàn)接口攔截器來監(jiān)控接口的執(zhí)行情況

    在排查問題的時候,由于沒有對接口的執(zhí)行情況,以及入?yún)⑦M行監(jiān)控,所以排查起問題就特別費勁,今天我們就一起來寫一個接口的攔截器來監(jiān)控接口的執(zhí)行情況吧
    2024-01-01
  • Spring整合Junit的使用詳解

    Spring整合Junit的使用詳解

    這篇文章主要介紹了Spring整合Junit的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 簡單了解Spring Boot及idea整合jsp過程解析

    簡單了解Spring Boot及idea整合jsp過程解析

    這篇文章主要介紹了簡單了解Spring Boot及idea整合jsp過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 自定義@RequestBody注解如何獲取JSON數(shù)據(jù)

    自定義@RequestBody注解如何獲取JSON數(shù)據(jù)

    這篇文章主要介紹了自定義@RequestBody注解如何獲取JSON數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 教你使用springboot配置多數(shù)據(jù)源

    教你使用springboot配置多數(shù)據(jù)源

    發(fā)現(xiàn)有很多小伙伴還不會用springboot配置多數(shù)據(jù)源,今天特地給大家整理了本篇文章,文中有非常詳細(xì)的圖文介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05
  • springboot框架各個層次基礎(chǔ)詳解

    springboot框架各個層次基礎(chǔ)詳解

    這篇文章主要介紹了springboot框架各個層次基礎(chǔ),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java字符串相加時的內(nèi)存表現(xiàn)和原理分析

    java字符串相加時的內(nèi)存表現(xiàn)和原理分析

    這篇文章主要介紹了java字符串相加時的內(nèi)存表現(xiàn)和原理分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Springboot logback-spring.xml無法加載問題

    Springboot logback-spring.xml無法加載問題

    這篇文章主要介紹了Springboot logback-spring.xml無法加載問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評論

隆子县| 申扎县| 那曲县| 江门市| 乐至县| 卢氏县| 夹江县| 盐源县| 洛阳市| 陇川县| 萨迦县| 封开县| 梁山县| 温州市| 商都县| 临江市| 剑川县| 白银市| 苗栗市| 曲阳县| 且末县| 洱源县| 江口县| 桃江县| 仁布县| 焦作市| 安国市| 两当县| 额敏县| 东平县| 沙雅县| 甘德县| 石嘴山市| 杭州市| 和硕县| 蕲春县| 顺义区| 霞浦县| 璧山县| 自贡市| 麻栗坡县|