spring @profile注解的使用方法

2025-05-29 0 49

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

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

那么,spring中的@profile注解的作用就体现在这里。在spring使用di来依赖注入的时候,能够根据当前制定的运行环境来注入相应的bean。最常见的就是使用不同的datasource了。

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

首先是新建maven工程

mvn archetype:generate -darchetypecatalog=internal

下面是pom文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62
<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>

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

spring @profile注解的使用方法

首先是person类中有一个speak的方法,这个方法是movefactor这个借口提供的。chinese、english和german都实现了这个接口。但是这三个类的@profile中的值是不同的。通过springtest中分配不同的activeprofile就能够实现调用不同的speak方法。

下面看代码:
movefactor.interface

?

1

2

3

4

5

6
package com.xueyou.demo;

public interface movefactor {

void speak();

}

person.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
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进行测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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

}

}

运行结果:

spring @profile注解的使用方法

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

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

spring @profile注解的使用方法

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

app.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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

}

}

运行结果:

spring @profile注解的使用方法

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

spring @profile注解的使用方法

最后提一下,如果是在web的后台项目中如何进行设置。这个时候我们通过xml的方式进行设置:

?

1

2

3

4
<context-param>

<param-name>spring.profiles.active</param-name>

<param-value>doubleupmint</param-value>

</context-param>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/wild46cat/article/details/71189858

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 spring @profile注解的使用方法 https://www.kuaiidc.com/114140.html

相关文章

发表评论
暂无评论