[英]Spring autowired object is null in postcontruct method
我有一個下面的設置
@Service
public class TestDispatcherImpl implements Dispatcher <MobilePushNotification> {
@Autowired
private A a;
@Autowired
private B b;
@Autowired
private C c;
@PostConstruct
public void initialize() {} {
b.someMethod(); //GETTING A NULL POINTER EXCEPTION,
//in fact all the beans are null
}
}
public interface Dispatcher <T extends Notification> {
}
怎么了 ?
嘗試刪除多余的花括號:
public void initialize() {} {
^^
b.someMethod(); //GETTING A NULL POINTER EXCEPTION,
//in fact all the beans are null
}
我懷疑初始化程序塊(在其中您調用b.someMethod()
在Spring連接依賴項之前被調用。 因此, b
在這一點上為null
。
嘗試使用上下文來處理它:
簡單易用的界面
org.springframework.beans.factory.config.BeanPostProcessor
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.