【问题标题】:configuring db properties in servlet-context and spring-security xml files in spring在 spring 中配置 servlet-context 和 spring-security xml 文件中的 db 属性
【发布时间】:2014-07-30 05:32:47
【问题描述】:

数据库配置:

servlet-context.xml

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

spring-security.xml

<beans:bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url" value="jdbc:mysql://localhost:3306/rni" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="root" />
</beans:bean>

在 servlet-context.xml 我从属性文件配置数据库属性

我需要在 spring-security.xml 中使用的相同属性

请告诉我如何接近?

【问题讨论】:

  • 为什么需要复制dataSource的配置?

标签: java xml spring spring-mvc


【解决方案1】:

由于两者都由 spring 的单个容器管理,因此您也可以在 spring-security.xml 中使用 EL

<beans:bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url" value="jdbc:mysql://localhost:3306/rni" />
    <beans:property name="username" value="${jdbc.username}" />
    <beans:property name="password" value="${jdbc.password}" />
</beans:bean> 

但是我不确定为什么需要两个数据源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 2014-12-07
    • 1970-01-01
    • 2017-03-03
    • 2014-01-24
    • 2017-02-15
    • 2020-03-05
    • 2014-02-25
    相关资源
    最近更新 更多