1

I have the following piece of code, which I modeled on this answer:

public class DeployerServlet extends HttpServlet {
    @Resource
    Engine engine;

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

    // ...
}

But the servlet is not even correctly instantiated. When creating an instance, Tomcat tries to look up the name com.example.DeployerServlet/engine in JNDI which results in an exception,

SEVERE: Allocate exception for servlet Deploy Servlet
javax.naming.NameNotFoundException: Name com.example.DeployerServlet is not bound in this Context

So, what is the recommended way to inject a Spring bean into a servlet?

Community
  • 1
  • 1
Saintali
  • 4,482
  • 2
  • 29
  • 49

2 Answers2

1

The @Resource annotation is a JavaEE element. It's used to declare a reference to a resource. Although Spring can use it the same way it does with @Inject and @Autowired, in this case the servlet Container acts first. Just replace your @Resource with @Autowired.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Note there is slight difference between `@Autowired` and `@Resource`: the later binds beans by name first, then falls back to binding by type; the former binds exclusively by type, using name (as supplied by `@Qualifier` annotation) as a hint only (http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s11.html). – Jerzyna Aug 19 '13 at 14:38
-1

Seems to be your build is not properly done. clean your project and rebuild. Your issue will solve

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • I use Maven and have been getting this error consistently across multiple builds. Also I do not see how this could be a build problem, as obviously there is no such JNDI name. – Saintali Aug 05 '13 at 14:47