Results 1 to 6 of 6

Thread: Avoidng Hardcode values for DB Connection

Hybrid View

  1. #1
    Join Date
    Jun 2007
    Posts
    8

    Default 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. #2

    Default

    See the PropertyPlaceHolderConfigurer in the Spring Documentation.

  3. #3
    Join Date
    Jun 2007
    Posts
    8

    Thumbs down

    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.

  4. #4

    Default

    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

  5. #5

    Default

    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. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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 at 12:06 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •