Accessing spring managed beans from non spring managed classes

There may be situation when some part of application uses Springs for dependency injection and bean management and other part of application is outside the spring managed environment. If classes which are not managed by Spring requires access to the Spring managed classes, then directory creating instance of spring managed class with new operator will not work. Because if we use new for creating instance, then spring will not be able to wire the dependency correctly.

In such situations we need to create bridge between Spring managed beans and classes outside the spring management scope. Following is the code for accessing Spring Managed beans from non Spring managed classes.

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Bridge Between Spring Managed beans and non spring managed classes
 * @author Pranav Maniar
 */
@Component
public class SpringBridge implements ApplicationContextAware {

    // Maintain reference to Spring's Application Context
    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext context)
            throws BeansException {
        this.context = context;
    }

    // Make constructor private, so that the class can not be instantiated
    private SpringBridge() {
    }

    /**
     * Get Spring Managed bean from Non Spring Managed (outside of spring) classes
     * 
     * @param type, Class of Bean whose instance is required
     * @return Spring managed bean
     */
    public static <T> T getBean(Class<T> type) {
        return (T) context.getBean(type);
    }
}

For e.g. If there is Spring managed bean UserService, which needs to be accessed from some other class. In following way spring managed beans can be accessed in outside of spring (non managed) class.

 UserService userService = SpringBridge.getBean(UserService.class);

 

How does it work?

Spring Managed Component

First SpringBridge needs to be registered as spring managed component. In the above code it is done by using @Component annotation on top of the SpringBridge class. Also, SpringBridge class needs to be on the location of Spring’s component scan, so that spring can recognize it as spring managed component.

ApplicationContextAware Interface

By implementing this interface object will be notified of the ApplicationCotext that it runs in. Normally setApplicationContext method will be invoked after population of normal bean properties but before an init callback. SpringBridge class stores reference to the applicationContext in the static variable.  Using this applicationContext any bean can be looked up.

Generic method for returning the bean

Static method public static <T> T getBean(Class<T> type)  takes class name as a parameter and returns the instance of the bean. It looks up for the bean in ApplicationContext and if instance is found there, then it is returned.

Leave a Reply