Example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

List of usage examples for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory.

Prototype

AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

Source Link

Document

Expose AutowireCapableBeanFactory functionality for this context.

Usage

From source file:org.constretto.spring.assembly.AssemblyWithConcreteClassesTest.java

@Test(expected = BeanCreationException.class)
public void injectionNotUsingConstrettoUsingInterface() {
    class MyConsumer {
        @Autowired/*from w w w  . ja  va  2 s  .  c o  m*/
        CommonInterface commonInterface;
    }
    ApplicationContext ctx = loadContextAndInjectWithoutConstretto();

    MyConsumer consumer = new MyConsumer();
    ctx.getAutowireCapableBeanFactory().autowireBean(consumer);
    Assert.assertEquals(consumer.commonInterface.getClass(), CommonInterfaceDefault.class);
}

From source file:com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
    if (!(autowireCapableBeanFactory instanceof DefaultListableBeanFactory)) {
        throw new IllegalArgumentException(
                "The implementation returned from applicationContext.getAutowireCapableBeanFactory() "
                        + "doesn't implement DefaultListableBeanFactory");
    }// w ww  .ja  v a  2 s . co  m
    DefaultListableBeanFactory factory = (DefaultListableBeanFactory) autowireCapableBeanFactory;
    registerBean(factory, WorkflowScopeBeanNames.GENERIC_ACTIVITY_CLIENT, GenericActivityClient.class);
    registerBean(factory, WorkflowScopeBeanNames.GENERIC_WORKFLOW_CLIENT, GenericWorkflowClient.class);
    registerBean(factory, WorkflowScopeBeanNames.WORKFLOW_CLOCK, WorkflowClock.class);
    registerBean(factory, WorkflowScopeBeanNames.WORKFLOW_CONTEXT, WorkflowContext.class);
    registerBean(factory, WorkflowScopeBeanNames.DECISION_CONTEXT, DecisionContext.class);
}

From source file:com.zenika.stripes.contrib.spring.SpringRuntimeConfiguration.java

/**
 * Splits a comma-separated list of class names and maps each {@link LifecycleStage} to the
 * interceptors in the list that intercept it. Also automatically finds Interceptors in
 * packages listed in {@link BootstrapPropertyResolver#PACKAGES} if searchExtensionPackages is true.
 * /*www .  j  ava2 s. c  om*/
 * Interceptors are instancied from the Spring factory AutowireCapableBeanFactory.
 * @see <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-beans/3.0.2.RELEASE/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java#AbstractAutowireCapableBeanFactory.createBean%28java.lang.Class%29">createBean code</a>
 * 
 * @return a Map of {@link LifecycleStage} to Collection of {@link Interceptor}
 */
@Override
protected Map<LifecycleStage, Collection<Interceptor>> initInterceptors(List classes) {

    Map<LifecycleStage, Collection<Interceptor>> map = new HashMap<LifecycleStage, Collection<Interceptor>>();

    for (Object type : classes) {
        try {
            ServletContext servletContext = getServletContext();
            ApplicationContext applicationContext = WebApplicationContextUtils
                    .getWebApplicationContext(servletContext);

            AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
            Interceptor interceptor = (Interceptor) beanFactory.createBean((Class) type,
                    AutowireCapableBeanFactory.AUTOWIRE_NO, false);
            addInterceptor(map, interceptor);
        } catch (Exception e) {
            throw new StripesRuntimeException(
                    "Could not instantiate configured Interceptor [" + type.getClass().getName() + "].", e);
        }
    }

    return map;
}

From source file:org.echocat.jomon.spring.BeanProvider.java

@Override
public void setApplicationContext(@Nonnull ApplicationContext context) throws BeansException {
    if (!(context.getAutowireCapableBeanFactory() instanceof BeanDefinitionRegistry)) {
        throw new IllegalArgumentException(
                "The autowireCapableBeanFactory of given context must also implement "
                        + BeanDefinitionRegistry.class.getName() + ".");
    }//from   w  w  w. j  a  va  2  s.c om
    _applicationContext = context;
}

From source file:works.cirno.mocha.SpringMVCFactory.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    processer = new AutowiredAnnotationBeanPostProcessor();
    processer.setBeanFactory(applicationContext.getAutowireCapableBeanFactory());
}

From source file:org.usergrid.management.cassandra.ManagementTestHelperImpl.java

