Implementation of spring custom annotation scanning

Source: Internet
Author: User

Goal: Implement custom Spring Auto-scan annotations. Provide technical support for late implementation of distributed service Framework automatic annotations

Technical analysis: Make spring parse labels by configuring component scan labels.

1. Jewelscanbeandefaultparser.java

 Public classJewelscanbeandefaultparser implements Beandefinitionparser {Private StaticFinal String Resource_pattern ="/**/*.class"; PrivateResourcepatternresolver Resourcepatternresolver =NewPathmatchingresourcepatternresolver ();  PublicBeandefinition Parse (element element, ParserContext parsercontext) {String basepackage= Element.getattribute ("Base-package"); if(Stringutils.hastext (basepackage)) {Try{String pattern= Resourcepatternresolver.classpath_all_url_prefix +Classutils.convertclassnametoresourcepath (basepackage)+Resource_pattern; Resource[] Resources=resourcepatternresolver.getresources (pattern); Metadatareaderfactory readerfactory=NewCachingmetadatareaderfactory ( This. Resourcepatternresolver);  for(Resource resource:resources) {if(Resource.isreadable ()) {Metadatareader Reader=Readerfactory.getmetadatareader (Resource); String ClassName=reader.getclassmetadata (). GetClassName (); Class<?> Clazz =Class.forName (className); Jewelservice Jewelservice= Clazz.getannotation (Jewelservice.class); if(NULL!=Jewelservice)                        {annotationbeandefinitionloader.load (Jewelservice, Clazz, ParserContext); } jewelremote jewelremote= Clazz.getannotation (jewelremote.class); if(NULL!=jewelremote)                        {annotationbeandefinitionloader.load (Jewelremote, Clazz, ParserContext); }                    }                }            } Catch(IOException e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        return NULL; }}

2. Annotation interface Definition

Jewelremote.java

@Documented @target (Elementtype.type) @Retention (retentionpolicy.runtime) public @interface jewelremote {public String name () default "";p ublic string version () default "v_0.0.1";p ublic string Registry () default "registry";}

Jewelservice.java

@Documented @target (Elementtype.type) @Retention (retentionpolicy.runtime) public @interface Jewelservice {public String name () default "";p ublic string version () default "v_0.0.1";p ublic string Registry () default "registry";}

Implementation of spring custom annotation scanning

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.