I am having the below managed bean. But every time I do a post back to the same bean ie while calling updateFileList. I get a new instance of FileDAO.
How can I prevent this? Is it safe to have a DAO inside a managed bean, if not what changes can I make to better it.
@ManagedBean(name = "file") @ViewScoped public class FileController implements Serializable { private static final long serialVersionUID = 1L; private List<LoadFileLog> fileList = null; private Date selectedDate; FileDAO fileDAO; public FileController() { System.out.println(" In file Controller constructor"); ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); fileDAO = (FileDAO) context.getBean("FileDAO"); } public FileDAO getFileDAO() { return fileDAO; } public void setFileDAO(FileDAO fileDAO) { this.fileDAO = fileDAO; } public List<LoadFileLog> getFileList() { return fileList; } public Date getSelectedDate() { return selectedDate; } public void setSelectedDate(Date selectedDate) { this.selectedDate = selectedDate; } public void updateFileList() { SystemController systemControl = (SystemController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("system"); List systemList = new ArrayList(); if (systemControl != null) { systemControl.populateSelectedSystems(); systemList = systemControl.getSelectedSysIdList(); } if (selectedDate != null) { fileList = getFileDAO().getFiles(systemList, selectedDate); } } }
Thanks!