我配置了一个bean,它有一些初始化逻辑。我使用@ApplicationScoped注释注释了这个bean。但不知何故,cdi并没有采摘这种豆。
beans.xml内容:
<?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文件:
@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.
}
}
读取配置文件的代码:
@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;
}
}
我正在使用: cdiL:用于依赖注入, apache-deltaspike:用于读取配置文件。 wildfly-swarm:服务器
答案 0 :(得分:0)
我已经解决了这个问题。
通过更改方法声明解决问题,如下所示:
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
//................code logic here................
}