Java Code Examples for org.springframework.beans.factory.config.SingletonBeanRegistry

The following examples show how to use org.springframework.beans.factory.config.SingletonBeanRegistry. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static void main(String[] args) throws InterruptedException {
    // 创建 BeanFactory 容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    // 创建一个外部 UserFactory 对象
    UserFactory userFactory = new DefaultUserFactory();
    SingletonBeanRegistry singletonBeanRegistry = applicationContext.getBeanFactory();
    // 注册外部单例对象
    singletonBeanRegistry.registerSingleton("userFactory", userFactory);
    // 启动 Spring 应用上下文
    applicationContext.refresh();

    // 通过依赖查找的方式来获取 UserFactory
    UserFactory userFactoryByLookup = applicationContext.getBean("userFactory", UserFactory.class);
    System.out.println("userFactory  == userFactoryByLookup : " + (userFactory == userFactoryByLookup));

    // 关闭 Spring 应用上下文
    applicationContext.close();
}
 
Example 2
public static void registerSingleton(ApplicationContext applicationContext, String beanName, Object singletonObject) {

    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    if (!SingletonBeanRegistry.class.isAssignableFrom(beanFactory.getClass())) {
      throw new IllegalArgumentException(
          "ApplicationContext: " + applicationContext.getClass().toString()
              + " doesn't implements SingletonBeanRegistry, cannot register JMS connection at runtime");
    }

    SingletonBeanRegistry beanDefinitionRegistry = (SingletonBeanRegistry) beanFactory;
    beanDefinitionRegistry.registerSingleton(beanName, singletonObject);

  }
 
Example 3
public static void registerSingleton(BeanDefinitionRegistry registry, String beanName, Object singletonObject) {

    if (!SingletonBeanRegistry.class.isAssignableFrom(registry.getClass())) {
      throw new IllegalArgumentException(
          "BeanDefinitionRegistry: " + registry.getClass().toString()
              + " doesn't implements SingletonBeanRegistry, cannot register JMS connection at runtime");
    }

    SingletonBeanRegistry beanDefinitionRegistry = (SingletonBeanRegistry) registry;
    beanDefinitionRegistry.registerSingleton(beanName, singletonObject);

  }
 
Example 4
Source Project: onetwo   Source File: SpringUtils.java    License: Apache License 2.0 5 votes vote down vote up
/*****
 * 获取SingletonBeanRegistry
 * @param applicationContext
 * @return
 */
public static SingletonBeanRegistry getSingletonBeanRegistry(Object applicationContext){
	Object bf = applicationContext;
	if(applicationContext instanceof AbstractApplicationContext){
		bf = ((AbstractApplicationContext)applicationContext).getBeanFactory();
	}
	if(bf==null || !SingletonBeanRegistry.class.isInstance(bf)){
		return null;
	}
	SingletonBeanRegistry sbr = (SingletonBeanRegistry) bf;
	return sbr;
}
 
Example 5
public SingletonBeanRegistry getSingletonBeanRegistry() {
    return (SingletonBeanRegistry) getRegistry();
}