溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

@profile注解如何在spring中使用

發(fā)布時(shí)間:2021-03-30 16:00:37 來源:億速云 閱讀:174 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)@profile注解如何在spring中使用,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

首先是新建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>

整體看一下工程中的類和接口:

@profile注解如何在spring中使用

首先是Person類中有一個speak的方法,這個方法是MoveFactor這個借口提供的。Chinese、English和German都實(shí)現(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進(jìn)行測試

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é)果:

@profile注解如何在spring中使用

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

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

@profile注解如何在spring中使用

-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é)果:

@profile注解如何在spring中使用

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

@profile注解如何在spring中使用

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

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

以上就是@profile注解如何在spring中使用,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI