简体   繁体   中英

configuring db properties in servlet-context and spring-security xml files in spring

DB Configuration :

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>

In servlet-context.xml i configured database properties from properties file

Same properties i need to use in spring-security.xml

please tell me the way how to approach?

Since both are managed by spring's single container, you can use the EL in spring-security.xml as well

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

However I'm not sure why you need two datasources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM