spring @profile注解的使用

本文主要介绍spring中@profile的使用方法以及在什么情况下使用。
本文主要参考:
http://www.cnblogs.com/davidwang456/p/4429058.html

好,下面上货。

首先说一下为什么要使用这个@profile注解。@profile注解是spring提供的一个用来标明当前运行环境的注解。我们正常开发的过程中经常遇到的问题是,开发环境是一套环境,qa测试是一套环境,线上部署又是一套环境。这样从开发到测试再到部署,会对程序中的配置修改多次,尤其是从qa到上线这个环节,让qa的也不敢保证改了哪个配置之后能不能在线上运行。
为了解决上面的问题,我们一般会使用一种方法,就是配置文件,然后通过不同的环境读取不同的配置文件,从而在不同的场景中跑我们的程序。
那么,spring中的@profile注解的作用就体现在这里。在spring使用DI来依赖注入的时候,能够根据当前制定的运行环境来注入相应的bean。最常见的就是使用不同的DataSource了。

下面详细的介绍一下,如何通过spring的@profile注解实现上面的功能。

首先是新建maven工程
mvn archetype:generate -DarchetypeCatalog=internal

下面是pom文件:

[html]  view plain  copy
  1. <properties>  
  2.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  3.     <springframework.version>4.3.7.RELEASE</springframework.version>  
  4.   </properties>  
  5.   
  6.   <dependencies>  
  7.     <dependency>  
  8.       <groupId>junit</groupId>  
  9.       <artifactId>junit</artifactId>  
  10.       <version>4.12</version>  
  11.       <scope>test</scope>  
  12.     </dependency>  
  13.     <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->  
  14.     <dependency>  
  15.       <groupId>org.springframework</groupId>  
  16.       <artifactId>spring-context</artifactId>  
  17.       <version>${springframework.version}</version>  
  18.     </dependency>  
  19.     <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->  
  20.     <dependency>  
  21.       <groupId>org.springframework</groupId>  
  22.       <artifactId>spring-test</artifactId>  
  23.       <version>${springframework.version}</version>  
  24.     </dependency>  
  25.   
  26.   </dependencies>  
  27.   <build>  
  28.     <plugins>  
  29.       <plugin>  
  30.         <groupId>org.apache.maven.plugins</groupId>  
  31.         <artifactId>maven-compiler-plugin</artifactId>  
  32.         <configuration>  
  33.           <source>1.8</source>  
  34.           <target>1.8</target>  
  35.           <encoding>utf-8</encoding>  
  36.         </configuration>  
  37.       </plugin>  
  38.       <plugin>  
  39.         <artifactId>maven-assembly-plugin</artifactId>  
  40.         <version>3.0.0</version>  
  41.         <configuration>  
  42.           <archive>  
  43.             <manifest>  
  44.               <mainClass>com.xueyou.demo</mainClass>  
  45.             </manifest>  
  46.           </archive>  
  47.           <descriptorRefs>  
  48.             <descriptorRef>jar-with-dependencies</descriptorRef>  
  49.           </descriptorRefs>  
  50.         </configuration>  
  51.         <executions>  
  52.           <execution>  
  53.             <id>make-assembly</id> <!-- this is used for inheritance merges -->  
  54.             <phase>package</phase> <!-- bind to the packaging phase -->  
  55.             <goals>  
  56.               <goal>single</goal>  
  57.             </goals>  
  58.           </execution>  
  59.         </executions>  
  60.       </plugin>  
  61.     </plugins>  
  62.   </build>  

整体看一下工程中的类和接口:


首先是Person类中有一个speak的方法,这个方法是MoveFactor这个借口提供的。Chinese、English和German都实现了这个接口。但是这三个类的@profile中的值是不同的。通过SpringTest中分配不同的activeprofile就能够实现调用不同的speak方法。

下面看代码:
MoveFactor.interface
[java]  view plain  copy
  1. package com.xueyou.demo;  
  2.   
  3.   
  4. public interface MoveFactor {  
  5.     void speak();  
  6. }  

Person.java
[plain]  view plain  copy
  1. package com.xueyou.demo;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component  
  7. public class Person {  
  8.   
  9.     @Autowired  
  10.     private MoveFactor moveFactor;  
  11.   
  12.     public void speak(){  
  13.         moveFactor.speak();  
  14.     }  
  15. }  


Chinese.java
[java]  view plain  copy
  1. package com.xueyou.demo;  
  2.   
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.context.annotation.Profile;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7.   
  8. @Configuration  
  9. @Profile(value = "dev")  
  10. @Component  
  11. public class Chinese implements MoveFactor {  
  12.     @Override  
  13.     public void speak() {  
  14.         System.out.println("我是中国人");  
  15.     }  
  16. }  

English.java
[java]  view plain  copy
  1. package com.xueyou.demo;  
  2.   
  3. import org.springframework.context.annotation.Profile;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component  
  7. @Profile("qa")  
  8. public class English implements MoveFactor{  
  9.     @Override  
  10.     public void speak() {  
  11.         System.out.println("i am an English");  
  12.     }  
  13. }  

German.java
[plain]  view plain  copy
  1. package com.xueyou.demo;  
  2.   
  3. import org.springframework.context.annotation.Profile;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6.   
  7. @Component  
  8. @Profile("prod")  
  9. public class German implements MoveFactor{  
  10.     @Override  
  11.     public void speak() {  
  12.         System.out.println("i am a German");  
  13.     }  
  14. }  

使用springtest进行测试
[plain]  view plain  copy
  1. package com.xueyou.demo;  
  2.   
  3. import org.junit.Test;  
  4. import org.junit.runner.RunWith;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.test.context.ActiveProfiles;  
  7. import org.springframework.test.context.ContextConfiguration;  
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  9.   
  10.   
  11. @RunWith(SpringJUnit4ClassRunner.class)  
  12. @ContextConfiguration(classes = App.class)  
  13. @ActiveProfiles("dev")  
  14. public class SpringTest {  
  15.   
  16.     @Autowired  
  17.     Person p;  
  18.   
  19.     @Test  
  20.     public void testProfile(){  
  21.         p.speak();  
  22.     }  
  23.   
  24. }  

运行结果:


当修改@ActiveProfile中的值时,输出的内容也会随之改变。



如果使用的是main函数进行真正的开发、测试和上线时,我们需要设置一下运行参数:


-D 后面加上需要设置的spring的属性,就能够在main函数中使用了。

App.java
[java]  view plain  copy
  1. package com.xueyou.demo;  
  2.   
  3. import org.springframework.context.ConfigurableApplicationContext;  
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  5. import org.springframework.context.annotation.ComponentScan;  
  6. import org.springframework.context.annotation.Configuration;  
  7.   
  8. /** 
  9.  * Hello world! 
  10. // */  
  11. @Configuration  
  12. @ComponentScan(basePackages = {"com.xueyou.demo"})  
  13. public class App {  
  14.     public static void main(String[] args) {  
  15.         ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(com.xueyou.demo.App.class);  
  16.         Person p = context.getBean(Person.class);  
  17.         p.speak();  
  18.     }  
  19. }  

运行结果:


如果需要得到当前的activeprofile可以通过ConfigurableApplicationContext的实例来的到。



最后提一下,如果是在web的后台项目中如何进行设置。这个时候我们通过xml的方式进行设置:
[html]  view plain  copy
  1. <context-param>  
  2.   <param-name>spring.profiles.active</param-name>  
  3.   <param-value>DOUBLEUPMINT</param-value>  
  4. </context-param>  

猜你喜欢

转载自blog.csdn.net/AlbenXie/article/details/80224548