除了使用<aop:config>或<aop:aspectj-autoproxy>在配置中声明方面之外,还可以通过编程方式创建通知目标对象的代理。有关Spring的AOP API的完整详细信息,请参阅下一章。在这里,我们将重点介绍使用@Aspectj特性自动创建代理的能力。
可以使用org.springframework.aop.aspectj.annotation.AspectJProxyFactory类为一个或多个@Aspectj方面建议的目标对象创建代理。这个类的基本用法非常简单,如下示例所示:
// create a factory that can generate a proxy for the given target object AspectJProxyFactory factory = new AspectJProxyFactory(targetObject); // add an aspect, the class must be an @AspectJ aspect // you can call this as many times as you need with different aspects factory.addAspect(SecurityManager.class); // you can also add existing aspect instances, the type of the object supplied must be an @AspectJ aspect factory.addAspect(usageTracker); // now get the proxy object... MyInterfaceType proxy = factory.getProxy();
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8