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
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................
}