我有以下一段代码,我以这个答案为模型:
public class DeployerServlet extends HttpServlet {
@Resource
Engine engine;
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
}
// ...
}
但是 servlet 甚至没有正确实例化。创建实例时,Tomcat 会尝试com.example.DeployerServlet/engine
在 JNDI 中查找名称,这会导致异常,
SEVERE: Allocate exception for servlet Deploy Servlet
javax.naming.NameNotFoundException: Name com.example.DeployerServlet is not bound in this Context
那么,将 Spring bean 注入 servlet 的推荐方法是什么?