我是第一次尝试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());
}
}发布于 2013-01-12 01:51:04
这是错误的:
@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);另请参阅(有关其他解决方案)
https://stackoverflow.com/questions/14283750
复制相似问题