Example usage for org.springframework.context ConfigurableApplicationContext getBeanFactory

List of usage examples for org.springframework.context ConfigurableApplicationContext getBeanFactory

Introduction

In this page you can find the example usage for org.springframework.context ConfigurableApplicationContext getBeanFactory.

Prototype

ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

Source Link

Document

Return the internal bean factory of this application context.

Usage

From source file:org.syncope.core.init.AbstractLoader.java

protected DefaultListableBeanFactory getBeanFactory() {
    ConfigurableApplicationContext context = ApplicationContextManager.getApplicationContext();

    return (DefaultListableBeanFactory) context.getBeanFactory();
}

From source file:runkoserver.integration.SeleniumTestExecutionListener.java

@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
    if (webDriver != null) {
        return;//from   w w w  . j  ava  2 s  .  co  m
    }
    ApplicationContext context = testContext.getApplicationContext();
    if (context instanceof ConfigurableApplicationContext) {

        SeleniumTest annotation = findAnnotation(testContext.getTestClass(), SeleniumTest.class);
        webDriver = BeanUtils.instantiate(annotation.driver());

        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) context;
        ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
        bf.registerResolvableDependency(WebDriver.class, webDriver);
    }
}

From source file:be.hikage.springtemplate.TemplateTest.java

@Test
public void testGood() {

    ApplicationContext context = new ClassPathXmlApplicationContext("test-config-good.xml", this.getClass());
    assertTrue("Bean simple-dev must be defined", context.containsBean("simple-dev"));
    assertEquals(SimpleBean.class, context.getBean("simple-dev").getClass());
    SimpleBean simpleBean = (SimpleBean) context.getBean("simple-dev");

    assertEquals("constructorData.dev", simpleBean.getConstructorValue());
    assertEquals("ExternalizedConstructor", simpleBean.getExternalizedConstructorValue());

    assertEquals("propertyData.dev", simpleBean.getPropertyValue());
    assertEquals("ExternalizedProperty", simpleBean.getExternalizedPropertyValue());

    assertTrue("Bean container-dev must be defined", context.containsBean("container-dev"));
    assertEquals(ContainerBean.class, context.getBean("container-dev").getClass());

    ConfigurableApplicationContext listableBeanFactory = (ConfigurableApplicationContext) context;
    RootBeanDefinition beanDefinition = (RootBeanDefinition) listableBeanFactory.getBeanFactory()
            .getBeanDefinition("container-dev");
    assertArrayEquals(new String[] { "simple-dev" }, beanDefinition.getDependsOn());

}

From source file:com.graby.store.base.remote.RemotingAnnotationHandlerMapping.java

/**
 * Checks for presence of the {@link org.springframework.web.bind.annotation.RequestMapping}
 * annotation on the handler class and on any of its methods.
 *///ww w .jav a2s .  c  o  m
protected String[] determineUrlsForHandler(String beanName) {

    ApplicationContext context = getApplicationContext();
    Class<?> handlerType = context.getType(beanName);
    RemotingService mapping = AnnotationUtils.findAnnotation(handlerType, RemotingService.class);
    if (mapping == null && context instanceof ConfigurableApplicationContext
            && context.containsBeanDefinition(beanName)) {
        ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
        BeanDefinition bd = cac.getBeanFactory().getMergedBeanDefinition(beanName);
        if (bd instanceof AbstractBeanDefinition) {
            AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
            if (abd.hasBeanClass()) {
                Class<?> beanClass = abd.getBeanClass();
                mapping = AnnotationUtils.findAnnotation(beanClass, RemotingService.class);
            }
        }
    }

    if (mapping != null) {
        this.cachedMappings.put(handlerType, mapping);
        Set<String> urls = new LinkedHashSet<String>();
        String path = mapping.serviceUrl();
        if (path != null) {
            addUrlsForPath(urls, path);
            return StringUtils.toStringArray(urls);
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.os890.adapter.cdi.springmvc.CdiAwareDispatcherServlet.java

@Override
protected WebApplicationContext findWebApplicationContext() {
    ConfigurableApplicationContext bridgeContext = ApplicationContextAwareWebappAwareSpringContainerManager
            .consumePreviousApplicationContext();
    ConfigurableListableBeanFactory bridgeFactory = bridgeContext.getBeanFactory();
    BeanFactoryAwareBeanFactoryPostProcessor.currentBeanFactory.set(bridgeFactory);

    WebApplicationContext result = null;
    try {//  www  . j a  v  a2 s.  com
        result = createWebApplicationContext(null);
        return result;
    } finally {
        BeanFactoryAwareBeanFactoryPostProcessor.currentBeanFactory.set(null);
        BeanFactoryAwareBeanFactoryPostProcessor.currentBeanFactory.remove();

        if (result instanceof ConfigurableApplicationContext) {
            SpringBridgeExtension.updateSpringContext((ConfigurableApplicationContext) result);
        }
    }
}

From source file:org.vaadin.webinars.springandvaadin.serialization.SerializableUI.java

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();//from ww  w . j  a v a2  s.  c o  m
    ConfigurableApplicationContext appContext = (ConfigurableApplicationContext) applicationContextHolder.get();
    BeanConfigurerSupport configurerSupport = new BeanConfigurerSupport();
    configurerSupport.setBeanFactory(appContext.getBeanFactory());
    configurerSupport.afterPropertiesSet();
    configurerSupport.configureBean(this);
    configurerSupport.destroy();
}

From source file:com.ivanzhangwb.interpose.core.InterposeBootStrap.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    if (interposeClassPathApplicationContext == null) {
        interposeClassPathApplicationContext = new InterposeClassPathApplicationContext(
                this.applicationContext);
    }/*from   ww  w  . j ava  2s .  c  o m*/
    boolean needInterpose = false;
    Class beanClass = bean.getClass();
    do {
        Method[] methods = beanClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (Interpose.class != null && methods[i].isAnnotationPresent(Interpose.class)
                    && methods[i].equals(ClassUtils.getMostSpecificMethod(methods[i], bean.getClass()))) {
                needInterpose = true;
                handleMethodAnnotation(methods[i]);
            }
        }
        beanClass = beanClass.getSuperclass();
    } while (beanClass != null);

    if (needInterpose) {
        ConfigurableApplicationContext atx = (ConfigurableApplicationContext) interposeClassPathApplicationContext;
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) atx.getBeanFactory();
        ProxyFactoryBean factoryBean = new ProxyFactoryBean();
        factoryBean.setTarget(bean);
        factoryBean.setInterceptorNames(new String[] { InterposeConstants.INTERPOSE_CORE_INTERCEPTOR });
        factoryBean.setBeanFactory(beanFactory);
        return factoryBean.getObject();
    } else {
        return bean;
    }
}

