xml中直接读取properties键值的解决方式
2017-05-12 00:19
246 查看
spring 框架的xml文件如何读取properties文件数据
第一步:在spring配置文件中
注意:value可以多配置几个properties文件
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/db.properties</value>
</list>
</property>
</bean>
或者
<context:property-placeholder
location="classpath:db.properties"
/>
第二步:
在src目录下面建立db.properties文件
user=sa
password=sa
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName=DB1
第三步:
在spring的配置文件中通过EL表达式的形式调用
${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配置文件中
注意:value可以多配置几个properties文件
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/db.properties</value>
</list>
</property>
</bean>
或者
<context:property-placeholder
location="classpath:db.properties"
/>
第二步:
在src目录下面建立db.properties文件
user=sa
password=sa
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName=DB1
第三步:
在spring的配置文件中通过EL表达式的形式调用
${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>
相关文章推荐
- 解决JAVA,properties loadFromXML读取中文XML乱码的方法
- Tomcat启动读取applicationContext.xml出错的解决方式
- Properties中文键值读取乱码解决随记
- spring基于xml加载和读取properties配置文件的方式
- 【java】common-configuration读取xml,properties文件&问题解决
- 项目开发中经常要用到的知识----Java读取.Properties,.Xml,.Ini文件(三)
- XMLHttpRequest读取中文网页时返回乱码的解决办法
- ie无法直接打开xml 文件的解决方法:
- 在SQL2005里直接读取XML并解析其内容
- 项目开发中经常要用到的知识----Java读取.Properties,.Xml,.Ini文件(一)
- xmlHttpRequest 以Post方式发数据到Asp.net页,在gb2312编码下的解决办法
- Flash读取XML文件最直接的方法
- Spring中从Classpath路径中读取XML定义文件的几种方式
- IE中点击链接下载EXCEL文件直接以乱码方式打开解决
- 直接读取XML结点属性值
- 使用javascript以键值对的方式读取查询字符串【QueryString】
- 使用common-configuration读取xml,properties文件
- xml 中的冒号 读取问题的解决
- XMLHttpRequest读取中文网页时返回乱码的解决办法
- 读取配置参数的properties文件,路径获取完美解决