此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
使用ProxyFactory
使用 Spring 以编程方式创建 AOP 代理很容易。这样,您就可以使用 不依赖于 Spring IoC 的 Spring AOP。
目标对象实现的接口包括 自动代理。下面的清单显示了为目标对象创建代理,其中有一个 Interceptor 和一个 Advisor:
-
Java
-
Kotlin
ProxyFactory factory = new ProxyFactory(myBusinessInterfaceImpl);
factory.addAdvice(myMethodInterceptor);
factory.addAdvisor(myAdvisor);
MyBusinessInterface tb = (MyBusinessInterface) factory.getProxy();
val factory = ProxyFactory(myBusinessInterfaceImpl)
factory.addAdvice(myMethodInterceptor)
factory.addAdvisor(myAdvisor)
val tb = factory.proxy as MyBusinessInterface
第一步是构造org.springframework.aop.framework.ProxyFactory
.您可以使用目标
object,或指定要在备用
构造 函数。
您可以添加 advice(拦截器作为一种专门的 Advice)、advisor 或两者兼而有之
并在ProxyFactory
.如果您添加了IntroductionInterceptionAroundAdvisor
中,您可以使代理实现额外的
接口。
还有一些方便的方法ProxyFactory
(继承自AdvisedSupport
)
允许您添加其他 Advice 类型,例如 before 和 throws advice。AdvisedSupport
是两者的超类ProxyFactory
和ProxyFactoryBean
.
将 AOP 代理创建与 IoC 框架集成是大多数 应用。我们建议您使用 AOP 从 Java 代码外部化配置。 一般来说,你应该这样做。 |