java - How to determine that the ApplicationConfig.class is marked with a custom annotation -


i enable application features based on presence of custom annotation marks applicationconfig.class below:

@foobar(enabled = true) @configuration @componentscan(basepackageclasses = application.class, excludefilters = @filter({controller.class, configuration.class})) @enablejparepositories("com.package.repository") class applicationconfig {      // application specific configs , bean defs. } 

the custom annotation named foobar:

@target({elementtype.type}) @retention(retentionpolicy.runtime) public @interface foobar   {     boolean enabled() default true; } 

during application startup detect class (or other class/bean) annotated annotation.

here attempt, partly based on similar question includes 2 ways determine annotation being used.

@component public class myclasswitheventlisteners implements applicationcontextaware {  @autowired applicationeventpublisher applicationeventpublisher;  @autowired applicationcontext applicationcontext;   @eventlistener void contextrefreshedevent(contextrefreshedevent event) {     applicationcontext applicationcontext = event.getapplicationcontext();     applicationcontext.getclassloader();      autowirecapablebeanfactory autowirecapablebeanfactory = applicationcontext.getautowirecapablebeanfactory();      string[] names = event.getapplicationcontext().getbeandefinitionnames();      (string name : names) {         object o = autowirecapablebeanfactory.getbean(name);         if (aopproxyutils.ultimatetargetclass(o).isannotationpresent(foobar.class)) {             system.out.println("found class annotated foobar");         }     }  }  @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {     this.applicationcontext = applicationcontext;     list<string> beannames = getbeanswithannotation(foobar.class);     if(beannames !=null){         system.out.println("found class annotated foobar");     } }  public list<string> getbeanswithannotation(class<? extends annotation> type) {     predicate<map<string, object>> filter = predicates.alwaystrue();     return getbeanswithannotation(type, filter); }  public list<string> getbeanswithannotation(class<? extends annotation> type, predicate<map<string, object>> attributefilter) {      list<string> result = lists.newarraylist();      configurablelistablebeanfactory factory = ((configurableapplicationcontext) applicationcontext).getbeanfactory();     (string name : factory.getbeandefinitionnames()) {         beandefinition bd = factory.getbeandefinition(name);          if (bd.getsource() instanceof standardmethodmetadata) {             standardmethodmetadata metadata = (standardmethodmetadata) bd.getsource();              map<string, object> attributes = metadata.getannotationattributes(type.getname());             if (null == attributes) {                 continue;             }              if (attributefilter.apply(attributes)) {                 result.add(name);             }         }     }      return result; } } 

both contextrefreshedevent() , setapplicationcontext() methods called , neither able detect custom annotation.

what have observed applicationconfig.class present in list of beans/classes appears follows:

com.package.config.applicationconfig$$enhancerbyspringcglib$$15073fb3@196887

  • what enhancedbyspring?
  • how spring add functionality? @enablejparepositories adds repositories. replicate functionality own purposes.

the core container has support stuff there no need hack context. may benefit reading what's happening when 1 adds @enablexyz on configuration class. instance @enablecaching or @enablejms work pretty same way:

the interface meta-annotated @import leads more beans being loaded context. enabled part bit useless imo. presence or absence of annotation plays same role , more explicit.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -