How to use spring's autowire to inject beans into the servlet

Source: Internet
Author: User

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")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.