Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory registerSingleton

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory registerSingleton

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory registerSingleton.

Prototype

void registerSingleton(String beanName, Object singletonObject);

Source Link

Document

Register the given existing object as singleton in the bean registry, under the given bean name.

Usage

From source file:com.gwtplatform.dispatch.rpc.server.spring.utils.SpringUtils.java

public static <B> void registerBean(ApplicationContext applicationContext, B instance) throws BeansException {

    if (applicationContext instanceof GenericApplicationContext) {
        ConfigurableListableBeanFactory beanFactory = ((GenericApplicationContext) applicationContext)
                .getBeanFactory();/*from w w  w. ja v a  2  s . c o m*/
        beanFactory.registerSingleton(generateName(beanFactory, createBeanDefinition(instance)), instance);
    } else if (applicationContext instanceof AbstractRefreshableWebApplicationContext) {
        ConfigurableListableBeanFactory beanFactory = ((AbstractRefreshableWebApplicationContext) applicationContext)
                .getBeanFactory();
        beanFactory.registerSingleton(generateName(beanFactory, createBeanDefinition(instance)), instance);
    }
}

From source file:guru.qas.martini.annotation.StepsAnnotationProcessorTest.java

private static ClassPathXmlApplicationContext getContext(Object... beans) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("emptyContext.xml");
    ConfigurableListableBeanFactory factory = context.getBeanFactory();
    for (Object bean : beans) {
        factory.registerSingleton(bean.getClass().getName(), bean);
    }/*from   w ww .  j  av a  2  s.  co m*/
    return context;
}

From source file:com.ocs.dynamo.test.MockUtil.java

/**
 * Registers all fields that are annotated with "@Mock" as beans in the Spring context
 * //from  w ww.  ja  v a  2 s .  co  m
 * @param factory
 * @param subject
 * @param clazz
 */
public static void registerMocks(ConfigurableListableBeanFactory factory, Object subject, Class<?> clazz) {
    try {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getAnnotation(Mock.class) != null) {
                factory.registerSingleton(field.getName(), field.get(subject));
            }
        }
        if (clazz.getSuperclass() != null) {
            registerMocks(factory, subject, clazz.getSuperclass());
        }
    } catch (Exception e) {
        throw new OCSRuntimeException(e.getMessage(), e);
    }
}

From source file:lodsve.core.condition.ConditionEvaluationReport.java

/**
 * Obtain a {@link ConditionEvaluationReport} for the specified bean factory.
 *
 * @param beanFactory the bean factory//from ww  w.ja v  a 2 s.co m
 * @return an existing or new {@link ConditionEvaluationReport}
 */
public static ConditionEvaluationReport get(ConfigurableListableBeanFactory beanFactory) {
    synchronized (beanFactory) {
        ConditionEvaluationReport report;
        if (beanFactory.containsSingleton(BEAN_NAME)) {
            report = beanFactory.getBean(BEAN_NAME, ConditionEvaluationReport.class);
        } else {
            report = new ConditionEvaluationReport();
            beanFactory.registerSingleton(BEAN_NAME, report);
        }
        locateParent(beanFactory.getParentBeanFactory(), report);
        return report;
    }
}

From source file:com.rk.grid.cluster.slave.InjectionInterceptor.java

public void addBean(String name, Object bean) {
    if (this.context == null)
        this.context = new ClassPathXmlApplicationContext();
    context.refresh();/*from  www.  j  a  va2  s. com*/
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    beanFactory.registerSingleton(name, bean);
}

From source file:org.granite.client.tide.spring.SpringContextManager.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.registerSingleton("context", getContext());
    EntityManager entityManager = getContext().getEntityManager();
    entityManager.addListener(new SpringDataConflictListener());
    beanFactory.registerSingleton("entityManager", entityManager);
    beanFactory.registerSingleton("dataManager", getContext().getDataManager());
    for (Entry<String, Object> entry : getContext().getInitialBeans().entrySet())
        beanFactory.registerSingleton(entry.getKey(), entry.getValue());
    beanFactory.registerScope("view", new ViewScope());
}

From source file:org.dspace.servicemanager.spring.DSpaceBeanFactoryPostProcessor.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    // force config service to be registered first
    beanFactory.registerSingleton(ConfigurationService.class.getName(), configurationService);
    beanFactory.registerSingleton(ServiceManagerSystem.class.getName(), parent);
}

From source file:org.vertx.mods.spring.SpringModBase.java

@Override
public final void start() {

    super.start();

    // Make sure we're using the correct classloader
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    Assert.notNull(getContainer().getConfig(), "config object is null, which can't be good");
    String springConfig = getContainer().getConfig().getString("springConfig", "applicationConfig.xml");

    // Looks weird but avoids doing this twice below in the if/else block.
    // the vertx singleton needs to be registered before the context XML
    // is loaded or annotated @Configuration class is registered.
    ///* ww  w .j  a v a 2 s  . co m*/
    // I also like the analogy of the vertx instance being in a parent
    // context, as it's not instantiated in the context you really use.
    this.parent = new GenericApplicationContext();
    ConfigurableListableBeanFactory factory = parent.getBeanFactory();
    factory.registerSingleton("vertx", vertx);

    parent.refresh();
    parent.start();

    // Detect whether this app is configured with an XML or @Configuration.
    if (springConfig.endsWith(".xml")) {
        this.context = new GenericXmlApplicationContext();
        context.setParent(parent);

        ((GenericXmlApplicationContext) context).load(springConfig);
    } else {
        this.context = new AnnotationConfigApplicationContext();
        context.setParent(parent);

        try {
            Class<?> c = Class.forName(springConfig);
            ((AnnotationConfigApplicationContext) context).register(c);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(e);
        }
    }

    factory = context.getBeanFactory();
    BeanPostProcessor vertxSupportProcessor = new VertxAwareBeanPostProcessor(vertx);
    factory.addBeanPostProcessor(vertxSupportProcessor);

    beforeStartApplicationContext();

    context.refresh();
    context.start();
    context.registerShutdownHook();

    afterStartApplicationContext();
}

From source file:com.teradata.benchto.driver.jdbc.MultipleDataSourcesConfiguration.java

private void register(ConfigurableListableBeanFactory beanFactory, Map<String, DataSource> dataSources) {
    for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
        beanFactory.registerSingleton(entry.getKey(), entry.getValue());
    }//w w w. j  a v a  2 s  .  c om
}

From source file:org.github.aenygmatic.payroll.domain.annotations.postprocessing.CustomEnumAnnotationPostProcessor.java

private void registerCustomBeans(ConfigurableListableBeanFactory context, Object bean) {
    A component = bean.getClass().getAnnotation(annotationType());
    for (String name : enumToStringName(component)) {
        context.registerSingleton(name, bean);
    }//from  w ww .  j ava 2s . c om
}