【问题标题】:@PostConstruct doesnt work in Play Framework 2.4@PostConstruct 在 Play Framework 2.4 中不起作用
【发布时间】:2015-06-22 15:55:45
【问题描述】:

我需要为我的 bean 类提供一些配置。在常规 Spring 应用程序中,我使用 @PostConstruct 但它似乎不适用于 Play 2.4

我在我的 bean 类上使用 @Singleton 注解。我可以注入但 @PostConstruct 方法被忽略。如何将其他配置传递给我的 bean?

PS。方法定义如下:

void init() {
}

我已尝试将其设为 public\private 但没有任何帮助

谢谢

【问题讨论】:

  • 默认情况下 Spring 不知道 @PostConstruct 注释。要打开它,您必须注册 CommonAnnotationBeanPostProcessor 或在上下文配置中指定 <context:annotation-config />

标签: java playframework


【解决方案1】:

Play 和它的默认依赖注入实现 Guice 不支持通过来自 JSR 250: Common Annotations for the Java Platform 的注释进行完整的组件生命周期管理。他们只实现JSR 330: Dependency Injection for Java。但是 Play 有一些 limited component lifecycle support 并提供了在 play 应用程序关闭时进行组件清理的可能性。

对于为单例组件执行一些初始化任务的特定要求,我建议使用构造函数。 您甚至可以通过构造函数注入来注入其他组件。

@Singleton
public class MyComponent {
    @Inject
    Logger log;

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

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

【讨论】:

    猜你喜欢
    • 2015-08-20
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2015-11-08
    相关资源
    最近更新 更多