我已经定义了一个限定符来指定我想要注入的 bean 子类。当限定符在注入点没有指定参数时,一切都按预期工作。当我在注入点指定一个属性和值时,我得到一个 CDI 不满足的依赖关系错误。我在 Glassfish 4.0 上进行部署。此外,在注入的 bean 上使用无接口视图,但我认为在这种情况下并不重要。
限定符的目的是获得一个 Postgres 特定的子类(正在使用各种不同的数据库,以比较任何行为差异)。该参数用于指定 Postgres 模式(默认为“public”)。
限定符的定义如下Postgres.java
:
@Qualifier
@Retention(RUNTIME)
@Target({ FIELD, TYPE, METHOD })
public @interface Postgres
{
String schema() default "public";
}
注入的子类如下所示DBCmdPostgres.java
:
@Stateless
@Postgres
public class DBCmdPostgres
extends DBCmd
{
...
@PostConstruct
public void postConstruct()
{
Class cl = getClass();
Postgres postgresAnnotation = (Postgres)cl.getAnnotation(Postgres.class);
if (postgresAnnotation != null) {
System.out.println(">> @Postgres(schema = " + postgresAnnotation.schema() +")");
}
}
...
}
基类如下所示DBCmd.java
:
@Stateless
@Default
abstract public class DBCmd
{
...
}
注入点在 JSF 会话范围的支持 bean 中indexBacker.java
:
@Inject @Postgres(schema = "foo") private DBCmd postgresCmds;
如果我在注入点省略了(schema = "foo")
,事情会像我预期的那样工作,并且 PostConstruct 方法会显示正确的默认值“public”。如果我添加该(schema = "foo")
部件,我会在部署到 Glassfish 时收到以下错误:
[2013-09-18T08:36:24.209-0500] [glassfish 4.0] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=36 _ThreadName=admin-listener(3)] [timeMillis: 1379511384209] [levelValue: 1000] [[
Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [DBCmd] with qualifiers [@Postgres] at injection point [[BackedAnnotatedField] @Inject @Postgres private jsfBackers.IndexBacker.postgresCmds]
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [DBCmd] with qualifiers [@Postgres] at injection point [[BackedAnnotatedField] @Inject @Postgres private jsfBackers.IndexBacker.postgresCmds]
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:403)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:325)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:177)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:208)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:519)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:505)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:480)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:536)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:216)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
...
这些 bean 当前是,但如果我们能找出问题所在@Stateless
,它们将会变成@Stateful
(存储/使用参数的值以及其他一些东西)。schema