0

初めてSpringインジェクションを試しています。私は確かに明らかなことを忘れていますが、それが何であるかはわかりません。

src/main/java の下に、Hello、Animal、Cat を含む「example」パッケージがあります。

src/main/webapp/WEB-INF の下に、web.xml と springapp-servlet.xml があります。

Tomcat でアプリをデプロイすると、次のようになります。

javax.servlet.ServletException: Error instantiating servlet class example.Hello
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)

注射が機能するために欠けているものは何ですか?

以下のソース:

Hello.java

package example;

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.stereotype.Service;

@Service
public class Hello extends HttpServlet {
  private final Animal animal;

  @Autowired
  public Hello(final  Animal animal) {
    this.animal = animal;
  }

  @Override
  protected void doGet(final HttpServletRequest req,
          final HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write(animal.sound());
  }
}

Cat.java

package example;

import org.springframework.stereotype.Service;

@Service
public class Cat implements Animal {
  public String sound() {
    return "Miaou";
  }
}

Animal.java

package example;

public interface Animal {
  public String sound() ;
}

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springapp-servlet.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>example.Hello</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

springapp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="example" />
    <mvc:annotation-driven />

</beans>

おそらく私の springapp-servlet.xml が読み取られていないのではないかと最初は思っていましたが、web.xml で springapp-servlet.xml の名前をタイプミスすると、デプロイ時にエラーが発生するので、明らかにspringapp-servlet.xml の正しいパス。それはされていますが、まだ注射は機能していません。

アップデート:

以下の回答のおかげで、私のために働いた解決策を以下に示しています。Hello を除いて、すべてのコードは同じままです。

Hello.java

public class Hello extends HttpServlet {

  @Inject
  private Animal animal;

  @Override
  public void init(final ServletConfig config) throws ServletException {
    super.init(config);
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
            config.getServletContext());
  }

  @Override
  protected void doGet(final HttpServletRequest req,
          final HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write(animal.sound());
  }    
}
4

1 に答える 1

2

これは間違っています:

@Service
public class Hello extends HttpServlet {

サーブレットのライフサイクルは、Spring ではなく、サーブレット コンテナーによって制御されます。したがって、Spring Bean をサーブレットに直接オートワイヤーすることはできません。Spring はサーブレットをまったく作成すべきではありません。基本的に、Spring はサーブレットについて何も認識せず、インスタンス化を試みますが、サーブレット コンテナーによって作成され、リクエストの処理に使用されるインスタンスとは異なります。

最後に、サーブレットには引数なしのコンストラクターがありません。このようなコンストラクターは必須ですが、例を渡すことはできません。

解決策は、登録された Web アプリケーション コンテキストから目的の Spring Bean を直接フェッチすることです。

WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
Animal animal = context.getBean(Animal.class);

こちらもご覧ください(その他のソリューションについて)

于 2013-01-11T17:51:03.500 に答える