Announcement Announcement Module
Collapse
No announcement yet.
Avoidng Hardcode values for DB Connection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">

    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDrive r"/>

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

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

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

    </bean>

  • #2
    See the PropertyPlaceHolderConfigurer in the Spring Documentation.

    Comment


    • #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.

      Code:
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations"> 
      <value>classpath:com/mycompany/abc/def/jdbc.properties</value>
      </property>
      </bean>
      
      <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}"/>
      </bean>

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

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

      Comment


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

        /WEB-INF/myfile.proeprties

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

        classpath:/myfile.proeprties
        classpath:/my/package/myfile.properties

        If you are reading the file directly using the file system

        file:/c:/myfile.properties

        Hope this helps

        Comment


        • #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.

          Comment


          • #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, 01:06 PM.

            Comment

            Working...
            X