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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:11:20  来源:igfitidea点击:

Where to put business logic in spring mvc framework?

javaspring-mvcbusiness-logic

提问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

@Controllerclasses serve as C from MVC. Note that the real controller in Spring MVC is DispatcherServletthat will use the specific @Controllerclass to handle the URL request.

@Controller类充当MVC 中的 C。请注意,Spring MVC 中真正的控制器DispatcherServlet将使用特定的@Controller类来处理 URL 请求。

@Serviceclasses should serve for your service layer. Here you should put your business logic.

@Service类应该为您的服务层服务。您应该在这里放置您的业务逻辑

@Repositoryclasses should serve for your data access layer. Here you should put CRUD logic: insert, update, delete, select.

@Repository类应该为您的数据访问层服务。您应该在这里放置 CRUD 逻辑:插入、更新、删除、选择。

@Service, @Repositoryand 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

@Controllerclasses should only have access to @Serviceclasses through interfaces. Similar, @Serviceclasses should only have access to other @Serviceclasses and for a specific set of @Repositoryclasses 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.

恢复,想法是将业务逻辑移至模型层并简化您的服务方法。