下面顯示的是代碼片段,我嘗試引用我的ApplicationProperties bean。當我從構造函數中引用它時,它是null,但是當從另一個方法引用時它就沒有問題。直到現在,我還沒有在其他類中使用這個autowired bean沒有問題。但這是我第一次嘗試在另一個類的構造函數中使用它。@Autowired bean在另一個bean的構造函數中引用時爲null
在構造函數中調用applicationProperties下的代碼片段時,如果在轉換方法中引用它,則不是。我缺少
@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");
}
我們可以看到你的xml嗎? – Drahakar 2011-06-13 20:37:37