4

in my applicationcontext.xml i have this :

       <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/dashboardsupervisor" />
    <property name="username" value="root" />
    <property name="password" value="1234" />
</bean>

here i am connecting with my database :

              ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "applicationContext.xml");
        MySQLRdbHelper rdbHelper = (MySQLRdbHelper)  
                    ctx.getBean("ManagerSupervisor");

What is want is to NOT read the password "1234" from my applicationcontext.xml and read it from some properties file in my local drive . as this will be running on different machines and every one have different passwords.

Can i achieve this .

thanks

3 Answers 3

5

Yes, you can, and the key to this is Springs PropertyPlaceholderConfigurer.

For example, you create a file on your file system called database.properties, containing your password (note, that you can also add more settings to this file, like username, JDBC url, etc).

jdbc.password=1234

Next, you need to declare a PropertyPlaceholderConfigurer bean and point it to the database.properties file:

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

Note that the path is interpreted as a classpath resource, unless it is prefixed with file:.

Finally, replace the configuration of your dataSource bean: replace

<property name="password" value="1234" />

with

<property name="password" value="${jdbc.password}" />
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for your help , I have changed my applicationcontext.xml now ,but its giving error , I edited my question , please have a look
i have the database.properties in my F drive with jdbc.password=1234
thanks , its saying "class path resource [F:/database.properties] cannot be opened because it does not exist":: whereas database.properties file is in my F drive
In your question it says /F:/database.properties. It should be F:/database.properties, so without the leading /. Also, the error message reads class path resource, so maybe the file should reside on the classpath; I cannot find evidence of that in the documentation, though.
oh, if it needs to be in classpath , then that will not serve the purpose.: i tried chaning it to F:/database.properties, but no luck
|
0

You could use profiles:

 GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
 ctx.getEnvironment().setActiveProfiles("dev1");
 ctx.load("*Context.xml");
 ctx.refresh();

 <bean id="database1" profile="dev1"/>
 <bean id="database2" profile="dev2">

5 Comments

Where does the password come from in this scenario?
@mthmulders you set it in each bean, each environment gets it's own bean, thus it's own password
So if I understand correctly, you have the password hardcoded in a bean?
@mthmulders yup, you did understand perfectly. 99% of the time that is not a problem at all. Also this is a suggestion, not a "DO IT!" ;)
I'm not a big fan of putting production passwords in source control... but I'm glad to see it's not a "DO IT!" ;)
0

The best way is to create the data source in application server and configure as below in application.xml

  <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
     <property name="jndiName"><value>YourDSName</value></property>
  </bean>

  <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
      .....
   </bean>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.