2013-07-08 87 views
0

時候我已經注射了AdminService的顯示java.lang.NullPointerException出現了,雖然我在另一個託管bean注入這個bean與以同樣的方式和一切工作:春天注射顯示java.lang.NullPointerException

@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; 
    } 

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); 
    } 



} 
+0

你可以發佈bean配置嗎? – paary

+0

我發佈了AdminService實現 –

+0

我建議你讓服務有一個訪問它的應用程序bean(你可以直接通過代碼按照使用註釋的順序加載它),然後在你的視圖的每個bean中注入這個bean,以便能夠隨時隨地訪問服務。 –

回答

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批註的方法操作。

因此,通過

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

} 

更換

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

} 

無需爲<f:event>混亂。順便說一下,它也不一定需要進入<f:metadata>

0

我找到解決方案,這個問題是不是注射的問題是通話的問題,我這種方式工作,它的工作原理:

在託管bean我添加了一個init方法:

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

,並在page.xhtml:

<f:metadata> 
      <f:event type="preRenderView" listener="#{scheduleController.init()}"/> 
</f:metadata> 
相關問題