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
- search
db
(usingjpa;hibernate
) withinadvice
- raise event in system, using
applicationeventpublisher
- 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
Post a Comment