Announcement Announcement Module
Collapse
No announcement yet.
Reference Bean Property within Reference Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reference Bean Property within Reference

    Hi there,
    Hoping someone can help on this one. I want to pass intercept a password and decode it, from a properties file, at runtime. I've written a decoder class which is going to do this, and can happily return a String containing the decoded password. I then want to wire this PasswordDecoder into the BasicDatasource definition I'm using. So, it goes something like this...

    Code:
        <bean id="localDbDataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${local.driver}"/>
            <property name="url" value="${local.url}"/>
            <property name="username" value="${local.username}"/>
            <property name="password">
              <ref bean="localDatabasePassword"/>
            </property>
        </bean>
    
        <bean id="localDatabasePassword" class="java.lang.String">
        	<constructor-arg index="0" ref="localPasswordDecoder"/>
        </bean>
    
        <bean id="localPasswordDecoder" class="com.my.utils.PasswordDecoder"/>
    The problem here of course is that there is no contructor for String that takes an arguement of type PasswordDecoder. So what I need to be able to do is to reference a property within the reference!? Make sense?

    i.e. Something like...

    Code:
        <bean id="localDatabasePassword" class="java.lang.String">
        	<constructor-arg index="0" ref="localPasswordDecoder" property="password"/>
        </bean>
    Can someone let me know whether this is possible - because I don't seem to be able to find away - but can't see why this should be the case in such a versitile framework!

    Thanks in advance for any pointer.

  • #2
    For anyone who's interested in doing this, and hasn't yet found the factory bean and method construct, this is the way to handle this type of inversion...

    http://www.springframework.org/docs/...factory-method

    So, using the above as an example, we end up with something like...

    Code:
        <bean id="localDbDataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${local.driver}"/>
            <property name="url" value="${local.url}"/>
            <property name="username" value="${local.username}"/>
            <property name="password">
              <ref bean="localDatabasePassword"/>
            </property>
        </bean>
    
        <bean id="localPasswordDecoder" class="com.my.util.PasswordDecoder">
        	<constructor-arg index="0" value="${local.password}"/>
    		<constructor-arg index="1" value="${security.key}"/>
        </bean>
        
    	<bean id="localDatabasePassword"
              factory-bean="localPasswordDecoder"
              factory-method="getPassword"/>
    Hope this helps someone...

    Comment


    • #3
      An alternative would be to use a MethodInvokingFactoryBean to invoke the decode method. You could then use the return value of that as property value.

      Regards,
      Andreas

      Comment


      • #4
        You can make nested references in config files (as long as the bean contains a proper getter for the property being accessed), so the following should work:

        Code:
                <property name="password">
                  <ref bean="localDatabasePassword.password"/>
                </property>

        Comment

        Working...
        X