I have a problem with an autowire of a component.
My implementation consists in a Controller, an interface used by the Controller, and a Component that implements thagt interface. I want to autowire another component in the implementation.
This is the controller:
@Controller
public class MyController {
@RequestMapping(path = "/myPath/{subpath}", method = RequestMethod.GET)
public void myMethod(@PathVariable("subpath") String subpath, HttpServletResponse response){
try{
MyHandler handler = new MyHandlerImpl(response.getOutputStream());
handler.handle();
} catch (Exception e) {
}
}
}
This is the interface:
public interface MyHandler {
public void handle();
}
And this is the Implementation:
// tried all: @Component, @Service, @Repository, @Configurable
public class MyHandlerImpl implements MyHandler {
@Autowired
MyComponentToAutowired myComponentToAutowired; // <= this is NULL
public MyHandlerImpl (ServletOutputStream output) {
this.output = output;
}
private OutputStream output;
public void handle() {
myComponentToAutowired.theMethod(); // <- NullPointerException at this point
// ...
}
/*
If I don't create a default constructor, Spring crash at the start because it not finds the default constructor with no-args.
*/
}
What can I do to autowire the component properly?
Thanks.