0

I am trying to make a non-xml configuration for a simple Java application. Here are my files: This is my servlet:

package com.abcd.noxml;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

@WebServlet("/Ctrlr")
public class Ctrlr extends HttpServlet {
    @Autowired TeaService teaService;
    private static final long serialVersionUID = 1L;

    public Ctrlr() {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws  {
        String brand = request.getParameter("brand");
        teaService.teaServe(brand);

    }

}

and this is the jsp I have

<body>
<form action="Ctrlr" method="POST">
    <input type="text" name="brand" />
    <input type="submit" value="submit" />
</form>
</body>

this is my configuration file

package com.abcd.noxml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Autowired
    TeaService teaService;

    @Bean
    public TeaService teaService()
    {
        return new TeaServiceImpl();
    }
}

When I try to create a bean with @Autowire in my servlet, it is returning null. Please suggest how can this be solved. Redirect me to the existing links if this is a repeat question. Thank you

mkobit
  • 43,979
  • 12
  • 156
  • 150
goodbytes
  • 428
  • 3
  • 8
  • 21
  • 1
    Yuor servlet isn't a spring bean it is managed by the servlet container. Spring will only inject dependencies into beans it knows about. Instead of a servlet use a ` @Controller` as you normally would in a spring-mvc application. – M. Deinum Jan 08 '15 at 07:50
  • If you're using Spring, for heaven's sake don't manually write servlets. Use Web MVC and spend your time writing your application instead of another HTML rendering engine. I marked this as a duplicate, but the broader answer is *just don't do this*. – chrylis -cautiouslyoptimistic- Jan 08 '15 at 07:59
  • Most probably you forgot to include scanning of the required bean package (both of the injected bean and the bean that it's injected to). Read this answer I gave a while ago to understand better: http://stackoverflow.com/a/19419296/2083523. Also, you can view the completing question for `@Configuration` classes: http://stackoverflow.com/q/24014919/2083523 – Avi Jan 08 '15 at 08:33
  • 1- you should create your TeaServie interface , 2- you create the TeaServiceImpl class that implement TeaService and annotate your TeaServiceImpl class with the @Service annotation, that's all – Jileni Bouguima Jan 08 '15 at 08:48
  • Of course... !! Should have made a controller instead of servlet. Thanks guys – goodbytes Jan 12 '15 at 10:31
  • Of course I should have had a controller instead of a simple servlet. Thanks guys :) – goodbytes Jan 12 '15 at 10:37

0 Answers0