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

The following examples show how to use org.springframework.beans.factory.config.SingletonBeanRegistry. 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
Source File: SingletonBeanRegistrationDemo.java    From geekbang-lessons with Apache License 2.0 6 votes vote down vote up
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
Source File: BeanRegisterUtils.java    From artemis-disruptor-miaosha with Apache License 2.0 5 votes vote down vote up
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
Source File: BeanRegisterUtils.java    From artemis-disruptor-miaosha with Apache License 2.0 5 votes vote down vote up
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 File: SpringUtils.java    From onetwo with 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
Source File: ExposingClassPathBeanDefinitionScanner.java    From spring-context-support with Apache License 2.0 4 votes vote down vote up
public SingletonBeanRegistry getSingletonBeanRegistry() {
    return (SingletonBeanRegistry) getRegistry();
}