扩展@Named @SessionScoped bean
我有名为 Navigation
的父类和名为 ExtendingNavigation
的子类。 Navigation
注释为 @Named("navigation")
和 @SessionScoped
(javax.enterprise.context.SessionScoped;)。在其中,我注入 UserBean
并在 login()
方法中初始化它。假设我想在其他类中使用 userBean
。我该怎么做呢?为什么我无法从 Navigation
进行扩展?
以下是类(仅用于示例):
Navigation
@Named("navigation")
@SessionScoped
public class Navigation implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
protected UserBean userBean;
public void logout() {
userBean = null;
}
public void login() {
EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
userBean = em.find(UserBean.class, userBean.getUsername());
}
}
ExtendingNavigation
public class ExtendingNavigation extends Navigation{
}
如果我尝试编译它,则会出现错误。
17:01:29,956 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-7) MSC00001: Failed to start service jboss.deployment.unit."timereport.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."timereport.war".WeldService: org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [NavigationBean] with qualifiers [@Default] at injection point [[field] @Inject timereport.ui.biz.faces.context.ValidateUserAuthenticationAction.navigation]. Possible dependencies [[Managed Bean [class timereport.navigation.NavigationBean] with qualifiers [@Any @Default @Named], Managed Bean [class timereport.navigation.ExtendingNavigation] with qualifiers [@Any @Default]]]
at org.jboss.as.weld.services.WeldService.start(WeldService.java:96)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885) [:1.6.0_07]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907) [:1.6.0_07]
at java.lang.Thread.run(Thread.java:619) [:1.6.0_07]
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [NavigationBean] with qualifiers [@Default] at injection point [[field] @Inject timereport.ui.biz.faces.context.ValidateUserAuthenticationAction.navigation]. Possible dependencies [[Managed Bean [class timereport.navigation.NavigationBean] with qualifiers [@Any @Default @Named], Managed Bean [class timereport.navigation.ExtendingNavigation] with qualifiers [@Any @Default]]]
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:274)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:129)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:351)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:336)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:404)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:82)
at org.jboss.as.weld.services.WeldService.start(WeldService.java:89)
... 5 more
请帮忙!
I have parent class called Navigation
and child class called ExtendingNavigation
. Navigation
is annotated as @Named("navigation")
and @SessionScoped
(javax.enterprise.context.SessionScoped;). In it I inject UserBean
and initialize it in the login()
method. Lets say that I want to use the userBean
in other classes. How can I do it? And why I can't extend from Navigation
?
Here are the classes (just for the example) :
Navigation
@Named("navigation")
@SessionScoped
public class Navigation implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
protected UserBean userBean;
public void logout() {
userBean = null;
}
public void login() {
EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
userBean = em.find(UserBean.class, userBean.getUsername());
}
}
ExtendingNavigation
public class ExtendingNavigation extends Navigation{
}
If I try to compile this it gives me an error.
17:01:29,956 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-7) MSC00001: Failed to start service jboss.deployment.unit."timereport.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."timereport.war".WeldService: org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [NavigationBean] with qualifiers [@Default] at injection point [[field] @Inject timereport.ui.biz.faces.context.ValidateUserAuthenticationAction.navigation]. Possible dependencies [[Managed Bean [class timereport.navigation.NavigationBean] with qualifiers [@Any @Default @Named], Managed Bean [class timereport.navigation.ExtendingNavigation] with qualifiers [@Any @Default]]]
at org.jboss.as.weld.services.WeldService.start(WeldService.java:96)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885) [:1.6.0_07]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907) [:1.6.0_07]
at java.lang.Thread.run(Thread.java:619) [:1.6.0_07]
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [NavigationBean] with qualifiers [@Default] at injection point [[field] @Inject timereport.ui.biz.faces.context.ValidateUserAuthenticationAction.navigation]. Possible dependencies [[Managed Bean [class timereport.navigation.NavigationBean] with qualifiers [@Any @Default @Named], Managed Bean [class timereport.navigation.ExtendingNavigation] with qualifiers [@Any @Default]]]
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:274)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:129)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:351)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:336)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:404)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:82)
at org.jboss.as.weld.services.WeldService.start(WeldService.java:89)
... 5 more
Help pls!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题不在于
UserBean
的注入,您绝对可以在其他地方使用它。问题是您的子类
ExtendingNavigation
与您的Navigation
类具有相同的限定符(名称“navigation”)。使用@Named("extendedNavigation")
或类似内容注释ExtendingNavigation
,然后在旁边指定
在你想使用它的地方。@Named("extendedNavigation")
@Inject可以这样想:您说“给我一个 X 类的实例”,而 Weld 说“我不知道您想要哪一个”,因为实际上有两个类是导航。
另外,尝试使用接口!导航应该是一个界面。 BasicNavigation 应该实现它,ExtendNavigation 也应该实现它。
The problem isn't the injection of the
UserBean
, you absolutely can use it in other places.The problem is that your subclass
ExtendingNavigation
has the same qualifier as yourNavigation
class (the name "navigation"). AnnotateExtendingNavigation
with@Named("extendedNavigation")
or something like that and then specify that@Named("extendedNavigation")
alongside@Inject
where you wish to use it.Think of it this way: you're saying "Give me an instance of class X" and Weld is saying "I don't know which one you want" because there are actually two classes that are a Navigation.
Also, try to use interfaces! Navigation should be an interface. BasicNavigation should implement it, and ExtendedNavigation should also implement it.
您的问题是您正在类
ValidateUserAuthenticationAction
中注入Navigation
的实例,但是,您有两个不同的类可以在该注入点使用,就像主要异常一样州。解决此问题的最简单方法是向注入点(如果您选择)和两个Navigation
类之一添加限定符。限定符是这个问题的解决方案,也是它们在规范中的全部意义。限定符为注入点添加了额外的歧义,或者换句话说,它有助于专门化类和注入点。
Your problem is that you are injecting an instance of
Navigation
in your classValidateUserAuthenticationAction
however, you have two different classes that can be used at that injection point, just as the main exception states. The simplest way of solving this is to add a qualifier to the injection point (if you choose) and to one of the twoNavigation
classes.Qualifiers are the solution to this problem, and their whole point of being in the spec. A qualifier adds additional disambiguation for an injection point, or in other words it helps specialize a class and an injection point.
我遇到了非常相似的问题,我得到了一些离线帮助。我的问题是,我的服务所在的地方,它也包含在部署的耳朵中以及我的网络项目中。这是一个意外的重复,删除一个,如果这也是你的情况,它会起作用。
在下图中,我将 esb_khr 放在 esb_khr_web 中,我将其删除。在 Eclipse 中:转到属性和部署程序集。
I had very similar problem, and I got some offline help. My problem was that where my service was, it was included in a deployed ear AND in my web project as well. It was an accidental duplication, drop one out, and it will work if it is your case as well.
here on the following picture I had the esb_khr inside the esb_khr_web, I removed. In eclipse: go to properties and deployment assembly.