An example Analysis of Spring MVC complying with the principle of single responsibility
This article shares with you the content of a sample analysis of Spring MVC's adherence to the principle of single responsibility. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Abide by the principle of single responsibility
When designing and writing controllers in Spring MVC, you should follow the following two very practical operations:
Instead of using the controller class to execute the business logic, you should use the controller class to delegate the business processing to the relevant business class. This ensures that the controller focuses on its assigned responsibility, that is, to control the workflow of the application. Such as:
@ Controller publicclassUserController {@ Autowired private UserDAO userDAO; publicString listUser () {/ / handler method to list all users userDAO.list ();} publicString saveUser (User user) {/ / handler method to save/update a user userDAO.save (user);} publicString deleteUser (User user) {/ / handler method to delete a user userDAO.delete (user) } publicString getUser (int userId) {/ / handler method to get a user userDAO.get (userId);}}
Create a separate controller for each business area. For example, use UserController to control user-managed workflows, OrderController to control order processing workflows, and so on:
@ Controller publicclassUserController {} @ Controller publicclassProductController {} @ Controller publicclassOrderController {} @ Controller publicclassPaymentController {} Thank you for reading! This is the end of the article on "sample Analysis of Spring MVC complying with the principle of single responsibility". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!