Java spring mvc框架中业务逻辑放在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25355385/
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
Where to put business logic in spring mvc framework?
提问by Mark Vincent Osea
I don't know where to put the business logic in spring mvc because I'm new to it. I have a clue on what to do but because of lack in knowledge in spring mvc, I don't know where to start. I would also like to ask if somebody knows where I can get a good tutorial on this or a complete sample of a spring mvc web application that has a business logic on it? Anyways, the business logic that I was talking about is all about database handling :)
我不知道在 spring mvc 中将业务逻辑放在哪里,因为我是新手。我知道该怎么做,但由于缺乏 spring mvc 知识,我不知道从哪里开始。我还想问一下,是否有人知道我可以在哪里获得关于此的好的教程或具有业务逻辑的 spring mvc Web 应用程序的完整示例?无论如何,我所说的业务逻辑都是关于数据库处理的:)
采纳答案by Luiggi Mendoza
@Controller
classes serve as C from MVC. Note that the real controller in Spring MVC is DispatcherServlet
that will use the specific @Controller
class to handle the URL request.
@Controller
类充当MVC 中的 C。请注意,Spring MVC 中真正的控制器DispatcherServlet
将使用特定的@Controller
类来处理 URL 请求。
@Service
classes should serve for your service layer. Here you should put your business logic.
@Service
类应该为您的服务层服务。您应该在这里放置您的业务逻辑。
@Repository
classes should serve for your data access layer. Here you should put CRUD logic: insert, update, delete, select.
@Repository
类应该为您的数据访问层服务。您应该在这里放置 CRUD 逻辑:插入、更新、删除、选择。
@Service
, @Repository
and your entity classes will be M from MVC. JSP and other view technologies(e.g. JSP, Thymeleaf etc.) will conform V from MVC.
@Service
,@Repository
并且您的实体类将是来自 MVC 的 M。JSP等视图技术(例如JSP,Thymeleaf等)将符合从MVC V。
@Controller
classes should only have access to @Service
classes through interfaces. Similar, @Service
classes should only have access to other @Service
classes and for a specific set of @Repository
classes through interfaces.
@Controller
类应该只能@Service
通过接口访问类。类似地,@Service
类应该只能通过接口访问其他@Service
类和一组特定的@Repository
类。
回答by Neil McGuigan
Generally, your business logic goes in the service layer. Though you can put basic validation rules in your pojos with JSR annotations.
通常,您的业务逻辑位于服务层。尽管您可以使用 JSR 注释在 pojo 中放置基本的验证规则。
For a Spring MVC app you have controllers, which handle http requests, and a domain layer, which are pojos representing your business models. You often have a persistence layer, or DAO. You might have a service layer as well, for helping with non-trivial logic.
对于 Spring MVC 应用程序,您有处理 http 请求的控制器和一个域层,它们是代表您的业务模型的 pojo。您通常有一个持久层或 DAO。您可能还有一个服务层,用于帮助处理重要的逻辑。
Your comment about database handling doesn't make sense. Business rules are orthogonal to storing data. Your database handling should go in your persistence layer.
您对数据库处理的评论没有意义。业务规则与存储数据正交。您的数据库处理应该放在持久层中。
回答by Gigi
Many people would recommend to add the business logic to the service layer. I personally find out that's not a great idea, specially when you start testing: you may have to deal either with the persistence and business logic at the same time, or mocking everything around, and then things can get very messy.
许多人会建议将业务逻辑添加到服务层。我个人发现这不是一个好主意,特别是当您开始测试时:您可能必须同时处理持久性和业务逻辑,或者模拟周围的一切,然后事情会变得非常混乱。
I do recommend reading this article before taking any conclusions: The Biggest Flaw of Spring Web Applications
我建议在得出任何结论之前阅读这篇文章: Spring Web 应用程序的最大缺陷
Resuming, the idea would be to move the business logic to the model layer and simplify your services methods.
恢复,想法是将业务逻辑移至模型层并简化您的服务方法。