Enabling AOP breaks my dependency injection for a factory bean that takes a string

非 Y 不嫁゛ 提交于 2019-12-23 20:21:15

问题


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

回答1:


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 simple execution() designator though.

Unfortunatly, I have no idea why this causes Spring to proxy the String object.




回答2:


<aop:aspectj-autoproxy/> doesn't perform proxying without a reason. Perhaps you declared some aspect whose pointcut includes Strings, therefore it have been proxied.



来源:https://stackoverflow.com/questions/4850536/enabling-aop-breaks-my-dependency-injection-for-a-factory-bean-that-takes-a-stri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!