开发者

Creating mixin with Spring AOP Introductions

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