From source file:org.sakaiproject.entitybroker.util.spring.BeanCollectorAutoRegistrar.java

public void afterPropertiesSet() throws Exception {
    log.debug("setAC: " + applicationContext.getDisplayName());
    ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
    ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) cac.getBeanFactory();

    cbf.addBeanPostProcessor(new BeanPostProcessor() {
        @SuppressWarnings("unchecked")
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            if (bean instanceof BeanCollector<?>) {
                BeanCollector<Object> bc = (BeanCollector<Object>) bean;
                Class<?> c = bc.getCollectedType();
                if (c == null) {
                    throw new IllegalArgumentException("collected type cannot be null");
                }/*from   w  w w .j  a v a2  s  .c  om*/

                List<Object> l = getAutoRegisteredBeansOfType(c);
                logCollectedBeanInsertion(beanName, c.getName(), l);
                bc.setCollectedBeans(l);
            } else if (bean instanceof BeanMapCollector) {
                BeanMapCollector bc = (BeanMapCollector) bean;
                Class<?>[] cArray = bc.getCollectedTypes();
                if (cArray == null) {
                    throw new IllegalArgumentException("collected types cannot be null");
                }

                Map<Class<?>, List<?>> collectedBeans = new HashMap<Class<?>, List<?>>();
                for (int i = 0; i < cArray.length; i++) {
                    List<Object> l = getAutoRegisteredBeansOfType(cArray[i]);
                    logCollectedBeanInsertion(beanName, cArray[i].getName(), l);
                    collectedBeans.put(cArray[i], l);
                }
                bc.setCollectedBeansMap(collectedBeans);
            }
            return bean;
        }

        public Object postProcessAfterInitialization(Object bean, String beanName) {
            return bean;
        }
    });

}

From source file:com.longio.spring.LioBootstrap.java

private void boot(ConfigurableApplicationContext app) throws Exception {
    DefaultListableBeanFactory bf = (DefaultListableBeanFactory) app.getBeanFactory();

    for (String name : bf.getBeanDefinitionNames()) {
        if (name.equalsIgnoreCase("longio.bootstrap")) {
            bootEndpoints(bf, name);//from w  w w .j  av a2 s .  co m
            break;
        }
    }

    doScanAndRegist(bf);

    resolveLservice(app, bf);

    resolveLfilters(app, bf);
}

From source file:org.solmix.runtime.support.spring.SpringBeanProvider.java

public boolean hasConfiguredPropertyValue(String beanName, String propertyName, String searchValue) {
    if (context.containsBean(beanName) && !passThroughs.contains(beanName)) {
        ConfigurableApplicationContext ctxt = (ConfigurableApplicationContext) context;
        BeanDefinition def = ctxt.getBeanFactory().getBeanDefinition(beanName);
        if (!ctxt.getBeanFactory().isSingleton(beanName) || def.isAbstract()) {
            return false;
        }/*from w  w w .  ja v  a  2  s.c om*/
        Collection<?> ids = null;
        PropertyValue pv = def.getPropertyValues().getPropertyValue(propertyName);

        if (pv != null) {
            Object value = pv.getValue();
            if (!(value instanceof Collection)) {
                throw new RuntimeException("The property " + propertyName + " must be a collection!");
            }

            if (value instanceof Mergeable) {
                if (!((Mergeable) value).isMergeEnabled()) {
                    ids = (Collection<?>) value;
                }
            } else {
                ids = (Collection<?>) value;
            }
        }

        if (ids != null) {
            for (Iterator<?> itr = ids.iterator(); itr.hasNext();) {
                Object o = itr.next();
                if (o instanceof TypedStringValue) {
                    if (searchValue.equals(((TypedStringValue) o).getValue())) {
                        return true;
                    }
                } else {
                    if (searchValue.equals(o)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
    //        return orig.hasConfiguredPropertyValue(beanName, propertyName, searchValue);
}