Results 1 to 6 of 6

Thread: Spring Dependencies parameters

  1. #1
    Join Date
    Oct 2008
    Posts
    3

    Default Spring Dependencies parameters

    Hi,

    Just wondering if there is a way to do the following:

    I have a dependency between two objects ObjectA and ObjectB.

    In the current code the ObjectB is created before ObjectA and has a runtime parameter string attribute which has to be dynamic at runtime.

    What I want to do, is using the BeanFactory to get an instance of ObjectA already injected with ObjectB using constructor injection, the problem is how to get the runtime parameter into ObjectB

    Cheers
    Ben

  2. #2

    Default

    use @Value, passing in the spel expression that will evaluate to the dynamic value you need.
    -Amit

  3. #3
    Join Date
    Oct 2008
    Posts
    3

    Default

    Hi,

    I don't see how that will help Basically I have got the following and want to get rid of the factory altogether if possible.

    Code:
    public class ObjectFactory{
          ObjectA createObjectA(String dynamicValue){
             ObjectB objB = new ObjectB(dynamicValue);
             ObjectA ObjectA = new ObjectA(objB);
         }
    }
    Is this possible or am I missing something?

    Cheers
    Ben

  4. #4

    Default

    Of course the explanation may not help, coz your question was too abstract.

    How do you get the dynamic value? (post some code)
    Is is different for different instances of Object A?
    -Amit

  5. #5
    Join Date
    Oct 2008
    Posts
    3

    Default

    Hi,

    What I want to do is Declare the two Objects in the XML file as types prototype.

    Then I want to get A reference to ObjectA through the spring BeanFactory but passing in a single dynamic value to the constructors. The dynamic value can be anything really and is exactly that dynamic.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
     ">
        <bean class="com.somecomp.ObjectA" id="objectA" scope="prototype" lazy-init="true">
            <constructor-arg type="com.somecomp.ObjectB" ref="objectB"/>
        </bean>
    
        <!-- Message Managers -->
        <bean class="com.somecomp.ObjectB" id="objectB" scope="prototype" lazy-init="true">
            <constructor-arg type="java.lang.String" value="someValue"/>
        </bean>
    </beans>
    Then what I wanted to do was to replace the above "someValue" with my dynamic value and call
    Code:
    BeanFactory.getBean("objectA")
    but wanting to get whatever value I pass in or set into ObjectB.

    . It is not possible to pre-configure a list of beans in the XML

  6. #6

    Default

    The question still remains... how do you evaluate "someValue" at runtime?

    some pointers-
    Do read through the spring docs section 3. The IoC container to get some DI concepts cleared.
    Look at 3.4.7.1 Lookup method injection to see how you can supply different instances at runtime. This is where you need to put in the logic to evaluate "someValue" and return a new instance of ObjectB as needed...
    -Amit

Posting Permissions

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