开发者

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

开发者 https://www.devze.com 2023-02-08 03:56 出处:网络
Enabling AOP breaks my dependency injection for a factory bean that takes a string. Here\'s the fragment from the context file:

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 开发者_如何学GoString 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消