@Autowired bean 在另一个 bean 的构造函数中引用时为 null [英] @Autowired bean is null when referenced in the constructor of another bean

查看:26
本文介绍了@Autowired bean 在另一个 bean 的构造函数中引用时为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示的是我尝试引用我的 ApplicationProperties bean 的代码片段.当我从构造函数引用它时它是空的,但是当从另一个方法引用时它很好.到目前为止,我在其他类中使用这个自动装配的 bean 没有任何问题.但这是我第一次尝试在另一个类的构造函数中使用它.

Shown below is a snippet of code where I try and reference my ApplicationProperties bean. When I reference it from the constructor it is null, but when referenced from another method it is fine. Up until now I have not had no problem using this autowired bean in other classes. But this is the first time I have tried to use it in the constructor of another class.

在下面的代码片段中,当从构造函数调用时,applicationProperties 为 null,但在 convert 方法中引用时却不是.我错过了什么

In the code snippet below applicationProperties is null when called from the constructor but when referenced in the convert method it is not. What am I missing

@Component
public class DocumentManager implements IDocumentManager {

  private Log logger = LogFactory.getLog(this.getClass());
  private OfficeManager officeManager = null;
  private ConverterService converterService = null;

  @Autowired
  private IApplicationProperties applicationProperties;


  // If I try and use the Autowired applicationProperties bean in the constructor
  // it is null ?

  public DocumentManager() {
  startOOServer();
  }

  private void startOOServer() {
    if (applicationProperties != null) {
      if (applicationProperties.getStartOOServer()) {
        try {
          if (this.officeManager == null) {
            this.officeManager = new DefaultOfficeManagerConfiguration()
              .buildOfficeManager();
            this.officeManager.start();
            this.converterService = new ConverterService(this.officeManager);
          }
        } catch (Throwable e){
          logger.error(e);  
        }
      }
    }
  }

  public byte[] convert(byte[] inputData, String sourceExtension, String targetExtension) {
    byte[] result = null;

    startOOServer();
    ...

下面是来自 ApplicationProperties 的片段......

Below is s snippet from ApplicationProperties ...

@Component
public class ApplicationProperties implements IApplicationProperties {

  /* Use the appProperties bean defined in WEB-INF/applicationContext.xml
   * which in turn uses resources/server.properties
   */
  @Resource(name="appProperties")
  private Properties appProperties;

  public Boolean getStartOOServer() {
    String val = appProperties.getProperty("startOOServer", "false");
    if( val == null ) return false;
    val = val.trim();
    return val.equalsIgnoreCase("true") || val.equalsIgnoreCase("on") || val.equalsIgnoreCase("yes");
  }

推荐答案

Autowiring(来自 Dunes 评论的链接)在对象构建之后发生.因此,只有在构造函数完成后才会设置它们.

Autowiring (link from Dunes comment) happens after the construction of an object. Therefore they will not be set until after the constructor has completed.

如果你需要运行一些初始化代码,你应该能够将构造函数中的代码拉入一个方法中,并用@PostConstruct.

If you need to run some initialization code, you should be able to pull the code in the constructor into a method, and annotate that method with @PostConstruct.

这篇关于@Autowired bean 在另一个 bean 的构造函数中引用时为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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