Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiationException using Spring injection
    text
    copied!<p>I am experimenting with Spring injection for the first time. I am surely forgetting something obvious but I don't know what it is.</p> <p>Under src/main/java, I have a package 'example' containing Hello, Animal, Cat.</p> <p>Under src/main/webapp/WEB-INF, I have web.xml and springapp-servlet.xml.</p> <p>When I deploy my app with Tomcat, I get a: </p> <pre><code>javax.servlet.ServletException: Error instantiating servlet class example.Hello org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) </code></pre> <p>What am I missing for the injection to work?</p> <p>Source below:</p> <p>Hello.java</p> <pre><code>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()); } } </code></pre> <p>Cat.java </p> <pre><code>package example; import org.springframework.stereotype.Service; @Service public class Cat implements Animal { public String sound() { return "Miaou"; } } </code></pre> <p>Animal.java </p> <pre><code>package example; public interface Animal { public String sound() ; } </code></pre> <p>web.xml </p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;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"&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/springapp-servlet.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;Hello&lt;/servlet-name&gt; &lt;servlet-class&gt;example.Hello&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Hello&lt;/servlet-name&gt; &lt;url-pattern&gt;/hello&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>springapp-servlet.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;context:component-scan base-package="example" /&gt; &lt;mvc:annotation-driven /&gt; &lt;/beans&gt; </code></pre> <p>I initially thought that perhaps my springapp-servlet.xml was not even read, but if I make a typo on the name springapp-servlet.xml in my web.xml, I do get an error at deployment time, so I clearly have the correct path for springapp-servlet.xml. It is being but yet the injection isn't working. </p> <p>UPDATE:</p> <p>I am showing below the solution that worked for me thanks to the answers below. All code remains the same except for Hello:</p> <p>Hello.java</p> <pre><code>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()); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload