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.

Build Hadoop 2.7.x from source code on ubuntu

Following are the steps for building Hadoop 2.7.x from source code on ubuntu.

Checout Hadoop Code from Git

git clone git://git.apache.org/hadoop.git
git checkout branch-2.7.3

 

Install Dependencies

Install dependencies from apt-get

sudo apt-get update
sudo apt-get install openjdk-7-jdk maven git openssl dh-autoreconf cmake zlib1g-dev libssl-dev ssh rsync pkg-config

Install protocol buffer

wget https://github.com/google/protobuf/archive/v2.5.0.tar.gz
tar xvf v2.5.0.tar.gz
cd protobuf-2.5.0
./autogen.sh
./configure --prefix=/usr
make
make install

Install findbugs

wget https://sourceforge.net/projects/findbugs/files/findbugs/3.0.0/findbugs-3.0.0.zip/download
unzip download

 

Set Environment Variables

vim ~/.bashrc

## Put following lines at end of ~/.bashrc file
## Please change the path with appropriate value for your installation

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
export FINDBUGS_HOME=/opt/findbugs-3.0.0/

 

Build Hadoop Distribution

mvn clean install -Pdist,native,docs,src -Dtar -DskipTests

Hadoop tar.gz distribution will be created in hadoop-dist/target/ directory