您对第一个 Java EE Spring 项目的“最佳实践”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8569/
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-10-29 10:44:16  来源:igfitidea点击:

What's your "best practice" for the first Java EE Spring project?

javaspringjakarta-eeaop

提问by cringe

I'm currently trying to get into the Java EE development with the Spring framework. As I'm new to Spring, it is hard to imaging how a good running project should start off.

我目前正在尝试使用 Spring 框架进行 Java EE 开发。因为我是 Spring 的新手,所以很难想象一个好的运行项目应该如何开始。

Do you have any best practices, tipps or major DO NOTsfor a starter? How did you start with Spring - big project or small tutorial-like applications? Which technology did you use right away: AOP, complex Hibernate...

你有任何的最佳实践,窍门或重大DO组合这些的人吗?您是如何开始使用 Spring - 大项目或类似教程的小应用程序的?你马上使用了哪种技术:AOP、复杂的Hibernate……

采纳答案by Brian Laframboise

Small tip - I've found it helpful to modularize and clearly label my Spring xml context files based on application concern. Here's an example for a web app I worked on:

小提示 - 我发现基于应用程序关注的模块化和清楚地标记我的 Spring xml 上下文文件很有帮助。这是我使用的网络应用程序的示例:

  • MyProject / src / main / resources / spring /
    • datasource.xml- My single data source bean.
    • persistence.xml- My DAOs/Repositories. Depends on datasource.xmlbeans.
    • services.xml- Service layer implementations. These are usually the beans to which I apply transactionality using AOP. Depends on persistence.xmlbeans.
    • controllers.xml- My Spring MVC controllers. Depends on services.xmlbeans.
    • views.xml- My view implementations.
  • MyProject / src / main / resources / spring /
    • datasource.xml- 我的单一数据源 bean。
    • persistence.xml- 我的 DAO/存储库。取决于datasource.xml豆子。
    • services.xml- 服务层实现。这些通常是我使用 AOP 应用事务性的 bean。取决于persistence.xml豆子。
    • controller.xml- 我的 Spring MVC 控制器。取决于services.xml豆子。
    • views.xml- 我的视图实现。

This list is neither perfect nor exhaustive, but I hope it illustrates the point. Choose whatever naming strategy and granularity works best for you.

这份清单既不完美也不详尽,但我希望它说明了这一点。选择最适合您的命名策略和粒度。

In my (limited) experience, I've seen this approach yeild the following benefits:

在我(有限的)经验中,我已经看到这种方法产生以下好处:

Clearer architecture

更清晰的架构

Clearly named context files gives those unfamiliar with your project structure a reasonable place to start looking for bean definitions. Can make detecting circular/unwanted dependencies a little easier.

明确命名的上下文文件为那些不熟悉您的项目结构的人提供了一个开始寻找 bean 定义的合理位置。可以更轻松地检测循环/不需要的依赖项。

Helps domain design

帮助域设计

If you want to add a bean definition, but it doesn't fit well in any of your context files, perhaps there's a new concept or concern emerging? Examples:

如果你想添加一个 bean 定义,但它不适合你的任何上下文文件,也许有一个新的概念或问题出现?例子:

  • Suppose you want to make your Service layer transactional with AOP. Do you add those bean definitions to services.xml, or put them in their own transactionPolicy.xml? Talk it over with your team. Should your transaction policy be pluggable?
  • Add Acegi/Spring Security beans to your controllers.xmlfile, or create a security.xmlcontext file? Do you have different security requirements for different deployments/environments?
  • 假设您希望使用 AOP 使您的服务层具有事务性。您是将这些 bean 定义添加到services.xml还是将它们放在自己的定义中transactionPolicy.xml?和你的团队讨论一下。你的事务策略应该是可插入的吗?
  • 将 Acegi/Spring Security bean 添加到您的controllers.xml文件中,还是创建一个security.xml上下文文件?您对不同的部署/环境有不同的安全要求吗?

Integration testing

集成测试

You can wire up a subset of your application for integration testing (ex: given the above files, to test the database you need to create only datasource.xmland persistence.xmlbeans).

您可以连接应用程序的一个子集进行集成测试(例如:给定上述文件,以测试您只需要创建的数据库datasource.xmlpersistence.xmlbean)。

Specifically, you can annotate an integration test class as such:

具体来说,您可以这样注释集成测试类:

@ContextConfiguration(locations = { "/spring/datasource.xml" , "/spring/persistence.xml" })

Works well with Spring IDE's Beans Graph

适用于 Spring IDE 的 Beans Graph

Having lots of focused and well-named context files makes it easy to create custom BeansConfigSets to visualize the layers of your app using Spring IDE's Beans Graph. I've used this before to give new team members a high-level overview of our application's organization.

