1

At present in my spring mvc based application i am implementing Servlets as well (this is a healthcheck servlet). I am trying to inject dependency of a class in this servlet using @Autowire annotation but this class is not instantiating.

Please consider the below code.

@WebServlet(name = "myServlet", urlPatterns = "/app2")
public class HealthCheckController extends HealthCheckServlet{

/** The Constant serialVersionUID. */

    private static final long serialVersionUID = 1L;

    @Autowire
      private MyService service;

    /**
     * method to check healthiness.
     *
     * @return true, if is healthy
     * @throws HealthCheckException the health check exception
     */
    @Override
    public boolean isHealthy() throws HealthCheckException {
        try {
            service.showDetails("12", false,null);
            System.out.println("True");
            return true;
        } catch (Exception  e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return false;
    }

Could you please suggest how can i provide dependency of MyService class?

Thanks in advance.

Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
MiniSu
  • 566
  • 1
  • 6
  • 22
  • See https://stackoverflow.com/questions/22291894/failing-to-autowire-a-member-in-a-webservlet – Cisco May 30 '18 at 14:10
  • Where did you provide myServlet? In dispachter-servlet or in application-context? If it is in disptcher-servlet, you have to add @Component to the webservlet and add with the package of webservlet in disptcher servlet – Shubham Kadlag May 30 '18 at 14:17
  • myServlet is the name itself – MiniSu May 30 '18 at 14:33
  • you should provide the code for MyService class also and the beans configuration file if you are using XML. – Bằng Rikimaru May 30 '18 at 14:49

2 Answers2

0

This is not a Spring managed bean. Try adding @Component to the class declaration.

@Component
@WebServlet(name = "myServlet", urlPatterns = "/app2")
public class HealthCheckController extends HealthCheckServlet{

You can also try to refactor your class to be a Spring Controller:

@Controller
@RequestMapping("app2")
public class HealthCheckController extends HealthCheckServlet{

Another thing you can try is adding the servlet in the web.xml instead of using the @WebServlet annotation.

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>mypackage.HealthCheckController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/app2</url-pattern>
</servlet-mapping>
Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
0

Add '@Controller' as mentioned in the comments and update your URL mapping. Also, what is calling isHealthy()? Try adding your url mapping there.

@Controller
public class HealthCheckController extends HealthCheckServlet{

private static final long serialVersionUID = 1L;

@Autowire
  private MyService service;

@RequestMapping("/app2/*")
public boolean isHealthy() throws HealthCheckException {
    try {
        service.showDetails("12", false,null);
        System.out.println("True");
        return true;
    } catch (Exception  e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Adam
  • 490
  • 7
  • 21
  • HI Adam,this works fine but i am getting 406 unsupported media type.I have used MediaType.APPLICATION_JSON on isHealthy().but no success. – MiniSu Jun 05 '18 at 09:36
  • Your question was about dependency injection. If there is another issue you may need to research that and possibly post another question if you cannot find a solution to that new problem. – Adam Jun 05 '18 at 14:59