下面显示的是一段代码,我尝试引用我的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
发布评论