Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext registerBeanDefinition

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext registerBeanDefinition

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext registerBeanDefinition.

Prototype

@Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
            throws BeanDefinitionStoreException 

Source Link

Usage

From source file:com.kurento.kmf.spring.KurentoApplicationContextUtils.java

public static AnnotationConfigApplicationContext createKurentoHandlerServletApplicationContext(
        Class<?> servletClass, String servletName, ServletContext sc, String handlerClassName) {
    Assert.notNull(sc, "Cannot create Kurento ServletApplicationContext from null ServletContext");
    Assert.notNull(servletClass, "Cannot create KurentoServletApplicationContext from a null Servlet class");
    Assert.notNull(servletClass, "Cannot create KurentoServletApplicationContext from a null Hanlder class");

    if (childContexts == null) {
        childContexts = new ConcurrentHashMap<String, AnnotationConfigApplicationContext>();
    }//from   w ww  .  jav  a  2  s .  c o  m

    AnnotationConfigApplicationContext childContext = childContexts
            .get(servletClass.getName() + ":" + servletName);
    Assert.isNull(childContext, "Pre-existing context found associated to servlet class "
            + servletClass.getName() + " and servlet name " + servletName);

    childContext = new AnnotationConfigApplicationContext();
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClassName(handlerClassName);
    childContext.registerBeanDefinition(handlerClassName, beanDefinition);
    if (!kurentoApplicationContextExists()) {
        createKurentoApplicationContext(sc);
    }
    childContext.setParent(getKurentoApplicationContext());
    childContext.refresh();
    childContexts.put(servletClass.getName(), childContext);

    return childContext;
}

From source file:com.cloudera.cli.validator.Main.java

/**
 * From the arguments, run the validation.
 *
 * @param args command line arguments//from  w ww  .ja va  2  s .  c om
 * @throws IOException anything goes wrong with streams.
 * @return exit code
 */
public int run(String[] args) throws IOException {
    Writer writer = new OutputStreamWriter(outStream, Constants.CHARSET_UTF_8);
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    try {
        BeanDefinition cmdOptsbeanDefinition = BeanDefinitionBuilder
                .rootBeanDefinition(CommandLineOptions.class).addConstructorArgValue(appName)
                .addConstructorArgValue(args).getBeanDefinition();
        ctx.registerBeanDefinition(CommandLineOptions.BEAN_NAME, cmdOptsbeanDefinition);
        ctx.register(ApplicationConfiguration.class);
        ctx.refresh();
        CommandLineOptions cmdOptions = ctx.getBean(CommandLineOptions.BEAN_NAME, CommandLineOptions.class);
        CommandLineOptions.Mode mode = cmdOptions.getMode();
        if (mode == null) {
            throw new ParseException("No valid command line arguments");
        }
        ValidationRunner runner = ctx.getBean(mode.runnerName, ValidationRunner.class);
        boolean success = runner.run(cmdOptions.getCommandLineOptionActiveTarget(), writer);
        if (success) {
            writer.write("Validation succeeded.\n");
        }
        return success ? 0 : -1;
    } catch (BeanCreationException e) {
        String cause = e.getMessage();
        if (e.getCause() instanceof BeanInstantiationException) {
            BeanInstantiationException bie = (BeanInstantiationException) e.getCause();
            cause = bie.getMessage();
            if (bie.getCause() != null) {
                cause = bie.getCause().getMessage();
            }
        }
        IOUtils.write(cause + "\n", errStream);
        CommandLineOptions.printUsageMessage(appName, errStream);
        return -2;
    } catch (ParseException e) {
        LOG.debug("Exception", e);
        IOUtils.write(e.getMessage() + "\n", errStream);
        CommandLineOptions.printUsageMessage(appName, errStream);
        return -2;
    } finally {
        if (ctx != null) {
            ctx.close();
        }
        writer.close();
    }
}

From source file:org.jacpfx.vertx.spring.SpringVerticleFactory.java

private Verticle createSpringVerticle(final Class<?> currentVerticleClass, ClassLoader classLoader) {
    final SpringVerticle annotation = currentVerticleClass.getAnnotation(SpringVerticle.class);
    final Class<?> springConfigClass = annotation.springConfig();

    // Create the parent context  
    final GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
    genericApplicationContext.setClassLoader(classLoader);
    if (parentContext != null) {
        genericApplicationContext.setParent(parentContext);
    }//from   www. j a  v a 2 s . co  m
    genericApplicationContext.refresh();
    genericApplicationContext.start();

    // 1. Create a new context for each verticle and use the specified spring configuration class if possible
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);

    // 2. Register a bean definition for this verticle
    annotationConfigApplicationContext.registerBeanDefinition(currentVerticleClass.getSimpleName(),
            new VerticleBeanDefinition(currentVerticleClass));

    // 3. Add a bean factory post processor to avoid configuration issues
    annotationConfigApplicationContext
            .addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
    annotationConfigApplicationContext.refresh();
    annotationConfigApplicationContext.start();
    annotationConfigApplicationContext.registerShutdownHook();

    // 5. Return the verticle by fetching the bean from the context
    return (Verticle) annotationConfigApplicationContext.getBeanFactory()
            .getBean(currentVerticleClass.getSimpleName());
}

From source file:org.age.services.worker.internal.DefaultWorkerService.java

private void setupTaskFromClass(final @NonNull String className) {
    assert nonNull(className);
    assert !isTaskRunning() : "Task is already running.";

    taskLock.writeLock().lock();/* w w w . j  av a2 s .c  om*/
    try {
        log.debug("Setting up task from class {}.", className);

        log.debug("Creating internal Spring context.");
        final AnnotationConfigApplicationContext taskContext = new AnnotationConfigApplicationContext();

        // Configure task
        final BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(className);
        taskContext.registerBeanDefinition("runnable", builder.getBeanDefinition());

        prepareContext(taskContext);
        currentClassName = className;

        log.debug("Task setup finished.");
    } catch (final BeanCreationException e) {
        log.error("Cannot create the task.", e);
        cleanUpAfterTask();
    } finally {
        taskLock.writeLock().unlock();
    }
}