gpt4 book ai didi

java - 扩展@Named @SessionScoped bean

转载 作者:行者123 更新时间:2023-11-30 04:56:57 26 4
gpt4 key购买 nike

我有名为 Navigation 的父类和名为 ExtendingNavigation 的子类。 Navigation 注释为 @Named("navigation")@SessionScoped (javax.enterprise.context.SessionScoped;)。在其中,我注入(inject) UserBean 并在 login() 方法中初始化它。假设我想在其他类中使用 userBean 。我该怎么做?为什么我无法从 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());
}
}

扩展导航

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

请帮忙!

最佳答案

问题不在于UserBean的注入(inject),你绝对可以在其他地方使用它。

问题是您的子类 ExtendingNavigation 与您的 Navigation 类具有相同的限定符(名称“navigation”)。使用 @Named("extendedNavigation") 或类似内容注释 ExtendingNavigation,然后在 旁边指定 @Named("extendedNavigation") @Inject 你想在哪里使用它。

这样想:您说“给我一个 X 类的实例”,而 Weld 说“我不知道您想要哪个”,因为实际上有两个类是导航。

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

另外,尝试使用接口(interface)!导航应该是一个界面。 BasicNavigation 应该实现它,ExtendNavigation 也应该实现它。

关于java - 扩展@Named @SessionScoped bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8244564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com