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

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

Introduction

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

Prototype

public GenericApplicationContext() 

Source Link

Document

Create a new GenericApplicationContext.

Usage

From source file:org.mybatis.spring.support.SqlSessionDaoSupportTest.java

private void setupContext() {
    applicationContext = new GenericApplicationContext();

    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(MockSqlSessionDao.class);
    applicationContext.registerBeanDefinition("dao", definition);

    // add support for autowiring fields
    AnnotationConfigUtils.registerAnnotationConfigProcessors(applicationContext);
}

From source file:edu.wisc.my.portlets.dmp.tools.XmlMenuPublisher.java

/**
 * Get the bean factory for the publisher.
 *///from ww w.j a v a2s  .c om
private synchronized BeanFactory getFactory() {
    if (this.factory == null) {
        final GenericApplicationContext ctx = new GenericApplicationContext();
        final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
        xmlReader.loadBeanDefinitions(new ClassPathResource("/publisherApplicationContext.xml"));
        ctx.refresh();

        this.factory = ctx;
    }

    return this.factory;
}

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  ww  w  . j a  v a  2  s  .c om

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

}

From source file:com.opengamma.language.connector.LanguageSpringContext.java

/**
 * Creates a Spring context from the base configuration file in OG-Language and any other Spring XML configuration
 * files found in the configuration directory.  The directory must be specified using the system property named
 * {@link #LANGUAGE_EXT_PATH}.//from   www  .j  a  v a 2s.c o m
 * @return A Spring context built from all the XML config files.
 */
public static GenericApplicationContext createSpringContext() {
    s_logger.info("Starting OpenGamma language integration service");
    GenericApplicationContext context = new GenericApplicationContext();
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    xmlReader.loadBeanDefinitions(new ClassPathResource(CLIENT_XML));
    String[] xmlFiles = findSpringXmlConfig();
    xmlReader.loadBeanDefinitions(xmlFiles);
    s_logger.info("Creating context beans");
    context.refresh();
    s_logger.info("Starting application context");
    context.start();
    s_logger.info("Application context started");
    return context;
}

From source file:org.beanlet.springframework.impl.SpringHelper.java

public static synchronized ListableBeanFactory getListableBeanFactory(BeanletConfiguration<?> configuration,
        Element element) {// www .  j a  va  2s .co  m
    SpringContext springContext = getSpringContext(configuration, element);
    if (springContext == null) {
        throw new ApplicationContextException("No spring context specified.");
    }
    final ClassLoader loader = configuration.getComponentUnit().getClassLoader();
    Map<SpringContext, ListableBeanFactory> map = factories.get(loader);
    if (map == null) {
        map = new HashMap<SpringContext, ListableBeanFactory>();
        factories.put(loader, map);
    }
    ListableBeanFactory factory = map.get(springContext);
    if (factory == null) {
        ClassLoader org = null;
        try {
            org = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
                    // PERMISSION: java.lang.RuntimePermission getClassLoader
                    ClassLoader org = Thread.currentThread().getContextClassLoader();
                    // PERMISSION: java.lang.RuntimePermission setContextClassLoader
                    Thread.currentThread().setContextClassLoader(loader);
                    return org;
                }
            });
            if (springContext.applicationContext()) {
                factory = new GenericApplicationContext();
            } else {
                factory = new DefaultListableBeanFactory();
            }
            // Do not create spring context in priviliged scope!
            for (SpringResource r : springContext.value()) {
                String path = r.value();
                Resource resource = null;
                BeanDefinitionReader reader = null;
                switch (r.type()) {
                case CLASSPATH:
                    resource = new ClassPathResource(path);
                    break;
                case FILESYSTEM:
                    resource = new FileSystemResource(path);
                    break;
                case URL:
                    resource = new UrlResource(path);
                    break;
                default:
                    assert false : r.type();
                }
                switch (r.format()) {
                case XML:
                    reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) factory);
                    break;
                case PROPERTIES:
                    reader = new PropertiesBeanDefinitionReader((BeanDefinitionRegistry) factory);
                    break;
                default:
                    assert false : r.format();
                }
                if (resource != null && resource.exists()) {
                    reader.loadBeanDefinitions(resource);
                }
            }
            if (factory instanceof ConfigurableApplicationContext) {
                ((ConfigurableApplicationContext) factory).refresh();
            }
            map.put(springContext, factory);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new ApplicationContextException("Failed to construct spring "
                    + (springContext.applicationContext() ? "application context" : "bean factory") + ".", e);
        } finally {
            final ClassLoader tmp = org;
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    // PERMISSION: java.lang.RuntimePermission setContextClassLoader
                    Thread.currentThread().setContextClassLoader(tmp);
                    return null;
                }
            });
        }
    }
    return factory;
}

From source file:org.yardstickframework.gridgain.GridGainNode.java

/**
 * @param springCfgPath Spring configuration file path.
 * @return Grid configuration./*  ww  w . j av  a  2s.  co m*/
 * @throws Exception If failed.
 */
