java—以编程方式将bean添加到SpringWebApp上下文

gxwragnw  于 2021-07-11  发布在  Java
关注(0)|答案(5)|浏览(425)

由于插件体系结构的原因,我正在尝试以编程方式将bean添加到我的webapp中。我有一个 Spring Bean 通过 @Component 注解,我正在实现 ApplicationContextAware 接口。
我的覆盖函数如下所示:

  1. @Override
  2. public void setApplicationContext(ApplicationContext applicationContext)
  3. throws BeansException {
  4. // this fails
  5. this.applicationContext = (GenericWebApplicationContext) applicationContext;
  6. }

基本上,我不知道如何将bean添加到给定给setapplicationcontext的applicationcontext对象中。有人能告诉我我是怎么做的吗?
好吧,这就是我最终的解决方案:

  1. @Override
  2. public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry bdr)
  3. throws BeansException {
  4. BeanDefinition definition = new RootBeanDefinition(
  5. <My Class>.class);
  6. bdr.registerBeanDefinition("<my id>", definition);
  7. }
gr8qqesn

gr8qqesn1#

在Spring3.0中,您可以使bean实现 BeanDefinitionRegistryPostProcessor 并通过 BeanDefinitionRegistry .
在以前版本的spring中,您可以在 BeanFactoryPostProcessor (尽管你需要 BeanFactoryBeanDefinitionRegistry ,可能会失败)。

bvuwiixz

bvuwiixz2#

下面是一个简单的代码:

  1. ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
  2. beanFactory.registerSingleton(bean.getClass().getCanonicalName(), bean);
ergxz8rk

ergxz8rk3#

你为什么要它是那种类型的 GenericWebApplicationContext ?
我认为您可能可以使用任何applicationcontext类型。
通常使用init方法(除了setter方法之外):

  1. @PostConstruct
  2. public void init(){
  3. AutowireCapableBeanFactory bf = this.applicationContext
  4. .getAutowireCapableBeanFactory();
  5. // wire stuff here
  6. }

你可以用这两种方法 AutowireCapableBeanFactory.autowire(Class, int mode, boolean dependencyInject)AutowireCapableBeanFactory.initializeBean(Object existingbean, String beanName)

9cbw7uwe

9cbw7uwe4#

事实上 AnnotationConfigApplicationContext 派生自 AbstractApplicationContext ,其中有空 postProcessBeanFactory 用于重写的方法

  1. /**
  2. * Modify the application context's internal bean factory after its standard
  3. * initialization. All bean definitions will have been loaded, but no beans
  4. * will have been instantiated yet. This allows for registering special
  5. * BeanPostProcessors etc in certain ApplicationContext implementations.
  6. * @param beanFactory the bean factory used by the application context
  7. */
  8. protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  9. }

要利用这一点,请创建 AnnotationConfigApplicationContextProvider 可能看起来像下面的类(为 Vertx 例如,您可以使用 MyClass 取而代之的是。。。

  1. public class CustomAnnotationApplicationContextProvider {
  2. private final Vertx vertx;
  3. public CustomAnnotationApplicationContextProvider(Vertx vertx) {
  4. this.vertx = vertx;
  5. }
  6. /**
  7. * Register all beans to spring bean factory
  8. *
  9. * @param beanFactory, spring bean factory to register your instances
  10. */
  11. private void configureBeans(ConfigurableListableBeanFactory beanFactory) {
  12. beanFactory.registerSingleton("vertx", vertx);
  13. }
  14. /**
  15. * Proxy method to create {@link AnnotationConfigApplicationContext} instance with no params
  16. *
  17. * @return {@link AnnotationConfigApplicationContext} instance
  18. */
  19. public AnnotationConfigApplicationContext get() {
  20. return new AnnotationConfigApplicationContext() {
  21. @Override
  22. protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  23. super.postProcessBeanFactory(beanFactory);
  24. configureBeans(beanFactory);
  25. }
  26. };
  27. }
  28. /**
  29. * Proxy method to call {@link AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(DefaultListableBeanFactory)} with our logic
  30. *
  31. * @param beanFactory bean factory for spring
  32. * @return
  33. * @see AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(DefaultListableBeanFactory)
  34. */
  35. public AnnotationConfigApplicationContext get(DefaultListableBeanFactory beanFactory) {
  36. return new AnnotationConfigApplicationContext(beanFactory) {
  37. @Override
  38. protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  39. super.postProcessBeanFactory(beanFactory);
  40. configureBeans(beanFactory);
  41. }
  42. };
  43. }
  44. /**
  45. * Proxy method to call {@link AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(Class[])} with our logic
  46. *
  47. * @param annotatedClasses, set of annotated classes for spring
  48. * @return
  49. * @see AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(Class[])
  50. */
  51. public AnnotationConfigApplicationContext get(Class<?>... annotatedClasses) {
  52. return new AnnotationConfigApplicationContext(annotatedClasses) {
  53. @Override
  54. protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  55. super.postProcessBeanFactory(beanFactory);
  56. configureBeans(beanFactory);
  57. }
  58. };
  59. }
  60. /**
  61. * proxy method to call {@link AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(String...)} with our logic
  62. *
  63. * @param basePackages set of base packages for spring
  64. * @return
  65. * @see AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(String...)
  66. */
  67. public AnnotationConfigApplicationContext get(String... basePackages) {
  68. return new AnnotationConfigApplicationContext(basePackages) {
  69. @Override
  70. protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  71. super.postProcessBeanFactory(beanFactory);
  72. configureBeans(beanFactory);
  73. }
  74. };
  75. }
  76. }

创建时 ApplicationContext 您可以使用

  1. Vertx vertx = ...; // either create or for vertx, it'll be passed to main verticle
  2. ApplicationContext context = new CustomAnnotationApplicationContextProvider(vertx).get(ApplicationSpringConfig.class);
展开查看全部
ohtdti5x

ohtdti5x5#

首先初始化属性值

  1. MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
  2. mutablePropertyValues.add("hostName", details.getHostName());
  3. mutablePropertyValues.add("port", details.getPort());
  4. DefaultListableBeanFactory context = new DefaultListableBeanFactory();
  5. GenericBeanDefinition connectionFactory = new GenericBeanDefinition();
  6. connectionFactory.setBeanClass(Class);
  7. connectionFactory.setPropertyValues(mutablePropertyValues);
  8. context.registerBeanDefinition("beanName", connectionFactory);

添加到bean列表

  1. ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
  2. beanFactory.registerSingleton("beanName", context.getBean("beanName"));

相关问题