欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring @profile注解的使用方法

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

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

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

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

下面詳細(xì)的介紹一下,如何通過spring的@profile注解實(shí)現(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類中有一個(gè)speak的方法,這個(gè)方法是MoveFactor這個(gè)借口提供的。Chinese、English和German都實(shí)現(xiàn)了這個(gè)接口。但是這三個(gè)類的@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進(jìn)行測(cè)試

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

運(yùn)行結(jié)果:

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

如果使用的是main函數(shù)進(jìn)行真正的開發(fā)、測(cè)試和上線時(shí),我們需要設(shè)置一下運(yùn)行參數(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(); 
  } 
} 

運(yùn)行結(jié)果:

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

最后提一下,如果是在web的后臺(tái)項(xiàng)目中如何進(jìn)行設(shè)置。這個(gè)時(shí)候我們通過xml的方式進(jìn)行設(shè)置:

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

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

相關(guān)文章

最新評(píng)論