2

我正在我们公司做项目,我在注入对象时遇到问题。让我们考虑一下我有这个实体提供者:

@Stateless
@TransactionManagement 
public class EntityProviderBean<T> extends CachingMutableLocalEntityProvider<T> {

    public EntityProviderBean(Class<T> entityClass) {
        super(entityClass);
        setTransactionsHandledByProvider(false);
    }

    @PersistenceContext(unitName = CoreApplication.PERSISTENCE_UNIT)
    private EntityManager em;

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    protected void runInTransaction(Runnable operation) {
        super.runInTransaction(operation);
    }

    @PostConstruct
    public void init() {
        setEntityManager(em);
        setEntitiesDetached(false);
    }
}

并使用上面的实体提供者扩展了 JPAContainer

@UIScoped
public class IncidentContainer extends JPAContainer<Incident> {

    private static final long serialVersionUID = 8360570718602579751L;

    @Inject
    EntityProviderBean<Incident> provider;

    public IncidentContainer() {
        super(Incident.class);
    }

    @PostConstruct
    protected void init() {
        setEntityProvider(provider);
    }

}

问题是(我理解)我无法使用类类型定义 @Inject 对象,因为注入方法需要空白构造函数。这里有某种解决方案如何使它起作用吗?现在我得到了例外

org.apache.webbeans.util.InjectionExceptionUtils.throwUnsatisfiedResolutionException(InjectionExceptionUtils.java:77)

非常感谢您的回答:) Ondrej

4

2 回答 2

1

AFAIK Bean 需要一个没有参数的构造函数才能注入,或者所有构造函数参数也必须注入。您将无法满足这些要求。

于 2013-11-15T15:04:09.587 回答
0

构造注入

当 CDI 容器实例化一个 bean 类时,它会调用 CDI bean 的 bean 构造函数。

CDI 查找默认的 bean 构造函数或使用 @Inject 注释来获取 bean 的实例。

  • 如果 CDI bean 没有使用 @Inject 显式声明构造函数,则 CDI 容器不会接受任何参数/默认 bean 构造函数。
  • CDI bean 构造函数可以有任意数量的参数,并且容器初始化/注入所有这些
    参数,这些参数是 bean 构造函数的注入点。
  • CDI bean 类只能有一个用@Inject 注释的构造函数。如果 CDI 容器发现多个使用 @Inject 注释的构造函数,则会引发错误。

bean 构造函数注入的一个优点是它允许 bean 是不可变的。

你的问题是

IncidentContainer bean 类没有任何默认构造函数或使用@Injection 注释的构造函数。

您可以设置如下

public class IncidentContainer extends JPAContainer<Incident> {

    // @Inject no need to do filed injection here
    private EntityProviderBean<Incident> provider;

    @Inject // add this one
    public IncidentContainer(EntityProviderBean<Incident> provider) {
        super(Incident.class);
        this.provider = provider;
    }

    @PostConstruct
    protected void init() {
        setEntityProvider(provider);
    }
}

EntityProviderBean bean 类没有任何默认构造函数或使用@Injection 注解的构造函数。

您可以设置如下

public class EntityProviderBean<T> extends CachingMutableLocalEntityProvider<T> {

    public EntityProviderBean(Class<T> entityClass) {
        super(entityClass);
        setTransactionsHandledByProvider(false);
    }
    // add this cdi bean construtor
    @Inject
    public EntityProviderBean(Incident incident) {
        this((Class<T>) incident.getClass());
    }

    protected void runInTransaction(String operation) {
        super.runInTransaction(operation);
    }
}
于 2013-11-18T10:25:38.123 回答