使用 Spring 3 注解的最佳实践

发布于 2024-11-02 15:15:11 字数 422 浏览 2 评论 0原文

我正在寻找使用 Spring 3 注释时的一些最佳实践

我目前正在转向 Spring 3,从到目前为止我所读到的内容来看,我发现很多重点都放在使用注释和远离 XML 配置上。

实际上,推荐的是两种样式的混合,注释涵盖不会经常更改或从一次运行到下一次运行的内容(例如 @Controller 将在整个生命周期中保持这样的状态)应用程序),而更改且必须可配置的内容则放入 XML 中(例如邮件 smtp 地址、应用程序与之通信的 Web 服务端点等)。

我的问题是注释中应该包含哪些内容以及在多大程度上?

在什么时候注释会使事情变得更困难而不是更容易?该技术(Spring 3)是否被完全采用以能够做出这样的陈述,或者人们是否需要更多时间来获得它的经验,然后反思这个问题?

I'm looking for some best practices when using Spring 3 annotations.

I'm currently moving to Spring 3 and from what I've read so far I see a lot of accent placed on using annotations and moving away from XML configuration.

Actually what is recommended is a mix of both styles, with annotations covering things that won't change often or from one run to the next (e.g. a @Controller will remain like that for the life time of the application), while the things that change and must be configurable go into XML (e.g. a mail smtp address, endpoints for web services that your application talks to etc).

My question is what should go into annotations and to what extent?

At which point annotations make things harder instead of easier? Is the technology (Spring 3) fully adopted as to be able to make such statements or does it take some more time for people to gain experience with it and then reflect on the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空心空情空意 2024-11-09 15:15:11

获得真正的先进信息总是很困难。

简单的教程“看看我的博客,我从 Spring Source 网站复制了 hello word 教程......现在你可以在任何地方放置精美的注释,它解决了我们所有的问题,包括癌症和饥饿。”并不是很有用。

目的,其中包括:

  • 如果您还记得正确的话,
  • 弹簧芯有几个
    的实现/配置
    bean随时
  • 给予集中控制
    放置配置的地方

注释无法满足所有这些需求:

  • 它们引入了与 spring 的耦合
    (您只能使用标准注释
    但只要你至少有一个
    弹簧注解这不再是
    true)
  • 您需要修改源代码并且
    重新编译以更改bean
    实现或配置
  • 注释在你的应用程序中随处可见
    代码。可能很难找到
    什么bean将被真正使用
    读取代码或 XML 配置
    文件。

事实上,我们已经转移了焦点:

  • 我们意识到我们几乎从未
    提供了几种实现
    我们的服务。
  • 我们意识到依赖于
    API 并没有那么糟糕。
  • 我们意识到我们不仅使用spring
    用于真正的依赖注入
    不再,但主要是为了增加
    生产力并减少 java 代码
    冗长。

所以当注释有意义时我会使用它。当纯粹是为了删除样板代码时,冗长的内容。我会负责使用 XML 配置文件来实现您想要配置的内容,即使它只是在单元测试中提供服务的存根实现。

It is always difficult to get real advanced information.

The easy tutorial with "look on my blog, I copied the hello word tutorial from Spring Source website... Now you can put fancy annotations everywhere, it the solution of all of our problems including cancer and starvation." is not really usefull.

If you remember right spring core had several purposes, among them:

  • to be non intrusive
  • to change any
    implementation/configuration of a
    bean at any time
  • to give a centralized and controlled
    place to put your configuration

Anotation fail for all theses needs:

  • They introduce coupling with spring
    (you can use standard anotation only
    but as soon as you have at least one
    spring anotation this is no longer
    true)
  • You need to modify source code and
    recompile to change bean
    implementation or configuration
  • Annotations are everywhere in your
    code. It can be difficult to find
    what bean will be really used just by
    reading the code or XML configuration
    file.

In fact we have shifted our focus:

  • We realized that we almost never
    provide several implementations of
    our services.
  • We realised that being dependant of
    an API is not that bad.
  • We realized that we use spring not only
    for real dependancy injection
    anymore, but mainly to increase
    productivity and reduce java code
    verbosity.

So I would use anotations when it make sence. When it is purerly to remove boilerplate code, verbosity. I would take care of using the XML configuration file for thing that you want to make configurable, even if it is only to provide a stub implementation of the service in unit tests.

一影成城 2024-11-09 15:15:11

正如 kunal 所指出的,我使用 @Value 作为通过 PropertyPlaceholderConfigurer 在外部属性文件中配置的属性。

对于何时使用 xml 没有严格的界限,但我使用 xml:

  • 当 bean 不是类时,我控制
  • 对象何时与基础设施或配置相关,而不是与业务逻辑相关。
  • 当类具有一些我想要配置的原始属性时,但不一定通过外部化配置。

回应你的评论:spring被广泛采用,但“好”和“坏”是非常主观的。甚至我的台词也不是普遍真理。 XML、注释和编程配置都是有目的的,每个开发人员/公司都有自己的偏好。

正如我所说 - 注释没有严格的界限,也没有通用的良好实践。

I use @Value for properties that are configured in external properties file via PropertyPlaceholderConfigurer, as kunal noted.

There is no strict line for when to use xml, but I use xml:

  • when the bean is not a class I control
  • when the object is related to the infrastructure or configuration rather than to the business logic.
  • when the class has some primitive properties that I would like configurable, but not necessarily via externalized configurations.

In response to your comment: spring is very widely adopted, but "good" and "bad" are very subjective. Even my lines are not universal truths. XML, annotations and programmatic configuration all exists for a purpose, and each developer / company have their preferences.

As I said - there is no strict line, and no universal good practice for annotations.

痴骨ら 2024-11-09 15:15:11

注解无疑是 Java 中“更新”编程的继续方式。我将注释用于各种用途...例如 @Scope 用于 bean 范围,@Required 用于使依赖成为必需,@Aspect 用于配置建议,@Autowired 用于使用注释的构造函数注入。从spring 2.5开始,注解支持就已经很好了。
请参阅此处的 spring 教程,其中涵盖了基于注释的问题 此处

Annotations are surely the way by which "newer" programming in java will continue. I use annotations for various uses...like @Scope for scope of bean, @Required for making dependency necessary, @Aspect for configuring advices,@Autowired for constructor injection using annotations. Since spring 2.5, annotation support has been good.
See here for spring tutorial, where annotation based issue are covered here.

帝王念 2024-11-09 15:15:11

我认为使用注释的两种情况可能会导致一些问题。首先,如果您想在实体中编写复杂的命名查询(JPA)。我看到了一些实体代码示例,并问自己这些代码是否真的是java代码。程序代码中的许多元数据会降低其可读性,从而破坏了干净代码的原则。

第二个问题是 JVM 版本之间的可移植性。注释是 1.5+ 的一项功能。如果您的软件应该支持早期的 JVM 版本,那么您可能不会使用这些版本。

无论如何,您每次都可以毫无疑问地享受注释,并且节省时间,无需更改 IDE 选项卡来检查 XML 属性是否仍然存在,或者输入正确等。

对于非常小的项目,如果您没有,您仍然可以使用 XML 版本春天没有太多需要宣布的事情。但是,如果你在一个巨大的项目中,如果你有 10 个 xml 配置,事情可能会非常麻烦。

I think that two cases that the usage of annotations could cause some problems. Firstly, if you want to write complex named queries (JPA) in your entities. I saw some entity code samples and asked myself whether the code is really java code or not. To many metadata in program code will reduce the readability of it which kills clean code principles.

Second problem is portability between JVM versions. Annotation is a feature 1.5+. If your software should support earlier JVM versions, then you may not use these.

Anyway, you can enjoy with annotations everytime without having any doubt and spare your time not changing IDE tabs to check XMLs if the property is still there or not, or entered correct etc.

For very small projects you could still XML version if you haven't too many stuff to be declared in spring. But, if you are in a huge project, the things could be very troublesome if you had 10 xml configs.

星光不落少年眉 2024-11-09 15:15:11

这可能不会对您有太大帮助,但在工作中他们不想使用自动装配,因为它需要类路径扫描(但我认为这可以是包定义的)。因此它会根据项目的大小增加应用程序的启动时间。

This will perhaps not help you much but at work they don't want to use autowiring because it needs a classpath scan (but that can be package-defined i think). So it increases the startup time of the application according to the size of the project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文