@Component details


javaAnnotations, like reflection and generics, belong to metaprogramming. Compared with reflective class objects, generics live in the journey of parameter typing, and annotation is hardly too comfortable, which starts from its birth (definition),@interfaceIt is a tightly coupled, declarative programming method that can not survive independently and is always with the annotated object. This paper aims to explain@componentNotes and similar notes (e.g@service)Similarities and differences.


Annotation Meaning
@Component generic stereotype for any Spring-managed component
@Repository stereotype for persistence layer
@Service stereotype for service layer
@Controller stereotype for presentation layer (spring-mvc)

be similar

The above official words only explain the purpose of such annotations in spring, but do not explain why they can be used with each other in some cases

  • Emphasize one point, forBeanDefinitionAutomatic scan detection and dependency injection, all these annotations (i.e@ Component,@ Service,@ Repository,@ Controller)They are the same, they can replace each other


  • @ComponentSpecial features of
    • Is a generic stereotype annotation indicating that the class is a spring component
    • <context:component-scan>Scan only@ComponentAnd will not find@Controller@Service@RepositoryGenerally speaking. They are scanned because they are annotated@Component, see the source code definition
public @interface Service {
public @interface Repository {
public @interface Controller {

There are a number of annotations in spring, which are similar to composite design. Some annotations are just a combination of other annotations (e.gRestController = Response+Controller), some annotations are associated with the main annotation (such as conditional annotation)

  • @Controller,@Servicealso@RepositoryIs a special type@ComponentNotes.
  • <context:component-scan>Select them and register their subsequent classes asbean, as if they were annotated@Component
  • Special types of comments are also scanned because they all have their own@ComponentAnnotated annotation


  • Indicates that the class defines a data repository
  • In addition to pointing out that this is an annotation based configuration,@Repository Platform specific exceptions are caught and re thrown asSpringUnified unchecked exceptionone of. Usually you need tospringAdd the following configuration to your application contextPersistenceExceptionTranslationPostProcessor
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>


  • Should@ControllerThe annotated class acts as a controller
  • differ@Service,@RepositoryThe program scheduler scans the annotated class and internally detects its use@RequestMappingAnnotation method

Configuring XML vs annotations

Annotations provide a lot of context in their declarations, resulting in shorter and more concise configurations. XML is good at connecting components without touching the source code or recompiling them.springNotes and inxmlConfiguration, and so-calledjavaConfiguration class, which will eventually bespring iocInside the container, expressed asBeanDefinitionObject, environment object,profileOther object instances related to

  • Note: it is a kind of distributed metadata, which is tightly bound to the source code
  • XML: it is a kind of centralized metadata without binding with source code
  • What makes XM useful is classes that use XML. What also makes annotations effective isannotation processor Otherwise, annotations are no different from annotations.
  • Annotation is an independent mechanism independent of the Java programming language. It will make the Java language better, but it is not a necessary part of the language

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]