拥有大量重点突出且命名良好的上下文文件,可以轻松创建自定义 BeansConfigSets 以使用 Spring IDE 的Beans Graph可视化应用程序的层。我以前使用过它来向新团队成员提供我们应用程序组织的高级概述。

回答by GaryF

Focus first on the heart of Spring: Dependency Injection. Once you see all the ways that DI can be used, then start thinking about the more interesting pieces like AOP, Remoting, JDBC Templates etc. So my best bit of advice is let your use of Spring grow out from the core.

首先关注 Spring 的核心:依赖注入。一旦你看到了 DI 的所有使用方式,然后开始考虑更有趣的部分,比如 AOP、Remoting、JDBC 模板等。所以我最好的建议是让你从核心使用 Spring。

Best practice? If you're using the standard XML config, manage the size of individual files and comment them judiciously. You may think that you and others will perfectly understand your bean definitions, but in practice they're somewhat harder to come back to than plain old java code.

最佳实践?如果您使用标准 XML 配置,请管理单个文件的大小并明智地对其进行注释。您可能认为您和其他人会完全理解您的 bean 定义,但实际上它们比普通的旧 Java 代码更难理解。

Good luck!

祝你好运!

回答by mP.

First of all Spring is about modularity and works best if one focuses on writing small components that do one thing and do it well.

首先,Spring 是关于模块化的,如果专注于编写只做一件事并且做得很好的小组件,那么它的效果最好。

If you follow best practices in general like:

如果您遵循一般的最佳实践,例如:

  • Defining an interface rather than abstract classes
  • Making types immutable
  • Keep dependencies as few as possible for a single class.
  • Each class should do one thing and do it well. Big monolithic classes suck, they are hard to test and hard to use.
  • 定义接口而不是抽象类
  • 使类型不可变
  • 为单个类保留尽可能少的依赖项。
  • 每个班级要做好一件事,做好。大型单体类很糟糕,它们难以测试且难以使用。

If your components are small and follow the dogmas above they should be easy to wire up and play with other stuff. The above points are naturally also true of the Spring framework itself.

如果您的组件很小并且遵循上述教条,它们应该很容易连接并与其他东西一起玩。以上几点自然也适用于Spring框架本身。

PS

聚苯乙烯

Dont listen to the points above, they are talking about how to do whatever. Its more important to learn how to think rather than how to do something. Humans can think, repeating something is not clever, thinking is.

不要听上面的几点,他们在谈论如何做任何事情。学习如何思考而不是如何做某事更重要。人类会思考,重复某件事并不聪明,思考才是。

回答by bpapa

Start here - I actually think it's among the best Software Dev books that I've read.
Expert Spring MVC And Web Flow

从这里开始 - 我实际上认为它是我读过的最好的软件开发书籍之一。
专家 Spring MVC 和 Web Flow

Learn the new Annotation-based configuration for MVC classes. This is part of Spring 2.5. Using Annotation-based classes is going to make writing Unit tests a heck of a lot easier. Also being able to cut down on the amount of XML is a good thing.

了解 MVC 类的新的基于注解的配置。这是 Spring 2.5 的一部分。使用基于注解的类将使编写单元测试变得更加容易。能够减少 XML 的数量也是一件好事。

Oh yeah Unit Tests - if you're using Spring, you BETTER be Unit Testing. :) Write Unit tests for all of your Web and Service Layer classes.

哦,是的,单元测试 - 如果您使用 Spring,则最好进行单元测试。:) 为所有 Web 和服务层类编写单元测试。

Read up on Domain Driven Design. The fact that you can use Domain Object classes at all levels of a Spring Application means you're going to have a VERY powerful Domain Model. Leverage it.

阅读领域驱动设计。您可以在 Spring 应用程序的所有级别使用域对象类这一事实意味着您将拥有一个非常强大的域模型。利用它。

However, when using your Domain Object classes for form population, you will want to take heed of the recent security concerns around the Spring Framework. A discussion on the Server Sidereveals the way to close the hole in the comments.

但是,当使用域对象类进行表单填充时,您需要注意最近围绕 Spring 框架的安全问题。服务器端的讨论揭示了关闭评论漏洞的方法。

回答by Arcturus

I actually quite liked Spring.. It was a fresh breeze of air in your average J2EE Java Beans..

我实际上非常喜欢 Spring .. 在普通的 J2EE Java Beans 中这是一股清新的空气。

I recommend implementing the example Spring provides:

我建议实现 Spring 提供的示例:

http://static.springframework.org/docs/Spring-MVC-step-by-step/

http://static.springframework.org/docs/Spring-MVC-step-by-step/

Also, I decided to go full monty and added Hibernate to my Spring application ;), because Spring provides excellent support for Hibernate... :)

