spring @profile注解的使用方法
本文主要介紹spring中@profile的使用方法以及在什么情況下使用。
首先說一下為什么要使用這個@profile注解。@profile注解是spring提供的一個用來標明當前運行環(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ù)當前制定的運行環(huán)境來注入相應的bean。最常見的就是使用不同的DataSource了。
下面詳細的介紹一下,如何通過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é)果:

當修改@ActiveProfile中的值時,輸出的內(nèi)容也會隨之改變。
如果使用的是main函數(shù)進行真正的開發(fā)、測試和上線時,我們需要設置一下運行參數(shù):

-D 后面加上需要設置的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é)果:

如果需要得到當前的activeprofile可以通過ConfigurableApplicationContext的實例來的到。

最后提一下,如果是在web的后臺項目中如何進行設置。這個時候我們通過xml的方式進行設置:
<context-param> <param-name>spring.profiles.active</param-name> <param-value>DOUBLEUPMINT</param-value> </context-param>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot借助spring.factories文件跨模塊實例化Bean
這篇文章主要介紹了SpringBoot借助spring.factories文件跨模塊實例化Bean,文章圍繞主題展開詳細的內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04
spring AOP定義AfterThrowing增加處理實例分析
這篇文章主要介紹了spring AOP定義AfterThrowing增加處理,結(jié)合實例形式分析了spring面向切面AOP定義AfterThrowing相關實現(xiàn)步驟與操作技巧,需要的朋友可以參考下2020-01-01
springboot中json對象中對Long類型和String類型相互轉(zhuǎn)換
與前端聯(lián)調(diào)接口時,后端一些字段設計為Long類型,這樣就有可能導致前端缺失精度,這時候我們就需要將Long類型返回給前端時做數(shù)據(jù)類型轉(zhuǎn)換,本文主要介紹了springboot中json對象中對Long類型和String類型相互轉(zhuǎn)換,感興趣的可以了解一下2023-11-11
解決Springboot @WebFilter攔截器未生效問題
這篇文章主要介紹了解決Springboot @WebFilter攔截器未生效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
基于XML的MyBatis的環(huán)境搭建過程詳解(IDEA)
這篇文章主要介紹了基于XML的MyBatis的環(huán)境搭建過程詳解(IDEA),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

