簡體   English   中英

Spring自動裝配對象在后構造方法中為null

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM