【发布时间】:2016-04-18 02:38:04
【问题描述】:
我完全不知所措!我有这个类:
package com.company.resources
import com.company.transport.Repository; //an interface for an EJB
import com.company.transport.Expression; //a simple DTO, returned by the Interface
public class ResourceProducer
{
//@EJB(lookup="foo") Repository repo;
@Produces @Named("archive")
public String getArchiveString() {
return "archive";
}
@Produces @Named("repository")
public Repository getRemoteRepository(){
//return repo;
return new Repository(){
@Override
public Expression getExpression(String s, Long aLong) {
return null;
}
};
}
}
而且,简单地把 String 一个工作,另一个被容器忽略(Wildfly 9 / Weld)。
一开始我想注入一个 EJB 并且getRemoteRepository 没有被注释为@Named,因为我只知道为接口Repository 提供一个生产者。得到错误后,我将它们更改为相同,以限制错误的范围,即使在注入点中也是如此:
package com.company.resources
public Class ExpressionProxy {
@Inject @Named("archive") String target;
@Inject @Named("repository") Repositroy repo;
}
我明白了:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Repository with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject com.company.resources.ExpressionProxy
我没有得到这个字符串的异常!
另外,如果我用@ApplicationScoped 注释ResourceProducer - 使其成为一个Bean - 我希望得到模棱两可的绑定 - 因为我的生产者现在通过@Produces 注释本身找到并且存在于托管Bean 中。
我也只得到字符串的模棱两可的绑定异常,从来没有Repository
我将两个类移到一个 .war 和同一个包中 - 它根本无法与 Repository 一起使用。
我之前通过接口进行 CDI 注入 - 这里有什么问题?
编辑:充分披露:
我将它部署为一个库:
app.ear/
-jaxrs.war # contains rest interface, irrelevant for this bug
-lib/
-beans.jar #contains the Producer and ExpressionProxy
-RepositoryInterface.jar # containts Repository and Expression
我尝试了所涉及的 3 个档案的所有排列。
- 提到的
-
beans和interfaces作为 .war 中的库 -
beans作为附加部署 -
beans作为附加部署并在 ear/lib 中
/lib 中的 bean 显然会被焊接扫描,因为 String 不会产生任何问题。
【问题讨论】:
-
你能澄清一下吗? “另外,如果我用
@ApplicationScoped注释 ResourceProducer - 使其成为 Bean - 我希望得到模棱两可的绑定 - 因为我的 Producer 现在是通过@Produces注释本身找到的,并且存在于托管 Bean 中。”——对我来说,添加@ApplicationScoped应该不会导致歧义绑定异常。
标签: java jboss cdi wildfly jboss-weld