Announcement Announcement Module
No announcement yet.
Avoidng Hardcode values for DB Connection Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Avoidng Hardcode values for DB Connection

    I am new to Springs and right now doing my first project. I have no formal training in springs and just doing fire fighting.

    I am having a minor issue. Right now I have hard coded all connection properties in my applicationcontext.xml but I want to avoid this situation. Is there any other way, I can read from text file or java.util.Properties and populate in applicationContext at server startup. We would be deploying our application as desktop application and we donot want people to go and change values in xml file.

    Current Code from ApplicationContext.xml:

    <bean id="dataSource2"

    <property name="driverClassName" value=" r"/>

    <property name="url" value="jdbc:sqlserver://MYIP:MYPORT;database=CPIW"/>

    <property name="username" value="aaa"/>

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


  • #2
    See the PropertyPlaceHolderConfigurer in the Spring Documentation.


    • #3
      I tried following but I am getting io exception that file could not be opened because it is not found. I copied the properties file in my src folder in designated package.

      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations"> 
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">	
      		<property name="driverClassName" value="${driverClassName}"/>
      		<property name="url" value="${driverClassName}"/>
      		<property name="username" value="${driverClassName}"/>
      		<property name="password" value="${driverClassName}"/>

      I also tried following and hard coded the location and got the same problem

      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations"> 
      Any idea what am I doing wrong ? Any help is greatly appreciated.


      • #4
        It depends on how are you reading the spring context file.

        If you in a web application, the resource can be specified as a relative URL


        Or you can put the the properties file in a JAR file or in the WEB-INF/classes directory and use the classpath syntax


        If you are reading the file directly using the file system


        Hope this helps


        • #5
          Another idea is to configure the data source in the application server.
          That way you can have different databases locally and in the production environment.


          • #6
            Just wondered how are you bootstrapping the application?
            A bean factory post-processor is executed manually (in the case of a BeanFactory) or automatically (in the case of an ApplicationContext) to apply changes of some sort to the configuration metadata that defines a container. Spring includes a number of pre-existing bean factory post-processors, such as PropertyResourceConfigurer and PropertyPlaceholderConfigurer, both described below, and BeanNameAutoProxyCreator, which is very useful for wrapping other beans transactionally or with any other kind of proxy, as described later in this manual. The BeanFactoryPostProcessor can be used to add custom property editors.
            Last edited by karldmoore; Aug 29th, 2007, 12:06 PM.