Example usage for org.springframework.beans.factory.support DefaultListableBeanFactory registerSingleton

List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory registerSingleton

Introduction

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

Prototype

@Override
    public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException 

Source Link

Usage

From source file:com.setronica.ucs.server.MessageServer.java

private static ClassPathXmlApplicationContext bootstrapPostgres(String url) throws UnknownHostException {
    //create parent BeanFactory
    DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
    //register your pre-fabricated object in it
    parentBeanFactory.registerSingleton("address", url);
    return initApplication("postgres", parentBeanFactory);
}

From source file:com.setronica.ucs.server.MessageServer.java

private static ClassPathXmlApplicationContext bootstrapMongo(String mongoUrls) throws UnknownHostException {
    List<ServerAddress> seeds = new ArrayList<>();
    for (String s : mongoUrls.split(",")) {
        int idx = s.indexOf(":");
        ServerAddress address;//from   w  ww  . j  av  a  2 s. com
        if (idx > 0) {
            address = new ServerAddress(s.substring(0, idx), Integer.parseInt(s.substring(idx + 1)));
        } else {
            address = new ServerAddress(s);
        }
        seeds.add(address);
    }
    //create parent BeanFactory
    DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
    //register your pre-fabricated object in it
    parentBeanFactory.registerSingleton("addresses", seeds);
    ClassPathXmlApplicationContext applicationContext = initApplication("mongo", parentBeanFactory);

    MongoClient client = (MongoClient) applicationContext.getBean("mongoClient");
    logger.info("addresses: " + client.getAllAddress());
    return applicationContext;
}

From source file:org.twinkql.template.AbstractTwinkqlTemplateFactory.java

/**
 * Gets the twinkql template./*from   w w w  .  ja va  2s.  c  o m*/
 *
 * @return the twinkql template
 * @throws Exception the exception
 */
public TwinkqlTemplate getTwinkqlTemplate() {

    DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();

    parentBeanFactory.registerSingleton("twinkqlContext", this.getTwinkqlContext());

    GenericApplicationContext parentContext = new GenericApplicationContext(parentBeanFactory);

    parentContext.refresh();

    AnnotationConfigApplicationContext annotationConfigApplicationContext = this
            .decorateContext(new AnnotationConfigApplicationContext());
    annotationConfigApplicationContext.setParent(parentContext);
    annotationConfigApplicationContext.scan(PACKAGE_SCAN);
    annotationConfigApplicationContext.refresh();

    TwinkqlTemplate template = annotationConfigApplicationContext.getBean(TwinkqlTemplate.class);

    return template;
}

From source file:org.openvpms.component.business.service.scheduler.JobRunner.java

/**
 * Creates the job.//  www .  j a v  a  2s  .  c  om
 *
 * @return a new job
 * @throws ClassNotFoundException if the job class cannot be found
 */
private Job createJob() throws ClassNotFoundException {
    Job result;
    IMObjectBean bean = new IMObjectBean(configuration, service);
    Class type = Class.forName(bean.getString("class"));
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory(context);
    factory.registerSingleton("jobConfiguration", configuration);
    Object job = factory.createBean(type, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true);
    if (job instanceof Runnable) {
        result = new DelegatingJob((Runnable) job);
    } else {
        result = (Job) job;
    }
    return result;
}

From source file:org.cloudfoundry.reconfiguration.spring.AbstractCloudServiceBeanFactoryPostProcessor.java

private boolean reconfigureBean(DefaultListableBeanFactory beanFactory, Class<?> beanClass, String beanName) {
    Object serviceConnector = getCloud().getSingletonServiceConnector(beanClass, null);
    beanFactory.registerSingleton(getServiceBeanName(), serviceConnector);
    beanFactory.removeBeanDefinition(beanName);
    beanFactory.registerAlias(getServiceBeanName(), beanName);

    this.logger.info(String.format("Reconfigured bean %s into singleton service connector %s", beanName,
            serviceConnector.toString()));

    return true;/*from   w  w w  .  jav a 2s . c  om*/
}

From source file:ch.rasc.wampspring.method.PayloadArgumentResolverTest.java

@SuppressWarnings("resource")
@Before//w ww .jav a 2s . c o  m
public void setup() throws Exception {
    DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory();
    dlbf.registerSingleton("mvcValidator", testValidator());
    GenericApplicationContext ctx = new GenericApplicationContext(dlbf);
    ctx.refresh();
    this.resolver = new PayloadArgumentResolver(ctx,
            new MethodParameterConverter(new ObjectMapper(), new GenericConversionService()));
    this.payloadMethod = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class,
            String.class, String.class, String.class, String.class, Integer.class);

    this.paramAnnotated = getMethodParameter(this.payloadMethod, 0);
    this.paramAnnotatedNotRequired = getMethodParameter(this.payloadMethod, 1);
    this.paramAnnotatedRequired = getMethodParameter(this.payloadMethod, 2);
    this.paramWithSpelExpression = getMethodParameter(this.payloadMethod, 3);
    this.paramValidated = getMethodParameter(this.payloadMethod, 4);
    this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    this.paramValidatedNotAnnotated = getMethodParameter(this.payloadMethod, 5);
    this.paramNotAnnotated = getMethodParameter(this.payloadMethod, 6);
}

From source file:org.brutusin.rpc.RpcSpringContext.java

@Override
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
    beanFactory.registerSingleton(SpringNames.WSKT_CONTAINER, this.sc);
}

From source file:org.carewebframework.ui.spring.FrameworkAppContext.java

/**
 * Override to return a custom bean factory. Also, for desktop context, registers a desktop bean
 * into application context and registers any placeholder configurers found in parent context
 * with the child context (which allows resolution of placeholder values in the child context
 * using a common configurer).//from  w ww .  j  a v a  2 s. c  o  m
 */
@Override
public DefaultListableBeanFactory createBeanFactory() {
    DefaultListableBeanFactory factory = new FrameworkBeanFactory(getParent(), getInternalParentBeanFactory());

    if (desktop != null) {
        factory.registerSingleton("desktop", desktop);
    }

    return factory;
}

From source file:com.fitbur.testify.di.spring.SpringServiceLocator.java

@Override
public void addConstant(String name, Object instance) {
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
    beanFactory.registerSingleton(name, instance);
}

From source file:com.fitbur.testify.di.spring.SpringServiceLocator.java

@Override
public void replaceWithConstant(Set<Class<?>> contracts, String name, Object instance) {
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();

    for (Class<?> contract : contracts) {
        String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, contract, true,
                false);//from w  w  w.  j a  va2s .  c  o  m
        for (String beanName : beanNames) {
            beanFactory.removeBeanDefinition(beanName);
        }
    }

    beanFactory.registerSingleton(name, instance);
}