我有一块弹簧AOP通知的未触发时在auth(..)方法被调用创建的对象:Spring AOP的由工厂,是豆
@AfterReturning(pointcut="execution(* auth(..))",
returning="result")
public void trigger(final Boolean result) {
System.out.println("I was triggered " + result);
}
这是因为身份验证对象与该验证方法是不是一个Spring bean,但它是由一个工厂,是一个Spring bean创建:
<bean id="passwordUserAuthFact" class="foo.bar.PasswordAuthFactory">
...
</bean>
<bean id="server" class="foo.bar.Server">
<property name="authFactory" ref="passwordUserAuthFact"/>
...
</bean>
所以,当应用程序执行:
session.authenticator = server.getAuthFactory().create();
... session.authenticator
是一个未加密的简单PasswordAuthFactory,AOP不会发生在它上面。
在我看来,应该有可能告诉Spring豆类是工厂,以便它可以将工厂包装在代理中,使得由create()
返回的对象本身被包装在AOP代理中。
也就是说,春天会动态地创建在我厂的包装是这样的:
public Foo create() {
Foo instance = target.create();
return wrapWithAopProxy(instance);
}
,但我看不到一个现成的办法做到这一点。
我不想让我的课程能够感知春天。工厂类本身就是第三方,所以我宁愿选择不改变源代码的解决方案。
是否有共同的模式来实现这一目标?或者更好的方法来解决这个问题?
hope [this](http://stackoverflow.com/questions/2470014/how-to-create-an-aspect-on-class-that-is-not-a-bean-using-spring- aop)有助于 – 2015-02-25 06:26:23
有点 - 它说“你不能用代理来做到这一点,使用加载时编织”。将看看是否有人说不同。 – slim 2015-02-25 14:26:49