3

私はSpringを初めて使用し、spring iocを小さな(テスト)Webアプリに接続したいと考えています。

私はそのようなサーブレットを持っています ProductServlet

public class ProductServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    private RequestHelper requestHelper;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request);
    }

    private void processRequest(HttpServletRequest request){
        requestHelper.process(request);
    }

    public RequestHelper getRequestHelper() {
        return requestHelper;
    }

    public void setRequestHelper(RequestHelper requestHelper) {
        this.requestHelper = requestHelper;
    }

}

と私のweb.xml:

  <servlet>
    <servlet-name>ProductServlet</servlet-name>
    <servlet-class>com.epam.productshop.controller.ProductShop</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProductServlet</servlet-name>
    <url-pattern>/ProductServlet</url-pattern>
  </servlet-mapping>

     <listener>
      <listener-class>
        org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-config.xml
        </param-value>
    </context-param>

また、私はそのような春の構成xmlを持っています:

<bean id="factory" class="com.epam.productshop.readerfactory.ReaderFactory">
    <property name="file" value="/xml/products.xml" />
</bean>

<bean id="requestHelper" class="com.epam.productshop.requesthelper.RequestHelper" scope="singleton">
    <property name="factory" ref="factory" />
</bean>

<bean name="ProductServlet" class="com.epam.productshop.controller.ProductServlet" scope="singleton">
    <property name="requestHelper" ref="requestHelper"/>        
</bean>

そして私はそのような問題を抱えています:

サーブレットのinit()中に、スプリングセットrequestHelperオブジェクトをサーブレットに組み込みたい。しかし、これの代わりに、それは私にnullpointerを与えます。

サーブレットを実装しようとしています。init()メソッドなど、インターネットで見られるものHttpRequestHandlerに書き込んSpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());でいますが、これらすべてで問題が解決するわけではありません。

私を助けてください

4

3 に答える 3

6

あなたの質問ではあなたは

<bean name="ProductServlet" class="com.epam.productshop.controller.ProductServlet" scope="singleton">
    <property name="requestHelper" ref="requestHelper"/>        
</bean>

Springコンテナを使用してサーブレットをインスタンス化することはできません。サーブレットコンテナによってインスタンス化されます。ProductServletの別のインスタンスを宣言しているだけです。

したがって、サーブレットinit()メソッドが呼び出されたら、呼び出す必要があります

SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());`

requestHelperを挿入する@Autowiredには、サーブレットで注釈付きのフィールドまたはプロパティを宣言します。

private RequestHelper requestHelper;

@Autowired
public void setRequestHelper(RequestHelper requestHelper){
  this.requestHelper = requestHelper;
}

processInjectionBasedOnServletContext javadocから:

ServletContextに格納されている現在のルートWebアプリケーションコンテキストに基づいて、指定されたターゲットオブジェクトの@Autowiredインジェクションを処理します。

于 2012-11-19T20:54:50.947 に答える
2

これがあなたを助けるかもしれない解決策です:

public class ProductServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private RequestHelper requestHelper = null;

    private requestHelperInit(HttpServletRequest request)
    {
       if(requestHelper == null)
       {
          ApplicationContext ap = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
          requestHelper = ap.getBean(RequestHelper.class);
       }
    }
}

次に、最初のステートメントとして、またはメソッドrequestHelperInit(request)のいずれかでメソッドを呼び出します。doGet()doPost()

あなたがまだ解決策を探しているなら、私はこれがあなたを助けることを願っています。

于 2013-10-13T12:28:25.097 に答える
1

いくつかの選択肢があります。本当にサーブレットを注入したい場合、問題は、サーブレットコンテナがすでにサーブレットの作成を担当していることです。インジェクションは作成時に発生し、このサーブレットの作成を取得するのは少し難しいため、あまり洗練されていないソリューションを使用する必要があります。ただし、この質問では、サーブレットを挿入する方法について説明します。

一方、このアプローチを中止することを検討するかもしれません。実際にWebアプリケーションを作成している場合、サーブレットAPIに直接コーディングすることはかなりまれです。サーブレットAPIの上に配置され、より高いレベルの機能、別名ベルとホイッスルを提供する多くのWebアプリケーションフレームワークの1つを選択してみませんか。ベルとホイッスルの1つは、これらのフレームワークがSpringとの便利な統合を提供するため、コードを簡単に挿入できることです。たとえば、Struts 2にはSpringプラグインがあり、Springを使用して、フレームワークのインフラストラクチャコンポーネントを含む、フレームワークによって作成されたすべてのオブジェクトを注入できます。

于 2012-11-19T20:57:39.100 に答える