a. Is there any issue in initializing the static instance of LdapTemplate class in this way and using it in the static methods of the class. ( In particular is there any issue in using static instance of ldapTemplate?)
Kitty
No, there should be no issue with static references to LdapTemplate instances. Normally, LdapTemplate instances are being created as "singleton scoped" beans by Spring, so there is only one instance anyway.
b. How do we initialize static instances in spring bean configurations? Is there any generic way ( Most configurations I see are either constructor init or setters for instance variables )
Kitty
Let me first state my opinion here. It's not a good idea to have static utility methods if they need an external collaborator. It's OK to have static utility methods if they don't need any state whatsoever, apart from what they get passed as arguments, but as soon as they need to share something, they have lost their "right" to be static. Make them all non-static and inject an LdapTemplate as per normal, that's my advice.
Anyway, if you persist, there is a way to get Spring to wire up your bean. You write a static setter for LdapTemplate:
Code:
package com.example;
public class MyUtilities {
private static LdapTemplate ldapTemplate;
...
public static void setLdapTemplate(LdapTemplate ldapTemplate) {
MyUtilities.ldapTemplate = ldapTemplate;
}
}
Then you write the following bean definition in your config file:
Code:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="com.example.MyUtilities.setLdapTemplate" />
<property name="arguments" ref="ldapTemplate" />
</bean>