What are annotations and how do they actually work

2020-01-29 06:44发布

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

Set No. 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 ?

Set No. 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 ?

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

4条回答
老娘就宠你
2楼-- · 2020-01-29 07:14

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.

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.

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

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.

You can go through Spring documentation and books like Spring in Action and Spring Recipes to get a good idea about the framework.

查看更多
一纸荒年 Trace。
3楼-- · 2020-01-29 07:21

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 ?

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

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

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

What module of Spring Framework is widely used in Industry ?

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

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

查看更多
霸刀☆藐视天下
4楼-- · 2020-01-29 07:22

Annotations were introduced in Java 5 http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html and 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 @Override annotation) or by a framework such as spring (for example, the @Component annotation).

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.

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

查看更多
smile是对你的礼貌
5楼-- · 2020-01-29 07:30

What are annotations in general?

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

How does annotations works specifically with Spring framework?

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

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

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

What module of Spring Framework is widely used in Industry?

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

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

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.

查看更多
登录 后发表回答