Spring Boot Combat Notes (ix)--Spring advanced topics (combination annotations and meta annotations)

Source: Internet
Author: User

I. Combination annotations and meta-annotations

Starting with spring 2, in response to the annotation functionality introduced by JDK 1.5, spring began to add annotations to replace the XML configuration in large numbers. Spring annotations are mainly used to configure the injected bean, and the facets-related configuration (@Transactional). With the extensive use of annotations, it is quite verbose to use the same multiple annotations in each class. This is called template code and is the code to be eliminated in spring design principles.

The so-called meta-annotations are annotations to other annotations, annotated annotations are called combined annotations, and combinations of annotations have the function of meta-annotations. Many of the annotations in spring can be used as meta annotations, and spring itself has many combinations of annotations, such as @configuration, a combination of @component annotations, which indicates that the class is actually a bean.

Using @configuration and @componentscan annotations to the configuration class in previous studies, the following two meta-annotations are grouped together so that we can simply write an annotation to represent two annotations.

Example:

(1) Example combination annotations

 Packagecom.ecworking.annotation;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;Importjava.lang.annotation.*; @Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Documented @configuration//combining @configuration meta-annotations@ComponentScan//combining @componentscan meta-annotations Public@Interfacewiselyconfiguration {string[] value ()default{};//overriding the value parameter}

(2) Demo Service Bean.

 Package com.ecworking.annotation; Import Org.springframework.stereotype.Service; @Service  Public class Demoservice {    publicvoid  Outputresult () {        System.out.println ( "The Bean is also available from the combination annotation configuration");}    }

(3) New configuration class

Package// use @wiselyconfiguration instead of @configuration and @componentscanpublic  class Democonfig {}

(4) Operation

 Packagecom.ecworking.annotation;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext; Public classMain { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Democonfig.class); Demoservice Demoservice= Context.getbean (Demoservice.class);        Demoservice.outputresult ();    Context.close (); }}

Operation Result:

Spring Boot Combat Notes (ix)--Spring advanced topics (combination annotations and meta annotations)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.