maven 整合 spring profiles功能
来源:互联网 发布:淘宝助理登录 编辑:程序博客网 时间:2024/05/19 02:18
spring为beans标签提供了profile功能,以便项目的开发和生成环境分离。
01
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
02
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
=
"http://www.springframework.org/schema/context"
03
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
04
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
05
06
<
beans
profile
=
"dev,test"
>
07
<
context:property-placeholder
location
=
"classpath:application.properties"
/>
08
09
<
bean
id
=
"dataSource"
class
=
"com.jolbox.bonecp.BoneCPDataSource"
destroy-method
=
"close"
>
10
<
property
name
=
"driverClass"
value
=
"${db.driver}"
/>
11
<
property
name
=
"jdbcUrl"
value
=
"${db.url}"
/>
12
<
property
name
=
"username"
value
=
"${db.username}"
/>
13
<
property
name
=
"password"
value
=
"${db.password}"
/>
14
<
property
name
=
"idleConnectionTestPeriodInMinutes"
value
=
"60"
/>
15
<
property
name
=
"idleMaxAgeInMinutes"
value
=
"240"
/>
16
<
property
name
=
"maxConnectionsPerPartition"
value
=
"30"
/>
17
<
property
name
=
"minConnectionsPerPartition"
value
=
"10"
/>
18
<
property
name
=
"partitionCount"
value
=
"3"
/>
19
<
property
name
=
"acquireIncrement"
value
=
"5"
/>
20
<
property
name
=
"statementsCacheSize"
value
=
"100"
/>
21
<
property
name
=
"releaseHelperThreads"
value
=
"3"
/>
22
</
bean
>
23
<
beans
profile
=
"production"
>
24
<
context:property-placeholder
ignore-resource-not-found
=
"true"
location
=
"classpath:application.properties,classpath:application-production.properties"
/>
25
26
<
bean
id
=
"dataSource"
class
=
"org.springframework.jndi.JndiObjectFactoryBean"
>
27
<
property
name
=
"jndiName"
value
=
"${db.jndi}"
/>
28
</
bean
>
29
</
beans
>
30
31
</
beans
>
以数据库为例,开发环境使用的是直接将配置写在项目的配置文件里面,而生产环境则使用了jndi。
切换profile可以写在web.xml里面:
1
<
context-param
>
2
<
param-name
>spring.profiles.active</
param-name
>
3
<
param-value
>dev</
param-value
>
4
</
context-param
>
不过得改web.xml,现在一般项目都使用maven来管理,maven也有profile,可以将它们结合起来。
01
<
properties
>
02
<
profile.active
>dev</
profile.active
>
03
</
properties
><
span
></
span
> <
build
>
04
<
defaultGoal
>install</
defaultGoal
>
05
<
resources
>
06
<
resource
>
07
<
directory
>src/main/resources</
directory
>
08
</
resource
>
09
<
resource
>
10
<
directory
>src/main/resources</
directory
>
11
<
filtering
>true</
filtering
>
12
<
includes
>
13
<
include
>**/*.properties</
include
>
14
</
includes
>
15
</
resource
>
16
</
resources
>
17
18
</
build
>
19
...
20
<
profiles
>
21
<
profile
>
22
<
id
>dev</
id
>
23
<
activation
>
24
<
activeByDefault
>true</
activeByDefault
>
25
</
activation
>
26
</
profile
>
27
<
profile
>
28
<
id
>test</
id
>
29
</
profile
>
30
<
profile
>
31
<
id
>production</
id
>
32
<
properties
>
33
<
profile.active
>production</
profile.active
>
34
<
profile.scope
>provided</
profile.scope
>
35
</
properties
>
36
</
profile
>
37
</
profiles
<span></
span
>
mvn install -Pproduction 就是发布生产版本。
然后我们需要在项目里面src resource里面的某个配置文件添加如:
1
profile.active=${profile.active}
这样maven在编译时会自动设置profile。最后就是设法让spring能够读取到我们的配置。我们的做法是自己实现ContextLoaderListener,里面读取这个properties文件,将spring profiles属性设置为我们需要的值。
1
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, activeProfile);
- maven 整合 spring profiles功能
- 【Spring之配置propertie资源文件】Maven整合spring profiles功能配置propertie资源文件更灵活、简单
- maven profiles
- maven profiles
- maven profiles
- maven profiles
- maven profiles
- Maven Profiles
- maven项目 spring-boot 整合 mybatis 实现查询功能demo
- spring profiles
- maven+spring+mybatis整合
- Maven + Spring 整合 Mybatis
- SpringMVC+spring+maven整合
- MyBatis、Spring、maven整合
- spring+mybatis+maven整合
- Spring+Hibernate+maven整合
- Shiro整合Spring maven
- Maven + Spring 整合 Mybatis
- hibernate阻抗不匹配
- “老友记”中经典高频美语口语
- perl删除文件中的重复行
- c++预处理 宏
- 关于心智,自制力和大脑意识之间关系的一些思考
- maven 整合 spring profiles功能
- 解决ASP.NET中文乱码
- 在 Windows 上安装和使用 GNUstep 和 Objective-C
- Android模拟器不能上网的问题之解决方法
- 回顾毕业后经历的那些事
- windows boost 编译, 我用的版本是 boost_1_54_0
- Android UI组件开源实例(二)
- java如何进行垃圾回收的
- 安装SQL 2005 出现警告 32位ASP.NET已经注册,需要注册64位……解决方案