【Spring之配置propertie资源文件】Maven整合spring profiles功能配置propertie资源文件更灵活、简单
来源:互联网 发布:sql赋值 编辑:程序博客网 时间:2024/05/20 14:20
spring 框架的xml文件如何读取properties文件数据
第一步:在spring配置文件中
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/db.properties</value> </list> </property> </bean>
第二步:
user=sapassword=sadriver=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc:sqlserver://localhost:1433;databaseName=DB1
第三步:
${user}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/db.properties</value> </list> </property> </bean> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"> </property> <property name="url" value="${url}"> </property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="datasource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>entity/Users.hbm.xml</value> </list> </property> </bean> <bean id="UsersDAO" class="dao.UsersDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </beans>
spring为beans标签提供了profile功能,以便项目的开发和生成环境分离。
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
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<
beans
profile
=
"dev,test"
>
<
context:property-placeholder
location
=
"classpath:application.properties"
/>
<
bean
id
=
"dataSource"
class
=
"com.jolbox.bonecp.BoneCPDataSource"
destroy-method
=
"close"
>
<
property
name
=
"driverClass"
value
=
"${db.driver}"
/>
<
property
name
=
"jdbcUrl"
value
=
"${db.url}"
/>
<
property
name
=
"username"
value
=
"${db.username}"
/>
<
property
name
=
"password"
value
=
"${db.password}"
/>
<
property
name
=
"idleConnectionTestPeriodInMinutes"
value
=
"60"
/>
<
property
name
=
"idleMaxAgeInMinutes"
value
=
"240"
/>
<
property
name
=
"maxConnectionsPerPartition"
value
=
"30"
/>
<
property
name
=
"minConnectionsPerPartition"
value
=
"10"
/>
<
property
name
=
"partitionCount"
value
=
"3"
/>
<
property
name
=
"acquireIncrement"
value
=
"5"
/>
<
property
name
=
"statementsCacheSize"
value
=
"100"
/>
<
property
name
=
"releaseHelperThreads"
value
=
"3"
/>
</
bean
>
<
beans
profile
=
"production"
>
<
context:property-placeholder
ignore-resource-not-found
=
"true"
location
=
"classpath:application.properties,classpath:application-production.properties"
/>
<
bean
id
=
"dataSource"
class
=
"org.springframework.jndi.JndiObjectFactoryBean"
>
<
property
name
=
"jndiName"
value
=
"${db.jndi}"
/>
</
bean
>
</
beans
>
</
beans
>
以数据库为例,开发环境使用的是直接将配置写在项目的配置文件里面,而生产环境则使用了jndi。
切换profile可以写在web.xml里面:
1
2
3
4
<
context-param
>
<
param-name
>spring.profiles.active</
param-name
>
<
param-value
>dev</
param-value
>
</
context-param
>
不过得改web.xml,现在一般项目都使用maven来管理,maven也有profile,可以将它们结合起来。
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
<
properties
>
<
profile.active
>dev</
profile.active
>
</
properties
><
span
></
span
> <
build
>
<
defaultGoal
>install</
defaultGoal
>
<
resources
>
<
resource
>
<
directory
>src/main/resources</
directory
>
</
resource
>
<
resource
>
<
directory
>src/main/resources</
directory
>
<
filtering
>true</
filtering
>
<
includes
>
<
include
>**/*.properties</
include
>
</
includes
>
</
resource
>
</
resources
>
</
build
>
...
<
profiles
>
<
profile
>
<
id
>dev</
id
>
<
activation
>
<
activeByDefault
>true</
activeByDefault
>
</
activation
>
</
profile
>
<
profile
>
<
id
>test</
id
>
</
profile
>
<
profile
>
<
id
>production</
id
>
<
properties
>
<
profile.active
>production</
profile.active
>
<
profile.scope
>provided</
profile.scope
>
</
properties
>
</
profile
>
</
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);
实际环境,也如此:
<profiles><profile><id>dev</id><properties><profile.id>dev</profile.id></properties><!-- 配置POM.xml配置资源文件,可根据测试、线上都不同环境指向相应资源文件,这种方式替换了直接在applicationContext中配置。 --><activation><activeByDefault>true</activeByDefault></activation><build><filters><filter>src/main/properties/dev-local.properties</filter></filters></build></profile></profiles>
0 0
- 【Spring之配置propertie资源文件】Maven整合spring profiles功能配置propertie资源文件更灵活、简单
- log4j.propertie配置详解
- propertie文件的使用
- maven 整合 spring profiles功能
- spring ApplicationContext.xml之ResourceBundleMessageSource配置资源文件
- Spring - 资源文件properties的配置
- Spring 资源文件 properties 的配置
- Spring - 资源文件properties的配置
- Spring - 资源文件properties的配置
- Spring - 资源文件properties的配置
- 使用资源文件配置 Spring数据库连接
- spring mvc配置静态资源文件
- Spring加载资源文件的配置
- Maven 资源文件的配置
- 遍历Propertie属性文件所有内容
- Spring配置资源文件的两种方式
- Spring中多个工程上多个资源文件ignoreUnresolvablePlaceholders配置
- spring-boot 加载本地静态资源文件路径配置
- 装完XAMPP之后如何进行php网站的建设
- 图解TCP-IP协议
- Android一键开启远程ADB调试
- 开源3D打印软件ReplicatorG的源代码的获取与配置
- tail
- 【Spring之配置propertie资源文件】Maven整合spring profiles功能配置propertie资源文件更灵活、简单
- 测试
- Xcode 中的小技巧
- 前进的路上,我们永不止步
- 我对 rest 以及 restful 的一些粗浅的理解
- 天猫11.11:多终端下的一致性体验
- Redis
- JS代码验证用户身份证
- Android的onCreateOptionsMenu()创建菜单Menu详解