zoukankan      html  css  js  c++  java
  • java手动注入bean到spring容器

    ApplicationContext applicationContext = SpringContextUtils.getApplicationContext();
    
    //将applicationContext转换为ConfigurableApplicationContext
    
    ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
    
    // 获取bean工厂并转换为DefaultListableBeanFactory
    
    DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
    
    this.defaultListableBeanFactory = defaultListableBeanFactory;
    
    String[] beanNamesForType = defaultListableBeanFactory.getBeanNamesForType(PayClient.class);
    
    System.out.println("beanNamesForType:" + Arrays.toString(beanNamesForType));
    
    // defaultListableBeanFactory.removeBeanDefinition("com.xxx.feign.PayClient");
    
    defaultListableBeanFactory.removeBeanDefinition(beanNamesForType[0]);
    
    // 通过BeanDefinitionBuilder创建bean定义
    
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(PayClient.class);
    
    // 设置属性userService,此属性引用已经定义的bean:userService,这里userService已经被spring容器管理了.
    
    // beanDefinitionBuilder.addPropertyReference("payClient", "payClient");
    
    // 注册bean
    
    defaultListableBeanFactory.registerBeanDefinition("com.xxx.feign.PayClient", beanDefinitionBuilder.getRawBeanDefinition());
    
    Object bean = SpringContextUtils.getBean(PayClient.class);
  • 相关阅读:
    Dos.ORM logo.Net轻量级开源ORM框架 Dos.ORM
    C# FUNC 应用
    WCF教程网址
    C#扩展方法实现 byte[] 输出为HEX字符串形式
    apache配置,禁止指定后缀访问
    IServerChannelSinkProvider
    在服务器端的远程对象中加上抽象工厂的接口和实现类
    pannel加载窗体
    权限框架
    工作周记
  • 原文地址:https://www.cnblogs.com/bevis-byf/p/14866538.html
Copyright © 2011-2022 走看看