Example usage for org.springframework.context ApplicationContextAware setApplicationContext

List of usage examples for org.springframework.context ApplicationContextAware setApplicationContext

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContextAware setApplicationContext.

Prototype

void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

Source Link

Document

Set the ApplicationContext that this object runs in.

Usage

From source file:org.qi4j.library.spring.bootstrap.internal.application.Qi4jApplicationFactoryBean.java

public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
    if (this.applicationBootstrap instanceof ApplicationContextAware) {
        // propagate application context to the application bootstrap
        ApplicationContextAware aware = (ApplicationContextAware) this.applicationBootstrap;
        aware.setApplicationContext(applicationContext);
    }//from  w  w  w  . j a v  a2  s . co  m
}

From source file:org.apache.click.extras.spring.SpringClickServlet.java

/**
 * This method associates the <tt>ApplicationContext</tt> with any
 * <tt>ApplicationContextAware</tt> pages and supports the deserialization
 * of stateful pages.//www .  ja v a  2s . co m
 *
 * @see ClickServlet#activatePageInstance(Page)
 *
 * @param page the page instance to activate
 */
@Override
protected void activatePageInstance(Page page) {
    ApplicationContext applicationContext = getApplicationContext();

    if (page instanceof ApplicationContextAware) {
        ApplicationContextAware aware = (ApplicationContextAware) page;
        aware.setApplicationContext(applicationContext);
    }

    Class<? extends Page> pageClass = page.getClass();
    String beanName = toBeanName(pageClass);

    if (applicationContext.containsBean(beanName) || applicationContext.containsBean(pageClass.getName())) {

        // Beans are injected through Spring
    } else {

        // Inject any Spring beans into the page instance
        if (!pageSetterBeansMap.isEmpty()) {
            // In development mode, lazily loaded page classes won't have
            // their bean setters methods mapped, thus beans won't be injected
            List<BeanNameAndMethod> beanList = pageSetterBeansMap.get(page.getClass());
            if (beanList != null) {
                for (BeanNameAndMethod bnam : beanList) {
                    Object bean = applicationContext.getBean(bnam.beanName);

                    try {
                        Object[] args = { bean };
                        bnam.method.invoke(page, args);

                    } catch (Exception error) {
                        throw new RuntimeException(error);
                    }
                }
            }
        }
    }
}

From source file:ome.services.util.BeanHelper.java

public final void configure(SelfConfigurableService bean) {
    this.acquireContext();
    // This will, in turn, call throwIfAlreadySet
    this.applicationContext.applyBeanPropertyValues(bean, bean.getServiceInterface());
    // FIXME setApplicationContext should be called properly (I think?)
    // However, we're going to do it here anyway.
    if (bean instanceof ApplicationContextAware) {
        ApplicationContextAware aca = (ApplicationContextAware) bean;
        aca.setApplicationContext(applicationContext);
    }/* www . j  ava2 s.c  o m*/
}

From source file:org.springframework.ws.test.support.MockStrategiesHelper.java

/**
 * Returns a single strategy found in the given application context, or instantiates a default strategy if no
 * applicable strategy was found.//from   w ww.ja  v a 2s. c  om
 *
 * @param type the type of bean to be found in the application context
 * @param defaultType the type to instantiate and return when no bean of the specified type could be found
 * @return the bean found in the application context, or the default type if no bean of the given type can be found
 * @throws BeanInitializationException if there is more than 1 beans of the given type
 */
public <T, D extends T> T getStrategy(Class<T> type, Class<D> defaultType) {
    Assert.notNull(defaultType, "'defaultType' must not be null");
    T t = getStrategy(type);
    if (t != null) {
        return t;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("No " + ClassUtils.getShortName(type) + " found, using default "
                    + ClassUtils.getShortName(defaultType));
        }
        T defaultStrategy = BeanUtils.instantiateClass(defaultType);
        if (defaultStrategy instanceof ApplicationContextAware) {
            ApplicationContextAware applicationContextAware = (ApplicationContextAware) defaultStrategy;
            applicationContextAware.setApplicationContext(applicationContext);
        }
        if (defaultStrategy instanceof InitializingBean) {
            InitializingBean initializingBean = (InitializingBean) defaultStrategy;
            try {
                initializingBean.afterPropertiesSet();
            } catch (Exception ex) {
                throw new BeanCreationException("Invocation of init method failed", ex);
            }
        }
        return defaultStrategy;
    }
}