JSP development servlet calls dao, servletspring injected with spring Management
JSPs call and inject the dao managed by spring in servlet
We can use spring dependency injection to inject dao into the action, and then we can directly call the method in dao, but the servlet is not managed by the spring container, therefore, the dao class cannot be injected into the servlet, And the dao method cannot be used.
The implementation method is as follows:
Private UserDao userDao; public void init () throws ServletException {super. init (); ServletContext servletContext = this. getServletContext (); WebApplicationContext ctx = WebApplicationContextUtils. getWebApplicationContext (servletContext); userDao = (UserDao) ctx. getBean ("userDao ");}
Add the private variable UserDao to the servlet and initialize it in the init () method of the servlet.
Public UserDao getUserDao () {return userDao;} public void setUserDao (UserDao userDao) {this. userDao = userDao ;}
Add the get set Method (this is not tested)
You can call dao in servlet at will later, yeah!
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!