Using Spring Beans from Non-Spring Classes

The idea is to have some static field holds the references like:

   <bean id="serviceReferenceHolder" class="">
      <property name="1stService" ref="1stService"/>
      <property name="2ndService" ref="2ndService"/>

   <bean id="1stService" class="">
      <property name="xxxxDao" ref="xxxxDao"/>
      <property name="applicationUserInfoHolder" ref="applicationUserInfoHolder"/>
      <property name="velocity" ref="velocityEngine"/>
      <property name="sessionTimeout" value="${session.timeout}"/>
      <property name="sessionCheckTimer" value="${session.check.timer}"/>

   <bean id="2ndService" class="">
      <property name="xxxxDao" ref="xxxFormDao"/>
      <property name="velocity" ref="velocityEngine"/>
      <property name="xxxxEnvironment" value="${xxxx.environment}"/>

The class would look like:

public class ServiceReferenceHolder
    public static 1stService 1stService;
    public static 2ndService 2ndService;

    public 1stService get1stService()
        return 1stService;

    public void set1stService(1stService 1stService)
        ServiceReferenceHolder.1stService= 1stService;

    public 2ndService get2ndService()
        return 2ndService;

    public void set2ndService(2ndService 2ndService)
        ServiceReferenceHolder.2ndService= 2ndService;

Now you can use the spring service in any class just by calling ServiceReferenceHolder.1stService.whateverMethod();


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s