问题描述
我有:
@Controller
@RequestMapping(value="admin/*", method=RequestMethod.GET)
public class AdminController {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
@RequestMapping
public ResponseEntity<String> test0() {
System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
return null;
}
}
和标签:
<mvc:annotation-driven />
它我觉得应该足够了,但@Autowired有问题:
It should be enough I feel, but there is a problem with @Autowired:
找不到类型为[javax.servlet.http.HttpServletResponse]的匹配bean :预计至少有1个符合条件的bean ...
我看过几个解决方案提到设置bean等等,但我相信它已经有了做一些更好的方法。注释扫描应该解决这个问题。如果我必须在xml中为不同时间的几个不同注释设置bean,那就太糟糕了。我只想在我使用它们时注释工作!
I have seen a couple of solutions mention setting up beans and such, but I am sure there has to be some better way. The annotation-scan should take care of this. It would suck if I have to set up beans in xml for several different annotations at different times. I just want the annotations to work when I use them!
我看到:
谢谢!
推荐答案
自动装配没有'为响应工作,只有请求。有解决方法,但它们有点hacky和lame。我遇到了同样的问题,这是我的原始问题以及解决方法的链接:
Autowiring doesn't work for the response, only the request. There are workarounds, but they're kind of hacky and lame. I ran into the same issue, here's my original question with a link to the workaround: @Autowired HttpServletResponse
这篇关于Spring MVC @AutoWired响应不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!