@Autowired bean在另一个bean的构造函数中引用时为空(@Autowired bean is null when referenced in the constructor of ano

编程入门 行业动态 更新时间:2024-10-09 22:15:40
@Autowired bean在另一个bean的构造函数中引用时为空(@Autowired bean is null when referenced in the constructor of another bean)

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

在下面的代码片段中,applicationProperties在从构造函数调用时为null,但是在convert方法中引用它时不是。 我失踪了

@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的片段...

@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"); }

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.

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(); ...

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"); }

最满意答案

自动装配 (从Dunes注释的链接)发生在构建对象之后。 因此,在构造函数完成之后,它们将不会被置位。

如果你需要运行一些初始化代码,你应该可以将构造函数中的代码拉到一个方法中,并用@PostConstruct注释该方法。

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

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.

更多推荐

null,bean,public,private,电脑培训,计算机培训,IT培训"/> <meta name="desc

本文发布于:2023-08-07 11:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   函数   bean   Autowired   referenced

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!