I think that in principle, it should be a "single language / single framework" when building a web application, but in reality there are cases where that is not allowed. In particular, I hear that if you are involved in the development of business systems, you may be forced to develop something like patchwork (bloody tears).
As an extension of that, there was a case where a class created by Spring was used in Servlet, that is, @Autowired
had to be done in __Servlet. I thought it would be easy, but I was quite addicted to it, so I will show you a sample as a memo.
@WebServlet("/hoge")
public class HogeServlet extends HttpServlet {
@Autowired private HogeService hogeService;
@Override
protected void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
hogeService.doSomething();
}
}
The principle of Spring is to "leave the creation and disappearance of various beans to Spring", and @Autowired
can be said to be a typical example. Conversely, in order to use @Autowried
in a Servlet outside __Spring, it is necessary to incorporate that Servlet into the Spring life cycle, specifically the overridden ʻinit` method. The setting is done in. __
Recommended Posts