Using Spring Beans from Non-Spring Classes

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

   <bean id="serviceReferenceHolder" class="org.xxx.service.ServiceReferenceHolder">
      <property name="1stService" ref="1stService"/>
      <property name="2ndService" ref="2ndService"/>
   </bean>

   <bean id="1stService" class="org.xxx.service.1stServiceImpl">
      <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>

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

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();

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s