繁体   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