0

当我注入了 AdminService 时,出现了 java.lang.NullPointerException ,尽管我以相同的方式在另一个托管 bean 中注入了这个 bean 并且一切正常:

    @ManagedBean
    @SessionScoped
    public class ScheduleController implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = -1489523494215832724L;

        private ScheduleModel eventModel;

        @ManagedProperty(value = "#{adminService}")
        AdminService adminService;

        private ScheduleEvent event = new DefaultScheduleEvent();

        public ScheduleController() {
            List<Service> = adminService.getAllService();

        }

    public AdminService getAdminService() {
        return adminService;
    }


    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

管理服务实现:

Service("adminService")
public class AdminServiceImpl implements AdminService,Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    AdminDao adminDao ;
    // adminDao injected by spring 

    @Transactional
    public void add(Admin admin) {
        adminDao.save(admin);

    }

    @Transactional
    public void edit(Admin admin) {
        adminDao.update(admin);

    }

    @Transactional
    public void delete(Admin admin) {
        adminDao.delete(admin);

    }

    @Transactional
    public Admin getAdmin(Integer adminId) {
        return adminDao.findById(adminId);
    }

    @Transactional
    public List<Admin> getAllAdmin() {
        return adminDao.findAll();
    }

    /**
     * @return the adminDao
     */
    public AdminDao getAdminDao() {
        return adminDao;
    }

    /**
     * @param adminDao the adminDao to set
     */
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @Override
    public Admin authenticate(String adminName, String adminPass) {

        return this.adminDao.authenticate(adminName, adminPass);
    }



}
4

2 回答 2

0

您正在尝试在构建 bean期间访问注入的依赖项。这显然行不通。你基本上期望它在幕后工作如下:

ScheduleController scheduleController; // Declare.
scheduleController.adminService = new AdminService(); // Inject.
scheduleController = new ScheduleController(); // Construct.

这是没有意义的。在构造实例之前无法设置实例变量。相反,它的工作原理如下:

ScheduleController scheduleController; // Declare.
scheduleController = new ScheduleController(); // Construct.
scheduleController.adminService = new AdminService(); // Inject.

如果您想构造注入之后直接执行操作,那么您应该使用带@PostConstruct注释的方法。

所以,更换

public ScheduleController() {
    List<Service> services = adminService.getAllService();

}

经过

@PostConstruct
public void init() { // Note: method name is fully free to your choice.
    List<Service> services = adminService.getAllService();

}

不需要<f:event>乱七八糟。也不一定要<f:metadata>顺便进去。

于 2013-07-09T10:59:41.820 回答
0

我找到了解决方案,问题不是注入问题,而是调用问题,我用这种方式工作,它有效:

在托管 bean 中,我添加了一个 init 方法:

public void init(){ 
 List<Service> = adminService.getAllService();
}

在 page.xhtml 中:

<f:metadata>
            <f:event type="preRenderView" listener="#{scheduleController.init()}"/>
</f:metadata>
于 2013-07-09T01:29:21.817 回答