Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Wiring a bean from a database

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Location
    Miami, FL
    Posts
    3

    Default Wiring a bean from a database

    I am wiring a bean with the port values from our developer machines. I would like to retrieve the value from a database. I think a custom property editor would be the right way to accomplish this. Is there another way of accomplishing this?

    Thanks,
    Bobby

  2. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    You could use a PropertyPlaceholderConfigurer to externalize the properties. And there is another (spring) project that has created a few implementation (not only property files, but database access aswel), but I can`t find the link.

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Not sure if it helps, but Spring Modules provides integration with Commons Configuration.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  4. #4
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    The Spring modules factory bean looks interesting, but I couldn't find any documentation (except the extremely minimal javadocs). Could you add a section / example to the main HTML docs?

  5. #5
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Sure - can you please raise an issue on JIRA to keep track of it? Thanks.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  6. #6
    Join Date
    Dec 2006
    Posts
    17

    Default Getting an error

    I tried uisng the above setup for a properties configurer. i get the following e rror
    org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'propertiesArray' of bean class [org.springframework.beans.factory.config.PropertyP laceholderConfigurer]: Bean property 'proper
    tiesArray' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?

    Any idea why this is happening?
    Regards,
    Deepa

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I would presume as the method does exist, the arguments being passed in don't match the expected ones. It expects Properties[].

    http://static.springframework.org/sp...perties[])

  8. #8

    Default

    Does anyone know where this is? I need it too!

    cheers,

    David

  9. #9
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Here is a configuration file which loads properties from a database using Apache Commons Configuration (whose connection properties are loaded from db.properties). Since I'm using two PropertyPlaceholderConfigurer, I needed to use a different placeholderPrefix for the file based one. That way the properties in the rest of the context uses normal syntax:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="db.properties"/>
    		<property name="placeholderPrefix" value="$&#123;init&#58;"/>
    	</bean>
    	
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="properties">
    			<bean factory-class="org.apache.commons.configuration.ConfigurationConversion"
    	      		  factory-method="getProperties">
    		  		<constructor-arg>
    					<bean class="org.apache.commons.configuration.DatabaseConfiguration">
    				  		<constructor-arg>
    			   				<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    						      <property name="driverClassName" value="$&#123;init&#58;jdbc.driver&#125;"/>
    						      <property name="url" value="$&#123;init&#58;jdbc.url&#125;"/>
    						      <property name="username" value="$&#123;init&#58;jdbc.username&#125;"/>
    						      <property name="password" value="$&#123;init&#58;jdbc.password&#125;"/>
    						    </bean> 	
    						</constructor-arg>
      					    <constructor-arg value="$&#123;init&#58;table&#125;"/>
     					    <constructor-arg value="$&#123;init&#58;keyColumn&#125;"/>
    					    <constructor-arg value="$&#123;init&#58;valueColumn&#125;"/>
    				    </bean>
    				 </constructor-arg>
    			</bean>
    		</property>
    	</bean>
    </beans>

  10. #10
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Actually because of the recursive nature of my solution, you have to break it up into two contexts, with the bootstrap.xml being the parent to context.xml

    bootstrap.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="db.properties"/>
    	</bean>
    
    	<bean id="myProps" class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
      		<constructor-arg>
    			<bean class="org.apache.commons.configuration.DatabaseConfiguration">
    		  		<constructor-arg>
    					<bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    				      <property name="driverClassName" value="$&#123;jdbc.driver&#125;"/>
    				      <property name="url" value="$&#123;jdbc.url&#125;"/>
    				      <property name="username" value="$&#123;jdbc.username&#125;"/>
    				      <property name="password" value="$&#123;jdbc.password&#125;"/>
    				    </bean> 	
    		  		</constructor-arg>
    			    <constructor-arg value="$&#123;config.table&#125;"/>
    			    <constructor-arg value="$&#123;config.keyColumn&#125;"/>
    			    <constructor-arg value="$&#123;config.valueColumn&#125;"/>
    		    </bean>
    		 </constructor-arg>
    	</bean>
    		
    </beans>
    context.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="properties" ref="myProps"/>
    	</bean>
    
    	<!-- Beans which use the db based properties goes here -->
    </beans>

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

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