2

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!

nyxz
  • 6,918
  • 9
  • 54
  • 67

3 Answers3

1

Your problem is that you are injecting an instance of Navigation in your class ValidateUserAuthenticationAction 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 two Navigation 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.

LightGuard
  • 5,298
  • 19
  • 19
1

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 your Navigation class (the name "navigation"). Annotate ExtendingNavigation 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.

class ValidateUserAuthenticationAction {
    @Inject
    @Named("extendedNavigation")
    private Navigation navigation;
}

Also, try to use interfaces! Navigation should be an interface. BasicNavigation should implement it, and ExtendedNavigation should also implement it.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Doug Moscrop
  • 4,479
  • 3
  • 26
  • 46
0

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.

enter image description here

CsBalazsHungary
  • 803
  • 14
  • 30