Results 1 to 7 of 7

Thread: How to inject JndiObjectFactoryBean?

  1. #1

    Default How to inject JndiObjectFactoryBean?

    I have this in my context:

    <bean id="queueSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="jms/foo/TopicConnectionFactory" />
    <property name="resourceRef" value="true" />
    </bean>

    I want to inject that into my other bean:

    <bean id="test" class="com.acme.Foo">
    <constructor-arg ref="queueSource" />
    </bean>

    The queueSource is looking at an ActiveMQConnectionFactory defined in JNDI. Basically what I really need is for the "com.acme.Foo" object to have the broker URL of the connection factory. However, javax.jms.ConnectionFactory interface doesn't provide access to it and I didn't want to change my code to use an ActiveMQConnectionFactory (i.e. trying to stick to using interfaces), but I need the broker URL.

    So I thought I'd pass in the "queueSource" of type JndiObjectFactoryBean so I could get the info I needed without directly referencing ActiveMQ in my code....but I get an error because Spring is trying to pass my constructor the ActiveMQConnectionFactory instead of the JndiObjectFactoryBean.

    How can I get the JndiObjectFactoryBean object?

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by codecraig View Post
    ...

    So I thought I'd pass in the "queueSource" of type JndiObjectFactoryBean so I could get the info I needed without directly referencing ActiveMQ in my code....but I get an error because Spring is trying to pass my constructor the ActiveMQConnectionFactory instead of the JndiObjectFactoryBean.

    How can I get the JndiObjectFactoryBean object?

    3.7.3. Customizing instantiation logic using FactoryBeans:
    ...
    Finally, there is sometimes a need to ask a container for an actual FactoryBean instance itself, not the bean it produces. This may be achieved by prepending the bean id with '&' (sans quotes) when calling the getBean method of the BeanFactory (including ApplicationContext). So for a given FactoryBean with an id of myBean, invoking getBean("myBean") on the container will return the product of the FactoryBean, but invoking getBean("&myBean") will return the FactoryBean instance itself.
    However, I see couple of drawbacks at your approach:
    1. Your code is coupled to the Spring if it works in terms of JndiFactoryBean class;
    2. It's not convenient to store configuration details at IoC config (I mean data like connection factory jndi name);


    You can resolve that by:
    1. Introducing a separate properties file that holds jndi names;
    2. Using PropertyPlaceHolderConfigurer to inject the property file data to the context;
    3. Introducing a separate string property at the Foo class that is filled with the same property as JndiObjectFactoryBean;

  3. #3

    Default

    thanks for the help!

  4. #4

    Default

    That works if my code calls "getBean"...but how do I actually inject the factory bean?

    I naively tried:

    <constructor-arg ref="&queueSource" />

    Of course that didn't work.

  5. #5
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    You're not allowed to use '&' symbol directly at the xml. It should looks like the following:

    HTML Code:
    <constructor-arg ref="&amp;queueSource" />
    Anyway, it's bad to inject JndiObjectFactoryBean object only for the purpose of the property retrieval. It's much more better to inject the property itself to the bean that needs it. You can guarantee that factory bean and interested bean use the same property value if it is defined at external properties file and is injected to them via PPC.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Just another question, why do you need the brokerUrl? You already have the ConnectionFactory?! Do you want to bypass it or construct a new one?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    well, i was going to say why i needed it, but just realized the object that needed that property could be created as a spring bean and injected into the class I need it for.


Posting Permissions

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