spring @profile注解的使用方法
本文主要介紹spring中@profile的使用方法以及在什么情況下使用。
首先說(shuō)一下為什么要使用這個(gè)@profile注解。@profile注解是spring提供的一個(gè)用來(lái)標(biāo)明當(dāng)前運(yùn)行環(huán)境的注解。我們正常開(kāi)發(fā)的過(guò)程中經(jīng)常遇到的問(wèn)題是,開(kāi)發(fā)環(huán)境是一套環(huán)境,qa測(cè)試是一套環(huán)境,線上部署又是一套環(huán)境。這樣從開(kāi)發(fā)到測(cè)試再到部署,會(huì)對(duì)程序中的配置修改多次,尤其是從qa到上線這個(gè)環(huán)節(jié),讓qa的也不敢保證改了哪個(gè)配置之后能不能在線上運(yùn)行。
為了解決上面的問(wèn)題,我們一般會(huì)使用一種方法,就是配置文件,然后通過(guò)不同的環(huán)境讀取不同的配置文件,從而在不同的場(chǎng)景中跑我們的程序。
那么,spring中的@profile注解的作用就體現(xiàn)在這里。在spring使用DI來(lái)依賴注入的時(shí)候,能夠根據(jù)當(dāng)前制定的運(yùn)行環(huán)境來(lái)注入相應(yīng)的bean。最常見(jiàn)的就是使用不同的DataSource了。
下面詳細(xì)的介紹一下,如何通過(guò)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中的值是不同的。通過(guò)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("我是中國(guó)人");
}
}
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)行真正的開(kāi)發(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可以通過(guò)ConfigurableApplicationContext的實(shí)例來(lái)的到。

最后提一下,如果是在web的后臺(tái)項(xiàng)目中如何進(jìn)行設(shè)置。這個(gè)時(shí)候我們通過(guò)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)文章
SpringBoot借助spring.factories文件跨模塊實(shí)例化Bean
這篇文章主要介紹了SpringBoot借助spring.factories文件跨模塊實(shí)例化Bean,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04
對(duì)比Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式
這篇文章主要介紹了Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式對(duì)比,文中列舉了兩種模式的相似點(diǎn)和不同點(diǎn),并都舉了代碼的實(shí)例作為參照,需要的朋友可以參考下2016-04-04
Java實(shí)現(xiàn)提取QSV文件視頻內(nèi)容
QSV是一種加密的視頻文件格式。是愛(ài)奇藝公司研發(fā)的一種視頻文件格式,這篇文章主要為大家介紹了如何利用Java實(shí)現(xiàn)提取QSV文件視頻內(nèi)容,感興趣的可以了解一下2023-03-03
spring AOP定義AfterThrowing增加處理實(shí)例分析
這篇文章主要介紹了spring AOP定義AfterThrowing增加處理,結(jié)合實(shí)例形式分析了spring面向切面AOP定義AfterThrowing相關(guān)實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下2020-01-01
springboot中json對(duì)象中對(duì)Long類型和String類型相互轉(zhuǎn)換
與前端聯(lián)調(diào)接口時(shí),后端一些字段設(shè)計(jì)為L(zhǎng)ong類型,這樣就有可能導(dǎo)致前端缺失精度,這時(shí)候我們就需要將Long類型返回給前端時(shí)做數(shù)據(jù)類型轉(zhuǎn)換,本文主要介紹了springboot中json對(duì)象中對(duì)Long類型和String類型相互轉(zhuǎn)換,感興趣的可以了解一下2023-11-11
解決Springboot @WebFilter攔截器未生效問(wèn)題
這篇文章主要介紹了解決Springboot @WebFilter攔截器未生效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
JAVA 根據(jù)設(shè)置的概率生成隨機(jī)數(shù)的方法
本篇文章主要介紹了JAVA 根據(jù)設(shè)置的概率生成隨機(jī)數(shù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
基于XML的MyBatis的環(huán)境搭建過(guò)程詳解(IDEA)
這篇文章主要介紹了基于XML的MyBatis的環(huán)境搭建過(guò)程詳解(IDEA),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
java 中HttpClient傳輸xml字符串實(shí)例詳解
這篇文章主要介紹了java 中HttpClient傳輸xml字符串實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

