启用 AOP 会破坏我对带有字符串的工厂 bean 的依赖注入
启用 AOP 会破坏我对带有字符串的工厂 bean 的依赖注入。
这是上下文文件中的片段:
<aop:aspectj-autoproxy/>
<bean id="foo"
class="FooFactory"
p:url-ref="url"/>
<bean id="url" class="java.lang.String">
<constructor-arg value="#{ 'localhost:50131'}"/>
</bean>
这是工厂 bean。
public class FooFactory extends AbstractFactoryBean<Foo> {
private String url;
public void setUrl(final String url) {
this.url = url;
}
@Override
public Class<?> getObjectType() {
return Foo.class;
}
@Override
protected Foo createInstance() throws Exception {
Validate.notNull(url, "null URL");
return new FooFactory().createFoo(new String[]{url});
}
}
这是唯一声明的方面:
@Component
@Aspect
public class ProfilerAspect {
@Around("@target(org.springframework.stereotype.Controller) && args(model,..)")
public Object profileController(final ProceedingJoinPoint proceedingJoinPoint, final Model model) throws Throwable {
return proceedingJoinPoint.proceed();
}
}
这是例外
java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,java.lang.Comparable,java.lang.CharSequence,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [java.lang.String] for property 'url': no matching editors or conversion strategy found
Enabling AOP breaks my dependency injection for a factory bean that takes a string.
Here's the fragment from the context file:
<aop:aspectj-autoproxy/>
<bean id="foo"
class="FooFactory"
p:url-ref="url"/>
<bean id="url" class="java.lang.String">
<constructor-arg value="#{ 'localhost:50131'}"/>
</bean>
Here's the factory bean.
public class FooFactory extends AbstractFactoryBean<Foo> {
private String url;
public void setUrl(final String url) {
this.url = url;
}
@Override
public Class<?> getObjectType() {
return Foo.class;
}
@Override
protected Foo createInstance() throws Exception {
Validate.notNull(url, "null URL");
return new FooFactory().createFoo(new String[]{url});
}
}
Here is the only declared aspect:
@Component
@Aspect
public class ProfilerAspect {
@Around("@target(org.springframework.stereotype.Controller) && args(model,..)")
public Object profileController(final ProceedingJoinPoint proceedingJoinPoint, final Model model) throws Throwable {
return proceedingJoinPoint.proceed();
}
}
And this is the exception
java.lang.IllegalStateException: Cannot convert value of type [$Proxy13 implementing java.io.Serializable,java.lang.Comparable,java.lang.CharSequence,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [java.lang.String] for property 'url': no matching editors or conversion strategy found
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来,它与切入点表达式中的
@target
指示符有关。我可以使用与您类似的简单设置来重现该行为(仅在切入点中使用自定义注释)。不过,它与简单的execution()
指示符一起工作得很好。不幸的是,我不知道为什么这会导致 Spring 代理 String 对象。
It seems, that it has to do with the
@target
designator in the pointcut expression. I can reproduce the behaviour with a simple setup similar to yours (with only a custom annotation in the pointcut). It works fine with a simpleexecution()
designator though.Unfortunatly, I have no idea why this causes Spring to proxy the String object.
不会无故执行代理。也许您声明了某个方面,其切入点包含String
,因此它已被代理。<aop:aspectj-autoproxy/>
doesn't perform proxying without a reason. Perhaps you declared some aspect whose pointcut includesString
s, therefore it have been proxied.