【问题标题】:InstantiationException using Spring injection使用 Spring 注入的 InstantiationException
【发布时间】:2013-01-11 17:43:43
【问题描述】:

我是第一次尝试使用 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());
  }
}

猫.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());
  }    
}

【问题讨论】:

  • 你确定这是日志中唯一的例外吗?你检查过堆栈跟踪吗?
  • 是的,堆栈跟踪没有添加更多信息,这就是我没有包含的原因。没有其他例外。

标签: java spring tomcat code-injection


【解决方案1】:

这是错误的:

@Service
public class Hello extends HttpServlet {

Servlet 的生命周期由 servlet 容器控制,而不是由 Spring。因此,您不能将 Spring bean 直接自动装配到 servlet。 Spring 根本不应该创建 servlet。基本上,Spring 对您的 servlet 一无所知,它会尝试实例化它,但它与由 servlet 容器创建并用于处理请求的实例不同。

最后,您的 servlet 没有无参数构造函数。这样的构造函数是必需的,但它不会使您的示例通过。

解决方案是直接从注册的 Web 应用程序上下文中获取所需的 Spring bean:

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

另见(其他解决方案)

【讨论】:

  • 啊,好点。值得指出的是,如果您确实想将 spring bean 直接连接到您的 servlet,您应该使用 Spring MVC 并将它们连接到您的控制器。
  • 谢谢,这行得通:protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); final Animal 动物 = (Animal)context.getBean("cat"); resp.getWriter().write(animal.sound());这是正确的做事方式吗?每次注入都必须调用 Web 应用程序上下文有点麻烦,所以我想知道我是否在这里没有正确使用该语言。
  • 还有另一种方式使用spring HttpRequestHandlerServlet。查看以下 url 以了解它是如何设置的。 insidecoding.wordpress.com/2011/09/08/…
  • @Tomasz:你提到的帖子对我来说效果很好。我喜欢 Oliver 使用 SpringBeanAutowiringSupport 提供的解决方案。
猜你喜欢
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
相关资源
最近更新 更多