private static GridConfiguration loadConfiguration(String springCfgPath) throws Exception {
    URL url;

    try {
        url = new URL(springCfgPath);
    } catch (MalformedURLException e) {
        url = GridUtils.resolveGridGainUrl(springCfgPath);

        if (url == null)
            throw new GridException("Spring XML configuration path is invalid: " + springCfgPath
                    + ". Note that this path should be either absolute or a relative local file system path, "
                    + "relative to META-INF in classpath or valid URL to GRIDGAIN_HOME.", e);
    }

    GenericApplicationContext springCtx;

    try {
        springCtx = new GenericApplicationContext();

        new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url));

        springCtx.refresh();
    } catch (BeansException e) {
        throw new Exception("Failed to instantiate Spring XML application context [springUrl=" + url + ", err="
                + e.getMessage() + ']', e);
    }

    Map<String, GridConfiguration> cfgMap;

    try {
        cfgMap = springCtx.getBeansOfType(GridConfiguration.class);
    } catch (BeansException e) {
        throw new Exception(
                "Failed to instantiate bean [type=" + GridConfiguration.class + ", err=" + e.getMessage() + ']',
                e);
    }

    if (cfgMap == null || cfgMap.isEmpty())
        throw new Exception("Failed to find grid configuration in: " + url);

    return cfgMap.values().iterator().next();
}

From source file:fi.okm.mpass.shibboleth.profile.metadata.DataSourceMetadataResolverTest.java

public DataSourceMetadataResolver getResolver() throws Exception {
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().addBeanPostProcessor(new IdentifiableBeanPostProcessor());
    return getBean(BASE_PATH_BEANS + "/dataSourceEntity.xml", DataSourceMetadataResolver.class, context, false);
}

From source file:org.finra.jtaf.core.AutomationEngine.java

private AutomationEngine() {
    try {/* w w  w .ja v a2 s .c  o  m*/
        InputStream fi = getFrameworkFile();
        GenericApplicationContext ctx = new GenericApplicationContext();

        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
        xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);

        xmlReader.loadBeanDefinitions(new InputSource(fi));

        ctx.refresh();

        this.pluginManager = (PluginManager) ctx.getBean("PluginManager");

        digraph = new TestDigraph(new ClassBasedEdgeFactory<DiNode, DiEdge>(DiEdge.class));
        commandlibParser = new CommandLibraryParser();
        scriptParser = new ScriptParser();
        scriptParser.setDigraph(digraph);
        commandlibParser.setAutomationClassLoader(new DefaultAutomationClassLoader());
        testStrategyParser = new TestStrategyParser();
        testStrategyParser.setDigraph(digraph);
        initPostParseStrategyElementPlugins();
        testRoot = null;

        this.interpreter = (Interpreter) ctx.getBean("Interpreter");

        this.interpreter.setCommandRunnerPlugins(pluginManager.getCommandRunnerPlugins());
        this.interpreter.setTestRunnerPlugins(pluginManager.getTestRunnerPlugins());

        initPostParseAllPlugins();
        initPostParseSuitePlugins();
        initPostParseTestPlugins();

    } catch (Exception e) {
        // If something goes wrong here, we have a serious issue
        logger.fatal(e);
        throw new RuntimeException(e);
    }
}

From source file:org.pentaho.platform.dataaccess.datasource.wizard.service.impl.utils.PentahoSystemHelper.java

private static ApplicationContext getSpringApplicationContext() {

    String[] fns = { "pentahoObjects.spring.xml", "adminPlugins.xml", "sessionStartupActions.xml",
            "systemListeners.xml", "pentahoSystemConfig.xml" }; //$NON-NLS-2$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

    GenericApplicationContext appCtx = new GenericApplicationContext();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appCtx);

    for (String fn : fns) {
        File f = new File(getSolutionPath() + SYSTEM_FOLDER + "/" + fn); //$NON-NLS-1$
        if (f.exists()) {
            FileSystemResource fsr = new FileSystemResource(f);
            xmlReader.loadBeanDefinitions(fsr);
        }//from w  w  w .  j  av a  2  s  . c om
    }

    String[] beanNames = appCtx.getBeanDefinitionNames();
    System.out.println("Loaded Beans: "); //$NON-NLS-1$
    for (String n : beanNames) {
        System.out.println("bean: " + n); //$NON-NLS-1$
    }
    return appCtx;
}

From source file:org.wso2.carbon.springservices.GenericApplicationContextUtil.java

/**
 * Method to get the spring application context for a spring service
 *
 * @param axisService - spring service//  w  ww  . j  a  v a 2  s.  c  om
 * @param contextLocation - location of the application context file
 * @return - GenericApplicationContext for the given spring service
 * @throws AxisFault
 */

public static GenericApplicationContext getSpringApplicationContext(AxisService axisService,
        String contextLocation) throws AxisFault {

    Parameter appContextParameter = axisService.getParameter(SPRING_APPLICATION_CONTEXT);

    if (appContextParameter != null) {
        return (GenericApplicationContext) appContextParameter.getValue();

    } else {
        GenericApplicationContext appContext = new GenericApplicationContext();
        ClassLoader classLoader = axisService.getClassLoader();
        appContext.setClassLoader(classLoader);
        ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(classLoader);
            XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(appContext);
            xbdr.setValidating(false);
            InputStream in = classLoader.getResourceAsStream(contextLocation);
            if (in == null) {
                throw new AxisFault("Spring context cannot be located for AxisService");
            }
            xbdr.loadBeanDefinitions(new InputStreamResource(in));
            appContext.refresh();
            axisService.addParameter(new Parameter(SPRING_APPLICATION_CONTEXT, appContext));
        } catch (Exception e) {
            throw AxisFault.makeFault(e);
        } finally {
            // Restore
            Thread.currentThread().setContextClassLoader(prevCl);
        }
        return appContext;
    }
}