Java 什么是注解,它们实际上如何为 Spring 等框架工作?

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

What are annotations and how do they actually work for frameworks like Spring?

javaspringspring-mvcannotations

提问by Rachel

I am new to Spring and now a days I hear a lot about Spring Framework. I have two sets of very specific questions:

我是 Spring 的新手,现在我听到很多关于 Spring Framework 的信息。我有两组非常具体的问题:

Set No. 1:

第 1 组:

  • What are annotations in general ?

  • How does annotations works specifically with Spring framework ?

  • Can annotations be used outside Spring Framework or are they Framework specific ?

  • 什么是注解?

  • 注释如何专门用于 Spring 框架?

  • 注释可以在 Spring 框架之外使用还是特定于框架?

Set No. 2:

第 2 组:

  • What module of Spring Framework is widely used in Industry ?

  • I think it is Spring MVC but why it is the most used module, if am correct or correct me on this ?

  • Spring Framework 的哪些模块在行业中被广泛使用?

  • 我认为它是 Spring MVC 但为什么它是最常用的模块,如果我正确或纠正我呢?

I am newbie to Spring and so feel free to edit this questions to make more sense.

我是 Spring 的新手,因此请随时编辑这些问题以使其更有意义。

采纳答案by duffymo

What are annotations in general?

什么是注解?

Annotations can be thought of as meta-data for classes.

注释可以被认为是类的元数据。

How does annotations works specifically with Spring framework?

注释如何专门用于 Spring 框架?

Spring uses annotations as an alternative to XML for declarative configuration. Some people don't like XML.

Spring 使用注解作为 XML 的替代方案来进行声明式配置。有些人不喜欢 XML。

Can annotations be used outside Spring Framework or are they Framework specific?

注释可以在 Spring Framework 之外使用还是特定于框架?

You need the implementation JARs to use any annotation, so if you use the annotations you areusing Spring. Annotations are a general idea introduced into Java 5. You can write your own as well.

您需要实现 JAR 来使用任何注解,因此如果您使用注解,您使用 Spring。注释是引入到 Java 5 中的一般概念。您也可以编写自己的注释。

What module of Spring Framework is widely used in Industry?

业界广泛使用Spring Framework的哪些模块?

Spring Core is used by all the other modules; hence the name.

所有其他模块都使用 Spring Core;由此得名。

I think it is Spring MVC but why it is the most used module, if am correct or correct me on this ?

我认为它是 Spring MVC 但为什么它是最常用的模块,如果我正确或纠正我呢?

Absolutely incorrect. Spring MVC has lots of competitors (e.g., Struts 1 and 2, JSF, Wicket, Flex, etc.), so it's not always the first choice for web MVC. And all apps that use Spring aren't web apps. Spring Batch and Integration are quite popular and growing.

绝对不正确。Spring MVC 有很多竞争对手(例如,Struts 1 和 2、JSF、Wicket、Flex 等),因此它并不总是 Web MVC 的首选。并且所有使用 Spring 的应用程序都不是 Web 应用程序。Spring Batch 和 Integration 非常流行并且不断增长。

回答by Jeff Storey

Annotations were introduced in Java 5 http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.htmland are not Spring specific. In general, annotations allow you to add metadata to a class, method or variable. An annotation can be interpreted by the compiler (for example, the @Overrideannotation) or by a framework such as spring (for example, the @Componentannotation).

注释是在 Java 5 http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html中引入的,并且不是 Spring 特定的。通常,注释允许您向类、方法或变量添加元数据。注解可以由编译器(例如@Override注解)或框架(例如 spring)解释(例如@Component注解)。

Spring has many different annotations, so you would need to be more specific about which annotations you have questions about. But Spring annotations can only be used within Spring, and other annotations can be used within other frameworks.

Spring 有许多不同的注释,因此您需要更具体地说明您对哪些注释有疑问。但是Spring注解只能在Spring内部使用,其他的注解可以在其他框架内使用。

For Set No 2 of questions, that should be opened as a second questions since it doesn't relate to the annotations.

对于第 2 组问题,应该作为第二个问题打开,因为它与注释无关。

回答by stacker

What are annotations in general ?

什么是注解?

Annotations are a convienient way of configuration, which could also be done using explicit configuration files, like deployment descriptors.

注释是一种方便的配置方式,也可以使用显式配置文件(如部署描述符)来完成。

How does annotations works specifically with Spring framework ?

注释如何专门用于 Spring 框架?

In spring they are used for dependency injection and many other purposes

在 spring 中,它们用于依赖注入和许多其他目的

Can annotations be used outside Spring Framework or are they Framework specific ?

注释可以在 Spring 框架之外使用还是特定于框架?

Yes you can even write your own annotations. They have been introduced in java 1.5 for i.g. suppresing warnings.

是的,您甚至可以编写自己的注释。它们已在 java 1.5 中引入,用于 ig 抑制警告。

What module of Spring Framework is widely used in Industry ?

Spring Framework 的哪些模块在行业中被广泛使用?

The spring core module is used in all applications and therefore mostly used.

spring 核心模块用于所有应用程序,因此使用最多。

You should also browse the Documentationto get a first impression what the spring framework and its modules cover.

您还应该浏览文档以获得 Spring 框架及其模块涵盖的第一印象。

回答by user184794

Annotations in Java programing language is a special form of metadata that can be embedded in Java source code. The use of annotations in Java language is introduced in Java 5.0 ie Java 5 provides metdata support at language level.

Java 编程语言中的注解是一种特殊形式的元数据,可以嵌入到 Java 源代码中。Java 5.0 中引入了Java 语言中注释的使用,即Java 5 在语言级别提供元数据支持。

In Spring, XML based configurations is the most popular configuration style.. Then annotation based configuration style came which enables users to configure beans inside the java source file itself. Spring framework provides different custom java5+ annotations. These annotations can be used in transactional demarcation, aop, JMX etc. There are core Spring Annotations, Spring MVC Annotations, AspectJ Annotations, JSR-250 Annotations, Testing Annotations etc. Both xml based configurations and Annotations have pros and cons. I would suggest mixing the two.

在 Spring 中,基于 XML 的配置是最流行的配置风格。然后出现了基于注解的配置风格,它使用户能够在 java 源文件本身内部配置 bean。Spring 框架提供了不同的自定义 java5+ 注解。这些注解可用于事务划分、aop、JMX 等。核心有 Spring Annotations、Spring MVC Annotations、AspectJ Annotations、JSR-250 Annotations、Testing Annotations 等。基于 xml 的配置和 Annotations 各有利弊。我建议将两者混合。

The custom Spring annotations are framework specific. But you can write your own annotations also.

自定义 Spring 注释是特定于框架的。但是您也可以编写自己的注释。

The Core container module is the most important module which handles the basic principle of Dependency Injection and it's used in all other modules in the framework. Spring MVC is only a web MVC framework built on Spring's core functionality.

核心容器模块是最重要的模块,它处理依赖注入的基本原理,并用于框架中的所有其他模块。Spring MVC 只是一个建立在 Spring 核心功能之上的 Web MVC 框架。

You can go through Spring documentation and books like Spring in Actionand Spring Recipesto get a good idea about the framework.

您可以阅读 Spring 文档和诸如Spring in ActionSpring Recipes 之类的书籍,以对框架有一个很好的了解。