我的 Controller 中有两种方法:
- 公共(public)方法
- 私有(private)方法
它们都有@requestMapping
并且它们都引用一个全局变量(@autowrite
)。问题是第一个方法变量有值,第二个方法变量为空。
请帮助我。
/**
* Both of these methods are accessible through the browser,
* when i ask for query1 the flowService has value but
* when i ask for query2 the flowService is null.
* My spring version is 4.2.4
* Created by hanxiaofei on 2017/10/12.
*/
public class TestController {
@Autowired
private FlowService flowService;
@RequestMapping(value = {"/query1"})
@ResponseBody
public CommonListResult<WorkOrderMO> query1() {
return flowService.queryWorkOrderList(1);
}
@RequestMapping(value = {"/query2"})
@ResponseBody
private CommonListResult<WorkOrderMO> query2() {
return flowService.queryWorkOrderList(1);
}
}
最佳答案
用@RequestMappings
注释的 Controller 方法必须是公共(public)的才能正常工作。无论如何,没有理由将请求方法设置为私有(private),因为无论如何您都不应该自己从不同的组件调用 Controller 方法。
关于java - springmvc 的私有(private)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46684838/