Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating mixin with Spring AOP Introductions

Tags:

spring

mixins

aop

Could someone provide a sample code snippet that stitches two java interfaces using spring-aop introduction (mixin)?

I'm looking for AspectJ annotation style configuration. Also, the specific use case I have is to stitch a few java beans each implementing their own interfaces together. So, rather than having a delegate coded, if I could just get away by using Spring XML, it'd be awesome.

like image 531
kuriouscoder Avatar asked Jan 27 '11 05:01

kuriouscoder


2 Answers

You can use @DeclareParents or <aop:declare-parents> to get the mixin behavior. For example,

@DeclareParents(value="service.*", defaultImpl=AuditRecorderDefaultImpl.class)
private AuditRecorder mixin;

will mixin all classes in the service package with the AuditRecorder interface automatically forwarding each method to AuditRecorderDefaultImpl.

You can see working examples of this from AspectJ in Action's downloadable sources. You can also see detailed explanation in Spring documentation.

like image 107
ramnivas Avatar answered Sep 19 '22 07:09

ramnivas


A demo based on Spring in Action book 4th edition is here, the configuration is JavaConfig style with @ComponentScan

like image 45
fall Avatar answered Sep 22 '22 07:09

fall