春豆我有以下類:不能注入中
@Component("persistenceJPAConfig")
public class JPAPersistenceConfig {...}
使用Spring,我希望我能在我的目標類中添加使用@自動裝配Autowired的註釋的方法「注入」類使用JPAPersistenceConfig - Class。我工作得很好,班級本身很好。
問題是,我必須在運行時創建一個類(編寫源代碼並編譯它),它也必須使用這個bean。
我也添加了autowired-methods,但它始終爲空。
@org.springframework.stereotype.Component("Customers")
public class Customers {
public Customers() {
}
private org.product.server.database.JPAPersistenceConfig persistenceJPAConfig;
@org.springframework.beans.factory.annotation.Autowired
public void setPersistenceJPAConfig(org.product.server.database.JPAPersistenceConfig persistenceJPAConfig) {
this.persistenceJPAConfig = persistenceJPAConfig;
}
public void someMethod() {
this.persistenceJPAConfig.getClassByName(...)
// Throws nullpointer
}
}
我是否也必須使用@Component標記生成的文件,以便它在Spring中成爲「託管bean」?它在運行時如何在Spring中註冊?
拼命你:-)
你可以發表你的Spring bean定義文件? – ElderMael
如何在運行時創建新類? – vacuum