2
votes

I need to provide some configuration to my bean class. In regular Spring application I'm using @PostConstruct but it not seems to be working in Play 2.4

I use @Singleton annotation on my bean class. I can inject in but @PostConstruct method is ignored. How can I pass additional configuration to my bean?

PS. the method is defined as follows:

void init() {
}

I've tried make it public\private but nothing helps

Thanks

1
By default Spring isn't aware of the @PostConstruct annotation. To turn it on you have to either register CommonAnnotationBeanPostProcessor or specify the <context:annotation-config /> in your context configuration. - Kris

1 Answers

3
votes

Play and it's default Dependency Injection implementation Guice don't support full component lifecylce management via annotations from JSR 250: Common Annotations for the Java Platform. They only implement JSR 330: Dependency Injection for Java. However Play has some limited component lifecycle support and offers the possibility to do component cleanup when the play application shuts down.

For your specific requirement to do some initialization tasks for a singleton component I would recommend to use the constructor. You can even inject other components via constructor injection.

@Singleton
public class MyComponent {
    @Inject
    Logger log;

    @Inject
    public MyComponent(MyConfiguration conf) {
        conf.load();
        init();
    }

    public void init(){
        log.info("init");
    }
}