从java类调用控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20374972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoke controller method from java class
提问by Swapnil Walivkar
I just want to know whether controller class method is accessible from another java class.
following is my controller and its method.
以下是我的控制器及其方法。
@Controller
public class TestResultUploadController {
@RequestMapping(method = RequestMethod.POST,value="/uploadTestResult")
public @ResponseBody
String uploadTestResult(String testResultBarcode,int deviceLoc) {
//some code goes here
return something;
}
I just want to call this controller method from another java class. How can I make it work? please suggest..
我只想从另一个 java 类调用这个控制器方法。我怎样才能让它工作?请建议..
回答by Nikola Yovchev
Short answer: yes, it is possible. In your other class/thread, you can do
简短回答:是的,这是可能的。在你的其他班级/线程中,你可以做
// this will create a new instance of that controller where no fields are wired/injected
TestResultUploadController controller = new TestResultUploadController();
controller.uploadTestResult("someString", 1234);
However, keep in mind that your setup is highly unusual and all your autowired fields wouldn't be wired correctly. If you obtain your controller from the context, you'd be able to have your fields wired/injected properly:
但是,请记住,您的设置非常不寻常,所有自动装配的字段都不会正确连接。如果您从上下文中获取您的控制器,您将能够正确连接/注入您的字段:
// obtain controller bean from context, should have fields wired properly
TestResultUploadController controller = ctx.getBean(TestResultUploadController.class);
controller.uploadTestResult("someString", 1234);
or you can, in your other class, have:
或者你可以在你的其他班级中:
@Autowired private TestResultUploadController controller;
....
public void doStuff(){
controller.uploadTestResult("someString", 1234);
}
Again, this is highly unusual, but highly possible. However, just cause something is possible to be done, doesn't mean you should do it. I would recommend the more common Spring/MVC approach in which you outsource the business logic to Services. Basically, to have something like this:
同样,这是非常不寻常的,但很有可能。然而,仅仅因为可以做某事,并不意味着你应该去做。我会推荐更常见的 Spring/MVC 方法,将业务逻辑外包给服务。基本上,有这样的事情:
@Controller
public class TestResultUploadController {
@Autowired private UploadTestResultService uploadTestResultService;
@RequestMapping(method = RequestMethod.POST,value="/uploadTestResult")
public @ResponseBody String uploadTestResult(String testResultBarcode,int deviceLoc) {
return uploadTestResultService.uploadTestResult(testResultBarcode, deviceLoc);
}
}
And in your thread:
在你的线程中:
//somehow get the service
UploadTestResultService uploadTestResultService = //somehowGetTheService (whether from context or in some other way)
uploadTestResultService.uploadTestResult(testResultBarcode, deviceLoc);
That way, you'd be able to mock the UploadTestResultService in your tests of the controller, and you'd also be able to test the uploadTestResult method of that service on its own without it being in a controller.
这样,您就可以在控制器的测试中模拟 UploadTestResultService,并且您还可以单独测试该服务的 uploadTestResult 方法,而无需将其置于控制器中。
EDIT:How to obtain the Spring context is outside of the scope of this question. I assume you know basic Spring and also basic java.
编辑:如何获取 Spring 上下文超出了这个问题的范围。我假设您了解基本的 Spring 和基本的 Java。