General Javapojo in applications are managed by spring, so injecting with autowire annotations does not create problems, but there are two exceptions, one is Filter, the other is a servlet, These two things are managed by the servlet container, so if you want to inject it with autowire like any other bean, you need to do some extra work.
Delegatingfilterproxy is provided for filter,spring, so this article focuses on the solution of the servlet.
1. A more intuitive but less elegant approach is to rewrite the init () method, where you can use Autowirecapablebeanfactory to manually tell spring: This servlet needs a bean of this kind. Specific wording:
public void init(ServletConfig servletConfig) throws ServletException {
ServletContext servletContext = servletConfig.getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.configureBean(this, BEAN_NAME);
}
Among them, Bean_name is the name that needs to be injected into the BEAN that is registered in spring.
The main problem with this writing is the Bean_name, which is a bit of an active lookup rather than a dependency on the injected sensation.
2, create a similar delegatingfilterproxy agent, through the agent based on configuration to find the actual servlet, the completion of business logic functions.
Suppose we have a servlet named Userservlet, we need to inject a usermanager, the pseudocode is as follows:
public class UserServlet extends HttpServlet {
@Autowired(required = true)
private UserManager userManager;
}
First step:
public class DelegatingServletProxy extends GenericServlet {
private String targetBean;
private Servlet proxy;
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
proxy.service(req, res);
}
@Override
public void init() throws ServletException {
this.targetBean = getServletName();
getServletBean();
proxy.init(getServletConfig());
}
private void getServletBean() {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.proxy = (Servlet) wac.getBean(targetBean);
}
}
Step Two:
Configure the Web.xml file, the original Userservlet configuration is roughly like this:
<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>com.sample.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
Now amend it to
<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>com.sample.DelegatingServletProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
Note that spring is looking for the proxy servlet based on the name of the servlet, so first we have to add @Component to the Userservlet class to tell Srping that I am also a bean. If the name is not the same as the one defined in Web.xml, you can specify the name of the bean here, such as: @Component ("Userservlet")