package com.thinkbiganalytics.spring;

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


/**
 * Wrapper to always return a reference to the Spring Application Context from within non-Spring enabled beans. Unlike Spring MVC's WebApplicationContextUtils we do not need a reference to the Servlet
 * context for this. All we need is for this bean to be initialized during application startup.
 */
public class SpringApplicationContext implements ApplicationContextAware {

    private static ApplicationContext CONTEXT;

    /**
     * This method is called from within the ApplicationContext once it is done starting up, it will stick a reference to itself into this bean.
     *
     * @param context a reference to the ApplicationContext.
     */
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        CONTEXT = context;
    }

    public static ApplicationContext getApplicationContext() {
        return CONTEXT;
    }

    /**
     * This is about the same as context.getBean("beanName"), except it has its own static handle to the Spring context, so calling this method statically will give access to the beans by name in the
     * Spring application context. As in the context.getBean("beanName") call, the caller must cast to the appropriate target class. If the bean does not exist, then a Runtime error will be thrown.
     *
     * @param beanName the name of the bean to get.
     * @return an Object reference to the named bean.
     */
    public static Object getBean(String beanName) throws BeansException {
        return CONTEXT.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz) {
        return CONTEXT.getBean(clazz);
    }
}

@@ -48,43 +48,176 @@
 rg.s
-pringframework.beans.BeansException
+lf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.beans.BeansException;%0Aimport org.springframework.beans.factory.config.AutowireCapableBeanFactory
 ;%0Aim
@@ -450,247 +450,157 @@
 ans.
- Unlike Spring MVC's WebApplicationContextUtils we do not need a reference to the Servlet%0A * context for this. All we need is for this bean to be initialized during application startup.%0A */%0Apublic class SpringApplicationContext implements 
+%0A */%0Apublic class SpringApplicationContext implements ApplicationContextAware %7B%0A%0A    private static final Logger log = LoggerFactory.getLogger(Spring
 Appl
@@ -605,39 +605,40 @@
 plicationContext
-Aware %7B
+.class);
 %0A%0A    private st
@@ -1863,10 +1863,1573 @@
 ;%0A    %7D%0A
+%0A%0A    /**%0A     * Autowire properties in the object%0A     */%0A    public static Object autowire(String key, Object obj) %7B%0A%0A        return autowire(key, obj, true);%0A    %7D%0A%0A    /**%0A     * Auto wire an object that is created outside of spring but references spring @Autowire annotation%0A     */%0A    public static Object autowire(Object obj) %7B%0A        autowire(null, obj, true);%0A        return obj;%0A    %7D%0A%0A%0A    /**%0A     * Autowire an object Force it to be autowired even if the bean is not registered with the appcontext%0A     */%0A    public static Object autowire(String key, Object obj, boolean force) %7B%0A        Object bean = null;%0A        if (key != null) %7B%0A            try %7B%0A                bean = getBean(key);%0A            %7D catch (Exception e) %7B%0A                //this is ok%0A            %7D%0A        %7D%0A        if (bean == null %7C%7C force) %7B%0A%0A            try %7B%0A%0A                if (CONTEXT != null) %7B%0A                    AutowireCapableBeanFactory autowire = getApplicationContext().getAutowireCapableBeanFactory();%0A                    autowire.autowireBean(obj);%0A                    //fire PostConstruct methods%0A                    autowire.initializeBean(obj, key);%0A                    return obj;%0A                %7D else %7B%0A                    log.error(%22Unable to autowire %7B%7D with Object.  ApplicationContext is null.%22, key, obj);%0A                %7D%0A            %7D catch (Exception e) %7B%0A                log.error(%22Unable to autowire %7B%7D with Object %22, key, obj);%0A            %7D%0A        %7D else if (bean != null) %7B%0A            return bean;%0A        %7D%0A        return null;%0A    %7D%0A
 %7D%0A

add util methods to autowire bean not created from spring