简体   繁体   English

具有Application Scope的CDI bean无法识别会话Bean的名称

[英]CDI bean with Application Scope doesn't recognize name of the Session Bean

I am getting an error in the AllUsersBean class in the default constructor. 我在默认构造函数的AllUsersBean类中遇到错误。 It doesn't recognize subBean in this line users.add(subBean.clone()); 它在此行中无法识别subBean users.add(subBean.clone()); I am using subBi.clone to clone submited info by the user to the UsersList that is called users. 我正在使用subBi.clone将用户提交的信息克隆到称为用户的UsersList中。 Thank you for any advice. 感谢您的任何建议。

This is error: 这是错误的:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-
compile) on project Test: Compilation failure
[ERROR] ......(here was path to this class) AllUsersBean.java:[25,18] cannot find symbol
[ERROR] symbol:   variable subBean

To solve the problem I trid to add getter and setter for appBean in UserBean class but it doesn't work. 为了解决该问题,我尝试在UserBean类中为appBean添加getter和setter,但是它不起作用。

package testpkg;

/*Importing required java libraries */
import javax.inject.Named; 
import javax.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.List;

@Named("appBean")
@ApplicationScoped
public class AllUsersBean {

   private List<UserBean> users;

   public AllUsersBean(){
      users = new ArrayList<UserBean>();
      users.add(subBean.clone());
   }

   public List<UserBean> getUsers() {
      return users;
   }

   public void setUsers(List<UserBean> users) {
      this.users = users;
   }
}


package testpkg;

/*Importing required java libraries */
import java.io.Serializable;
import javax.inject.Named; 
import javax.inject.Inject; 
import javax.enterprise.context.SessionScoped;

@Named("subBean")
@SessionScoped
public class UserBean implements Serializable{

   private String fname;

   @Inject
   private AllUsersBean appBean;

   public UserBean() {
     this.fname = null;
   }

   public String getFname() {
       return fname;
   }

   public void setFname(String fname) {
       this.fname = fname;
   }

    public AllUsersBean getAppbean() {
       return appBean;
   }

   public void setAppbean(AllUsersBean appBean) {
       this.appBean =appBean;
   }
}

Have you tried to create a field with name subBean to AllUsersBean and make you initialization in method with annotation @PostConstruct? 您是否尝试过创建名称为AllUsersBean的subBean的字段,并使用带有@PostConstruct批注的方法进行初始化? And in general using clone is not a good practice 通常,使用克隆不是一个好习惯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM