对于想了解在spring-context.xml和persistence.xml中加载.properties的读者,本文将提供新的信息,我们将详细介绍spring加载xml,并且为您提供关于1.Spr
对于想了解在spring-context.xml和persistence.xml中加载.properties的读者,本文将提供新的信息,我们将详细介绍spring 加载xml,并且为您提供关于1. Spring 基于 xml 加载和读取 properties 文件配置、ApplicationContext.xml中加载properties配置文件、applicationContext.xml中设置读取jdbc.properties文件、applicationContext.xml读取properties中属性的有价值信息。
本文目录一览:- 在spring-context.xml和persistence.xml中加载.properties(spring 加载xml)
- 1. Spring 基于 xml 加载和读取 properties 文件配置
- ApplicationContext.xml中加载properties配置文件
- applicationContext.xml中设置读取jdbc.properties文件
- applicationContext.xml读取properties中属性
在spring-context.xml和persistence.xml中加载.properties(spring 加载xml)
是否有办法在spring-context.xml和JPA persistence.xml中引用.properties文件?
我想我在春季上下文文件中已经看到过这样的例子,尽管我不记得那是哪里。也许有人知道吗?关于persistence.xml,我实际上不确定这是否奏效。
我的目标是在开发和发行配置之间更改某些属性。我目前的想法是通过模板配置中的ant手动替换文件中的所有属性。尽管应该有更好的方法来做到这一点。:)
答案1
小编典典您可以使用PropertyPlaceholderConfigurer从Spring bean定义文件中引用外部属性文件。我认为这不适用于JPA
persistence.xml,尽管Spring的JPA支持允许您将persistence.xml的大部分(如果不是全部)内容合并到bean文件本身中,在这种情况下它将很好地工作。
1. Spring 基于 xml 加载和读取 properties 文件配置
在 src 目录下,新建 test.properties 配置文件,内容如下
name=root
password=123456
logArchiveCron=0/5 * * * * ?
一种是使用 spring 提供的一个标签,在 spring-config.xml 中配置单个 properties,如下
<context:property-placeholder location="classpath:test.properties"/>
配置多个 properties 通过分号隔开在后面添加即可,例如
<context:property-placeholder location="classpath:test.properties,classpath:jdbc.properties"/>
另一种是通过注到 bean 的属性来进行配置,这里也是配置多个,如下
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:test.properties</value>
</list>
</property>
</bean>
取值方式如下:
在 spring-config.xml 中获取 properties 文件中的值:
<task:scheduled-tasks>
<task:scheduled ref="testJob" method="logArchiveBak" cron="${logArchiveCron}" />
</task:scheduled-tasks>
在 java 类里面获取 properties 文件里面的值,通过注解 @value (${key}) 来获取
@Value("${name}")
private String name;
@Value("${password}")
private String password;
亲测可用。
ApplicationContext.xml中加载properties配置文件
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/abc.properties" />
在Maven项目中,该配置文件在/src/main/resources/下
之后比如配置数据库连接,方言等,可以直接用EL表达式来配置
eg:
数据源配置 <bean id="dataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
applicationContext.xml中设置读取jdbc.properties文件
参考:http://blog.csdn.net/wu631464569/article/details/51898871
一、 jdbc.properties的主要内容:
里面的内容主要是数据库连接和配置的一些基本信息。
- #JDBCConfiguration
- jdbcDriverClassName=com.MysqL.jdbc.Driver
- jdbcUrl=jdbc:MysqL://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
- hibernate.dialect=org.hibernate.dialect.MysqL5InnoDBDialect
- jdbcUsername=root
- jdbcPassword=root
- #DBCPPoolsettings
- jdbcInitialSize=5
- jdbcMaxActive=10
- jdbcMaxIdle=jdbcMaxWait=30000
- jdbcValidationQuery=select1
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <!-- 配置C3P0连接池: --> <bean id="dataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!--每5小时检查所有连接池中的空闲连接。防止MysqL wait_timeout(默认的为8小时) --> <property name="idleConnectionTestPeriod" value="${jdbc.c3p0.idleConnectionTestPeriod}"/> </bean>
applicationContext.xml读取properties中属性
载入方式一:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> <context:property-placeholderlocation="classpath:uc.properties"/> </beans>
载入方式二:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> <beanid="configBean"https://www.jb51.cc/tag/fig/" target="_blank">fig.PropertyPlaceholderConfigurer"> <propertyname="locations"> <value>uc.properties</value> </property> </bean> </beans>
使用方式:
<value>${name}</value> 或者 <propertyname="driverClass"value="${driverClass}"/>
如果有多个properties文件的情况
<beanid="configBean"https://www.jb51.cc/tag/fig/" target="_blank">fig.PropertyPlaceholderConfigurer"> <propertyname="locations"> <list> <value>hello.properties</value> <value>welcome.properties</value> <value>other.properties</value> </list> </property> </bean>
今天的关于在spring-context.xml和persistence.xml中加载.properties和spring 加载xml的分享已经结束,谢谢您的关注,如果想了解更多关于1. Spring 基于 xml 加载和读取 properties 文件配置、ApplicationContext.xml中加载properties配置文件、applicationContext.xml中设置读取jdbc.properties文件、applicationContext.xml读取properties中属性的相关知识,请在本站进行查询。
本文标签: