Example usage for org.springframework.context.support GenericApplicationContext getBeanDefinition

List of usage examples for org.springframework.context.support GenericApplicationContext getBeanDefinition

Introduction

In this page you can find the example usage for org.springframework.context.support GenericApplicationContext getBeanDefinition.

Prototype

@Override
    public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException 

Source Link

Usage

From source file:net.cpollet.jixture.spring.TestConfiguration.java

@Test
public void springConfigurationIsWorking() {
    GenericApplicationContext applicationContext = new GenericApplicationContext( //
            new ClassPathXmlApplicationContext("classpath:/spring/jpa2-unit-test-context.xml"));

    for (String beanName : applicationContext.getBeanDefinitionNames()) {
        if (!applicationContext.getBeanDefinition(beanName).isAbstract()) {
            logger.info("Requesting bean {}", beanName);
            applicationContext.getBean(beanName);
        }/* w ww  .  j a v  a2s.com*/
    }
}

From source file:org.joinfaces.annotations.JsfCdiToSpringApplicationBeanFactoryPostProcessorIT.java

@Test
public void testNoScopedClass() {
    GenericApplicationContext acx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(acx);

    acx.registerBeanDefinition("noScopedClass",
            new AnnotatedGenericBeanDefinition(new StandardAnnotationMetadata(NoScopedClass.class)));
    acx.registerBeanDefinition("scopedBeansConfiguration",
            new RootBeanDefinition(ScopedBeansConfiguration.class));
    acx.addBeanFactoryPostProcessor(new JsfCdiToSpringBeanFactoryPostProcessor());
    acx.refresh();//from   w w w  . j  a v a 2 s  .co m

    assertThat(acx.getBeanDefinition("noScopedClass").getScope()).isEqualTo("");
    assertThat(acx.getBeanDefinition("noScopedBean").getScope()).isEqualTo("");

}

From source file:org.joinfaces.annotations.JsfCdiToSpringApplicationBeanFactoryPostProcessorIT.java

@Test
public void testViewScopedClass() {
    GenericApplicationContext acx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(acx);

    acx.registerBeanDefinition("viewScopedClass",
            new AnnotatedGenericBeanDefinition(new StandardAnnotationMetadata(ViewScopedClass.class)));
    acx.registerBeanDefinition("scopedBeansConfiguration",
            new RootBeanDefinition(ScopedBeansConfiguration.class));
    acx.addBeanFactoryPostProcessor(new JsfCdiToSpringBeanFactoryPostProcessor());
    acx.refresh();//from  w w w  .ja  v a2  s.  c o m

    assertThat(acx.getBeanDefinition("viewScopedClass").getScope()).isEqualTo(JsfCdiToSpring.VIEW);
    assertThat(acx.getBeanDefinition("viewScopedBean").getScope()).isEqualTo(JsfCdiToSpring.VIEW);
}

From source file:org.joinfaces.annotations.JsfCdiToSpringApplicationBeanFactoryPostProcessorIT.java

@Test
public void testSessionScopedClass() {
    GenericApplicationContext acx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(acx);

    acx.registerBeanDefinition("sessionScopedClass",
            new AnnotatedGenericBeanDefinition(new StandardAnnotationMetadata(SessionScopedClass.class)));
    acx.registerBeanDefinition("scopedBeansConfiguration",
            new RootBeanDefinition(ScopedBeansConfiguration.class));
    acx.addBeanFactoryPostProcessor(new JsfCdiToSpringBeanFactoryPostProcessor());
    acx.refresh();/*from  www  .  j a  va  2 s . c o  m*/

    assertThat(acx.getBeanDefinition("sessionScopedClass").getScope()).isEqualTo(JsfCdiToSpring.SESSION);
    assertThat(acx.getBeanDefinition("sessionScopedBean").getScope()).isEqualTo(JsfCdiToSpring.SESSION);
}

From source file:uk.co.modularaudio.util.spring.SpringComponentHelper.java

public GenericApplicationContext makeAppContext(final String beansResourcePath, final String configResourcePath,
        final String[] additionalBeansResources, final String[] additionalConfigResources)
        throws DatastoreException {
    GenericApplicationContext appContext = null;

    final Class<SpringComponentHelper> thisClass = SpringComponentHelper.class;

    // Do any work needed before we instantiate the context
    for (final SpringContextHelper helper : contextHelpers) {
        try {/*from  ww w.ja  va  2 s.c o  m*/
            helper.preContextDoThings();
        } catch (final Exception ce) {
            final String msg = "Exception caught calling precontext of helper " + helper.getClass() + ": "
                    + ce.toString();
            log.error(msg, ce);
            // Will halt context creation.
            throw new DatastoreException(msg, ce);
        }
    }

    try {
        final InputStream bIStream = thisClass.getResourceAsStream(beansResourcePath);
        if (bIStream != null) {
            final InputSource bISource = new InputSource(bIStream);

            appContext = new GenericApplicationContext();
            appContext.addBeanFactoryPostProcessor(beanInstantiationList);

            final XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);

            xbdr.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
            xbdr.loadBeanDefinitions(bISource);

            // And load any additional beans files now we've loaded the "driver"
            if (additionalBeansResources != null && additionalBeansResources.length > 0) {
                for (final String additionalBeansFilename : additionalBeansResources) {
                    InputStream aBiStream = null;
                    InputSource aBiSource = null;
                    try {
                        aBiStream = thisClass.getResourceAsStream(additionalBeansFilename);

                        if (aBiStream != null) {
                            aBiSource = new InputSource(aBiStream);
                            xbdr.loadBeanDefinitions(aBiSource);
                        } else {
                            throw new DatastoreException(
                                    "Failed to load additional services from file: " + additionalBeansFilename);
                        }
                    } finally {
                        if (aBiStream != null) {
                            aBiStream.close();
                        }
                    }
                }
            }

            final BeanDefinition bd = appContext.getBeanDefinition("configurationService");
            final MutablePropertyValues mpvs = bd.getPropertyValues();
            // Push in the configuration file if one needs to be set
            if (configResourcePath != null) {
                mpvs.removePropertyValue(CONFIG_RESOURCE_PATH_PROPERTY);
                mpvs.addPropertyValue(CONFIG_RESOURCE_PATH_PROPERTY, configResourcePath);

            }
            if (additionalConfigResources != null && additionalConfigResources.length > 0) {
                mpvs.removePropertyValue(ADDITIONAL_RESOURCE_PATHS);
                mpvs.addPropertyValue(ADDITIONAL_RESOURCE_PATHS, additionalConfigResources);
            }

            // Do any work needed before we refresh the context
            // Prerefresh
            for (final SpringContextHelper helper : contextHelpers) {
                try {
                    helper.preRefreshDoThings(appContext);
                } catch (final Exception prer) {
                    final String msg = "Exception caught calling prerefresh of helper " + helper.getClass()
                            + ": " + prer.toString();
                    log.error(msg, prer);
                    // Will halt context creation
                    throw new DatastoreException(msg, prer);
                }
            }

            appContext.refresh();

        } else {
            // Didn't find the beans file
            final String msg = "Unable to find beans file: " + beansResourcePath;
            log.error(msg);
            throw new DatastoreException(msg);
        }
    } catch (final Exception e) {
        final String msg = "Exception caught setting up app context: " + e.toString();
        log.error(msg, e);
        throw new DatastoreException(msg, e);
    }

    // Perform a GC pass here to clean up before things are launched post refresh
    Runtime.getRuntime().gc(); // NOPMD by dan on 30/07/15 15:00

    // Do any work needed after we refresh the context
    // Post refresh calls
    for (final SpringContextHelper helper : contextHelpers) {
        try {
            helper.postRefreshDoThings(appContext, beanInstantiationList);
        } catch (final Exception pre) {
            final String msg = "Exception caught calling postrefresh of helper " + helper.getClass() + ": "
                    + pre.toString();
            log.error(msg, pre);
            // Will halt context creation
            throw new DatastoreException(msg, pre);
        }
    }

    return appContext;
}