aspectj - What are the option for spring @Advice to communicate with some other service? -


we using spring spring aop. regular spring bean/service/repository (i.e @service , etc) @autowired annotation works well. but, won't work out of box @advice class.

my question is:

what option @advice communicate @service?

thanks!

some example

  1. search db (using jpa;hibernate) within advice
  2. raise event in system, using applicationeventpublisher
  3. call function defined in x service.

i have put simple experiment app:

@retention(retentionpolicy.runtime) public @interface injectadvicehere { }  @service public class fooservice {     public void foo(){         system.out.println("foo");     } }  @service public class barservice {     @injectadvicehere     public void bar(){         system.out.println("bar");     } }   @aspect @component public class myaspect {      private final fooservice fooservice;      @autowired     public myaspect(fooservice fooservice) {         super();         this.fooservice = fooservice;     }      @before("@annotation(ann)")     public void advize(injectadvicehere ann) {         fooservice.foo();     }  }  @springbootapplication @enableaspectjautoproxy public class demoapplication {      private @autowired barservice barservice;      public static void main(string[] args) {         springapplication.run(demoapplication.class, args);     }      @postconstruct     public void test(){         barservice.bar();     } } 

output is:

foo bar 

as can see, in spring aop aspects treated other beans , can have dependencies injected normally.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -