0
votes

I have a bean configured which have some initialization logic. I have annotated this bean using @ApplicationScoped annotation. But somehow, cdi is not picking this bean.

beans.xml content:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="annotated">
</beans>

Bean file:

@ApplicationScoped
public class Initializer{

    @Inject @ConfigProperty(name = "app.name")
    private String appName;
    @Inject @ConfigProperty(name = "app.token")
    private String appToken;
    @Inject @ConfigProperty(name = "app.version")
    private String appVersion;

    @PostConstruct
    public void init() {
       System.out.println("flow should come here....); //but this line does not execute.
    }
}

Code to read config file:

@Exclude
public class ConfigurationFile implements PropertyFileConfig {

    @Override
    public String getPropertyFileName() {
    String env = Util.getEnv();
    switch (env) {
    case "dev":
    case "uat":
    case "prod":
        return "config/" + env + "/default.properties";
    default:
        return "config/default.properties";
    }
    }

    @Override
    public boolean isOptional() {
    return false;
    }
}

I am using: cdiL: for dependency injection, apache-deltaspike: for reading config file. wildfly-swarm: server

1
Just adding a scope does not make it a managed bean. Add a @Named annotation - Kukeltje
I'm not sure what cdiL is, so hard to comment on what the issue might be - Ken
Are you actually using that bean somehow? Weld (CDI impl you are using) is lazily initiated and will not create the actual bean until needed, e.g. it won't call @PostConstruct - add some dummy method on that bean and try invoking it. - Siliarus
Thanks for the input. - G.G.
At Kukeltje: That's not right. The @Named is just optional to access the bean via EL. - Alex Fire

1 Answers

0
votes

I have got the solution to this problem.

Issue is solved by changing the method declaration as follows:

public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
//................code logic here................
}