另外,我决定全力以赴,将 Hibernate 添加到我的 Spring 应用程序中 ;),因为 Spring 为 Hibernate 提供了出色的支持... :)

I do have a DON'T however, which I learned the hard way (product in production)... If you only implement the Controller interface, and return a ModelAndView object with some data as provided with the interface, Spring does garbadge collect those resources, for tries to cache those data. So be careful to put large data in those ModelAndView objects, because they will hog up your server memory for as long as the server is in the air as soon as that page has been viewed...

但是,我确实有一个 DON'T,这是我通过艰难的方式(生产中的产品)学到的...如果您只实现 Controller 接口,并返回一个 ModelAndView 对象,其中包含该接口提供的一些数据,Spring 会收集这些数据资源,用于尝试缓存这些数据。因此,请小心将大数据放入那些 ModelAndView 对象中,因为只要查看该页面后服务器在空中,它们就会占用您的服务器内存......

回答by dlinsin

A good way to get started is to concentrate on the "Springframework". The Spring portfolio has grown to a big pile of projects around various aspects of Enterprise Software. Stick to the core at the beginning and try to grasp the concepts. Downloadthe latest binaries and check out Spring's petclinic example once you are familiar with the core. It gives quite a good overview of the various projects SpringSource has to offer.

一个很好的入门方法是专注于“Springframework”。Spring 产品组合已经发展成为围绕企业软件各个方面的一大堆项目。一开始就坚持核心,尽量抓住概念。下载最新的二进制文件并在您熟悉核心后查看 Spring 的 petclinic 示例。它很好地概述了 SpringSource 必须提供的各种项目。

Although the documentation is very good, I'd recommend a bookafter you grasp the concepts of the core. What I've found problematic with the documentation, is that it's not in depth and can't give you all the details you need.

虽然文档非常好,但我还是会在你掌握了核心概念之后推荐一本书。我发现文档的问题在于它不够深入,无法为您提供所需的所有细节。

回答by duffymo

"...Which technology did you use right away: AOP, complex Hibernate..." - I'd say a better question would be to ask what people did not use right away. I'd add the examples you cite to that list.

“...您立即使用了哪种技术:AOP、复杂的 Hibernate...” - 我认为更好的问题是询问人们没有立即使用哪些技术。我会将您引用的示例添加到该列表中。

Spring MVC and JDBC template would be my starting recommendations. You can go a very long way just with those.

Spring MVC 和 JDBC 模板将是我的入门建议。你可以走很长的路。

My recommendation would be to follow the Spring architectural recommendations faithfully. Use their layering ideas. Make sure that your web layer is completely detachable from the rest. You do this by letting the web tier interact with the back end only through the service layer.

我的建议是忠实地遵循 Spring 架构建议。使用他们的分层想法。确保您的 web 图层可以与其他图层完全分离。为此,您可以让 Web 层仅通过服务层与后端交互。

If you want to reuse that service layer, a good recommendation is to expose it using Spring "contract first" web services. If you start with the XML messages that you pass back and forth, your client and server can be completely decoupled.

如果您想重用该服务层,一个好的建议是使用 Spring 的“契约优先”Web 服务来公开它。如果您从来回传递的 XML 消息开始,您的客户端和服务器可以完全解耦。

The IDE with the best Spring support is IntelliJ. It's worth spending a few bucks.

具有最佳 Spring 支持的 IDE 是 IntelliJ。花几块钱是值得的。

回答by Michael Neale

Whilst its been years since I have used spring, and I can't say I am a fan of it, I know that the App Fuse tool (https://java.net/projects/appfuse/) has been helpful to help people bootstrap in terms of generating all the artifacts you need to get going.

虽然我已经好几年没有使用 spring,我不能说我是它的粉丝,但我知道 App Fuse 工具 ( https://java.net/projects/appfuse/) 对帮助人们很有帮助引导程序生成您开始所需的所有工件。

回答by Chris J

With the release of Spring 2.5 and 3.0, I think one of the most important best practices to take advantage of now are the Spring annotations. Annotations for Controllers, Services, and Repositories can save you a ton of time, allow you to focus on the business logic of your app, and can potentially all you to make all of your object plain old Java objects (POJOs).

随着 Spring 2.5 和 3.0 的发布,我认为现在可以利用的最重要的最佳实践之一是 Spring 注释。控制器、服务和存储库的注释可以为您节省大量时间,让您可以专注于应用程序的业务逻辑,并且可能使您的所有对象成为普通的旧 Java 对象 (POJO)。

回答by martinsb

Spring is also very much about unit testing and therefore testability of your classes. That basically means thinking about modularization, separation of concerns, referencing a class through interfaces etc.

Spring 也非常关注单元测试,因此也非常关注类的可测试性。这基本上意味着考虑模块化、关注点分离、通过接口引用类等。