简体   繁体   English

在焊接中如何在注入之前初始化对象

[英]In weld how do I initialize an object before its injected

I have a simple POJO class that I inject in to many places. 我有一个简单的POJO类,我注入了很多地方。 There is no explicit Producer for it. 它没有明确的生产者。 I just do @Inject POJO mypojo and it works great. 我只是做@Inject POJO mypojo而且效果很好。

Now my problem is that I want to initialize the POJO object(which involves reading from a datasource) before its injected to other places. 现在我的问题是我想在注入其他地方之前初始化POJO对象(涉及从数据源读取)。 The datasource itself is injected as @Resource(name = "jdbc/xx") DataSource ds; 数据源本身注入为@Resource(name = "jdbc/xx") DataSource ds; within the POJO. 在POJO内。 Now in the constructor of my POJO, the ds is null, its only injected after the constructor is complete. 现在在我的POJO的构造函数中, ds为null,它仅在构造函数完成后注入。

Is there a hook I can get after creating of the object and before injection so I can initialize my object before injection? 在创建对象之后和注射之前是否有一个钩子,所以我可以在注射之前初始化我的对象?

This is what the @PostConstruct annotation is for. 这就是@PostConstruct注释的用途。 It is called after your bean is constructed by the CDI container, but before it is actually placed into service. 它是在您的bean由CDI容器构造之后但在实际投入使用之前调用的。 Example: 例:

public class POJO {
    public Pojo() {
        super();
    }

    @PostConstruct
    protected void initialize() {
        // initialization code here
    }
}

Documentation: http://docs.oracle.com/javaee/6/api/javax/annotation/PostConstruct.html 文档: http//docs.oracle.com/javaee/6/api/javax/annotation/PostConstruct.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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