注入点带有限定符[@Default]的类型[...]的不满意依赖性(使用@Stateful EJB和CDI) [英] Unsatisfied dependencies for type [...] with qualifiers [@Default] at injection point (using @Stateful EJB with CDI)
问题描述
我有以下代码来管理两种存储库。这两个存储库类都继承一个接口,以允许对其资源进行重新初始化。
I have the following code to manage two kinds of repositories. Both repository classes inherit an interface to allow reinitialization of their resources.
public interface CachingRepository
{
public void invalidateCache();
}
应用程序范围的全局回购:
Global, application-scoped repo:
@Named("globalRepo")
@ApplicationScoped
public class GlobalRepository implements CachingRepository
{
private List<Category> categories;
...
@Override
public void invalidateCache()
{
categories = null;
}
...
}
每个用户的会话范围的存储库:
Per user, session-scoped repo:
@Named("userRepo")
@SessionScoped
//@Stateful // <- NOTE HERE
public class UserRepository implements CachingRepository, Serializable
{
private List<MyFile> files;
@Override
public void invalidateCache()
{
files = null;
}
...
}
将其(不带 @Stateful
)注入上下文时
When injecting this (without @Stateful
) into the context
@Named
@ViewScoped
public class MyHandler implements Serializable
{
@Inject
private UserRepository userRepo;
...
}
它有效。但是,将 @Stateful
添加到 UserRepository
类时,部署失败,并显示以下异常:
it works. However, when adding @Stateful
to the UserRepository
class, deployment fails with an exception saying:
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [UserRepository] with qualifiers [@Default] at injection point [[field] @Inject private de.company.project.pack.MyHandler.userRepo]
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:275)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:244)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:107)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:127)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:346)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:331)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:366)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:83)
at org.jboss.as.weld.services.WeldService.start(WeldService.java:76)
... 5 more
添加CDI bean的名称,例如
Adding the name of the CDI bean like
@Inject @Named("userRepo")
private UserRepository userRepo;
结果相同。与 @Stateful
一起使用的唯一方法是使用var声明中的接口:
results in the same exception. The only thing that works in conjunction with @Stateful
is to use the interface in the var declaration:
@Inject @Named("userRepo")
private CachingRepository userRepo;
但是我可能在这里需要子类功能,所以使用 CachingRepository $
I might need sub class functionality here however, so using CachingRepository
isn't really desired (at the moment).
Q's :
- 为什么这不能正常工作?
UserRepository
var应该已经标识了要实例化的类,不是吗? - 为什么
@Stateful
EJB注释在这里产生如此严重的影响?为什么从本质上迫使我在var声明中使用CachingRepository
接口?
- Why isn't this working as expected? The
UserRepository
var should already identify which class to instantiate, shouldn't it? What's the logic to this? - Why does the
@Stateful
EJB annotation have such severe effects here? Why does it essentially force me into using theCachingRepository
interface in the var declaration?
< hr>
注意,我使用Seam 3 Faces使 @ViewScoped
成为CDI视图,作用域Bean,因此当前的问题可能仍然是仅CDI 。
Note, I' using Seam 3 Faces making the @ViewScoped
become a CDI view-scoped bean, so the problem at hand is likely still CDI-only.
推荐答案
与此误导性异常相同的问题...
I had the same problem with this misleading exception...
通过将 @Stateful
添加到 UserRepository
,您可以公开 CachingRepository
接口的EJB方法,而无需声明无接口视图。将 @LocalBean
添加到 UserRepository
以激活无接口视图。请参见EJB 3.1规范的第4.9.8节会话Bean的无接口视图
By adding @Stateful
to UserRepository
you expose EJB methods of the CachingRepository
interface without having a no-interface view declared. Add @LocalBean
to UserRepository
to activate the no-interface view. See EJB 3.1 Specification, Section 4.9.8 "Session Bean's No-Interface View"
Bean类必须指定它公开了无-接口视图通过其bean类定义或在部署描述符中进行。适用以下规则:
The bean class must designate that it exposes a no-interface view via its bean class definition or in the deployment descriptor. The following rules apply:
- ...
- 如果bean暴露了至少一个其他客户端视图中,bean
表示它通过bean类或部署
描述符中的
@LocalBean注释公开无接口视图。 - ...
我也指的是此stackoverflow 答案可提供有关无接口视图的更多信息。
I also refer to this stackoverflow answer for more information about no-interface views.
这篇关于注入点带有限定符[@Default]的类型[...]的不满意依赖性(使用@Stateful EJB和CDI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!