Announcement Announcement Module
Collapse
No announcement yet.
How to inject JndiObjectFactoryBean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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;

    Comment


    • #3
      thanks for the help!

      Comment


      • #4
        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.

        Comment


        • #5
          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.

          Comment


          • #6
            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?

            Comment


            • #7
              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.

              Comment

              Working...
              X