I have some code where properties are only know during runtime.
in this case a user can enter a URL and username and password.
ConnectionFactory cf = (ConnectionFactory) this.factory.getBean(
"topicConnectionFactory", <URL which is entered at runtime>)
final UserCredentialsConnectionFactoryAdapter connectionFactory = this.factory
.getBean(UserCredentialsConnectionFactoryAdapter.class);
connectionFactory.setTargetConnectionFactory(cf);
The UserCredentialsConnectionFactoryAdapter doesn’t have constructor base setup, so the only method is to create using no-arg constructor and set the properties after, but spring runs the InitializingBean which throws an error that the TargetConnectionFactory is not set.
Is there a way to either pass this information during spring creation or for spring delay the Initializing of the Bean.
For the purpose of understanding the scope, below is the section of the configuration for spring.
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"
scope="prototype" />
<bean id="userCredentialsConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter"
scope="prototype" >
</bean>
<bean id="cachingFactory"
class="org.springframework.jms.connection.CachingConnectionFactory"
scope="prototype" />
<bean id="tibcoTopicConnectionFactory" class="com.tibco.tibjms.TibjmsTopicConnectionFactory"
scope="prototype" />
As mentioned, the url, username and password is not know until runtime and can changed. the application is multi threaded. idea is to connect to somewhere, send message, disconnect
You can do the following:
Writing your own extension of
UserCredentialsConnectionFactoryAdaptertaking theConnectionFactoryas a constructor argument:and then using it like this:
Don’t forget to change your bean decaration: