sprng中使用Profile注解

Spring框架中使用profile注解实现根据环境条件开启特定功能

1、Profile;

1
2
3
4
5
6
7
@Component
//spring.profiles.active = test\pro\pre 此实体类就会生效
@Profile({"test", "pro", "pre"})
//在虚拟机参数位置加载 -Dspring.profiles.active=test
public class xx {

}

2.spring框架中可使用active参数的配:不配置默认为default

1.jvm参数的形式指定active的值:

1
2
3

-Dsrping.profiles.active=test

2.在web.xml中配置:

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
方式二或三选择其一即可
方式一:
<!-- 在上下文context-param中设置profile.default的默认值 -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</context-param>
方式二:
<!-- 在上下文context-param中设置profile.active的默认值 -->
<!-- 设置active后default失效,web启动时会加载对应的环境信息 -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>development</param-value>
</context-param>
方式三:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 在DispatcherServlet参数中设置profile的默认值,active同理 -->
<init-param>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>