简体   繁体   中英

Spring autowired object is null in postcontruct method

I have a below setup

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

}

What is happening ?

Try removing the extra curly-braces:

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

}

I suspect the initializer block, where you call b.someMethod() , is invoked prior to Spring wiring the dependencies. Therefore b is null at this point.

try to handle it using the context:

simple to use this interface

org.springframework.beans.factory.config.BeanPostProcessor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM