p0tta
p0tta

Reputation: 1651

Keep DB passwords out of spring-servlet.xml file

Here is how my DB config looks currently

<beans:bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="packagesToScan" value="com.xxx.test" />
    <beans:property name="jpaVendorAdapter">
        <beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <beans:property name="generateDdl" value="true" />
        </beans:bean>
    </beans:property>
</beans:bean>

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
        value="jdbc:mysql://ip-xxxx.ec2.xxx.com:3306/db" />
    <beans:property name="username" value="user" />
    <beans:property name="password" value="password" />
</beans:bean >

I want to take the DB properties out of here and put them in tomcat's web.xml file or even from a config file. I am using Spring JPA. How do I do it?

Upvotes: 1

Views: 364

Answers (1)

Sadham Hussain
Sadham Hussain

Reputation: 74

add this to yout context file!

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="config.properties">
    </property>
</bean>

create config.properties in src/main/resources and store user name password as

 mongodb.username=mongodbuser
 mongodb.password=morebites

now you can refer username and password as

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
    value="jdbc:mysql://ip-xxxx.ec2.xxx.com:3306/db" />
<beans:property name="username" value="${mongodb.username}" />
<beans:property name="password" value="${mongodb.password}" />

Upvotes: 3

Related Questions