spring 框架的xml文件如何讀取properties文件數據


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>

第二步:

  在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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM