如何使用Spring手动自动装配bean? [英] How do I manually autowire a bean with Spring?

查看:1151
本文介绍了如何使用Spring手动自动装配bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bean B 我必须自己创建(使用 new B() @Autowire @PostConstruct 注释。

I have a bean B which I have to create myself (using new B()) and which has @Autowire and @PostConstruct annotations.

如何做我在Spring中处理这些注释来自我的bean A

How do I make Spring process these annotations from my bean A?

相关问题:

  • In Spring, can I autowire new beans from inside an autowired bean?

推荐答案

Aaron,我相信你的代码是正确的,但我使用了以下内容:

Aaron, I believe that your code is correct but I used the following:

B bean = new B();
AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
factory.autowireBean( bean );
factory.initializeBean( bean, "bean" );

第一种方法将处理 @Autowire 字段和方法(但不是经典属性)。第二种方法将调用后处理( @PostConstruct 和任何已定义的 BeanPostProcessor )。

The first method will process @Autowire fields and methods (but not classic properties). The second method will invoke post processing (@PostConstruct and any defined BeanPostProcessors).

如果实现 ApplicationContextAware 接口,则可以在bean中获取应用程序上下文。

Application context can be obtained in a bean if it implements ApplicationContextAware interface.

这篇关于如何使用Spring手动自动装配bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