在postcontruct方法中,Spring自动对象对象为null

时间:2014-08-21 11:10:24

标签: java spring dependency-injection postconstruct

我有以下设置

@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> {

}

发生了什么事?

2 个答案:

答案 0 :(得分:4)

尝试删除多余的大括号:

public void initialize() {} {
                         ^^
 b.someMethod(); //GETTING A NULL POINTER EXCEPTION, 
                 //in fact all the beans are null

}

我怀疑在Spring连接依赖项之前调用初始化块,在这里调用b.someMethod()。因此,b此时为null

答案 1 :(得分:0)

尝试使用上下文来处理它:

使用此界面简单

org.springframework.beans.factory.config.BeanPostProcessor