Beaver

一个合格的架构师,必须写的一手好代码为前提的基础上

Java Servlets 注入spring bean

Java Servlets 注入spring bean


方法一:HttpRequestHandler

此方法要实现HttpServlet,我们不能直接实现HttpServlet接口,而去实现HttpRequestHandler接口:
package com.xx.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;

import com.googlecode.jsonrpc4j.JsonRpcServer;

@Component("myServlet")
public class MyServlet implements HttpRequestHandler {

    @Autowired
    private TestManagerImpl testManagerImpl;

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        requestURI = requestURI.substring(requestURI.lastIndexOf("/") + 1);
        JsonRpcServer server = testManagerImpl.getServer(requestURI);
        server.handle(request, response);

    }

}

此类被spring托管,我们用@Component("myServlet")注解方法,属性
    @Autowired
    private TestManagerImpl testManagerImpl;
   是我们需要注入的对象。

我们还需要在web.xml中配置此伪servlet:
<servlet>
    <display-name>myServlet</display-name>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>
        org.springframework.web.context.support.HttpRequestHandlerServlet
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myurl/*</url-pattern>
</servlet-mapping>

注意:servlet-class的配置类是:
      <servlet-class>
        org.springframework.web.context.support.HttpRequestHandlerServlet
    </servlet-class>


方法二:利用org.springframework.beans.factory.annotation.Configurable 注解与  SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this) 实现


package com.xxx.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.googlecode.jsonrpc4j.JsonRpcServer;

@Configurable
public class TestServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @Autowired
    private TestManagerImpl   testManagerImpl;

    @Override
    public void init() throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        requestURI = requestURI.substring(requestURI.lastIndexOf("/") + 1);
        JsonRpcServer server = testManagerImpl.getServer(requestURI);
        server.handle(request, response);

    }
}

属性:
  @Autowired
    private TestManagerImpl   testManagerImpl;
必须是@Autowired注解,此属性是我们需要注入的对象。

 web.xml的配置如下:

  <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.xxx.controller.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet/*</url-pattern>
    </servlet-mapping>

标准配置Servlet方式。
        还需一下配置spring xml:

         <context:component-scan base-package="com.xxx"/> 
<context:annotation-config/>
<context:spring-configured/>


效果图:



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载[http://blog.csdn.net/doctor_who2004]。 https://blog.csdn.net/doctor_who2004/article/details/53996422
个人分类: Spring
所属专栏: spring 不一样的基础
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Java Servlets 注入spring bean

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