@Override
public void setup() throws Exception {
    // assertNotNull(client);

    String maven_opts = System.getenv("MAVEN_OPTS");
    logger.info("Maven options: " + maven_opts);

    logger.info("Starting Cassandra");
    embedded = new EmbeddedServerHelper();
    embedded.setup();//from ww  w .  ja va  2 s .  co  m

    // copy("/testApplicationContext.xml", TMP);

    String[] locations = { "testApplicationContext.xml" };
    ApplicationContext ac = new ClassPathXmlApplicationContext(locations);

    AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
    acbf.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    acbf.initializeBean(this, "testClient");

    assertNotNull(emf);
    assertTrue("EntityManagerFactory is instance of EntityManagerFactoryImpl",
            emf instanceof EntityManagerFactoryImpl);

    emf.setup();

    management.setup();

}

From source file:com.griddynamics.banshun.web.ScanChildrenHandlerMapping.java

private List<HandlerMapping> createDefaultHandlerMappings(ApplicationContext context) {
    List<HandlerMapping> handlerMappings = new ArrayList<HandlerMapping>(2);
    try {/*  w w  w  . ja  v a2  s  .co m*/
        for (Class<?> clazz : defaultHandlerMappingClasses) {
            Object handlerMapping = context.getAutowireCapableBeanFactory().createBean(clazz);
            handlerMappings.add((HandlerMapping) handlerMapping);
        }
    } catch (Exception ex) {
        throw new BeanInitializationException(ex.getMessage(), ex);
    }

    return handlerMappings;
}

From source file:ro.pippo.spring.SpringControllerFactory.java

public SpringControllerFactory(ApplicationContext applicationContext, boolean autoRegistering) {
    this.applicationContext = applicationContext;
    this.autoRegistering = autoRegistering;

    if (autoRegistering) {
        beanDefinitionRegistry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
    }//from w w  w .j av a2 s.  co  m
}

From source file:org.trpr.platform.batch.impl.job.ha.service.CuratorJobSyncHandlerFactory.java

/**
 * Interface method implementation. //from w w  w .j  a v a  2  s.  c  o  m
 * Constructs and returns as instance of {@link CuratorJobSyncHandler} for Trooper batch runtime.
 * @see org.springframework.beans.factory.FactoryBean#getObject()
 */
@Override
public CuratorJobSyncHandler getObject() throws Exception {
    ApplicationContext context = SpringBatchComponentContainer.getCommonBatchBeansContext();
    if (!context.containsBean(this.SYNC_HANDLER_BEAN_NAME)) {
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context
                .getAutowireCapableBeanFactory();
        BeanDefinitionBuilder builder = BeanDefinitionBuilder
                .rootBeanDefinition(CuratorJobSyncHandler.class.getName())
                .addConstructorArgValue(jobConfigurationService).addConstructorArgValue(curatorFramework)
                .addConstructorArgValue(bootstrapMonitorBean);
        beanFactory.registerBeanDefinition(this.SYNC_HANDLER_BEAN_NAME, builder.getBeanDefinition());
    }
    return (CuratorJobSyncHandler) context.getBean(this.SYNC_HANDLER_BEAN_NAME);
}

From source file:org.withinsea.izayoi.adapter.springmvc.SpringIzayoiDispatcherFilter.java

@Override
protected void initStrategies(ApplicationContext context) {
    super.initStrategies(context);
    SpringCloisterCortileViewResolver cloisterCortileViewResolver = (SpringCloisterCortileViewResolver) context
            .getAutowireCapableBeanFactory()
            .initializeBean(izayoiViewResolver, "cortilePathVariablesViewResolver");
    try {/*from w w  w.j  a va  2s .c  o  m*/
        Field field = DispatcherServlet.class.getDeclaredField("viewResolvers");
        field.setAccessible(true);
        @SuppressWarnings("unchecked")
        List<ViewResolver> viewResolvers = (List<ViewResolver>) field.get(this);
        if (viewResolvers == null) {
            field.set(this, Arrays.asList(cloisterCortileViewResolver));
        } else if (usingDefaultViewResolver) {
            viewResolvers.add(0, cloisterCortileViewResolver);
        } else {
            viewResolvers.add(cloisterCortileViewResolver);
        }
    } catch (Exception e) {
        throw new IzayoiRuntimeException(e);
    }
}