BeanNameAutoProxyCreator setBeanNames正则表达式不起作用? [英] BeanNameAutoProxyCreator setBeanNames regular expression not working?

查看:244
本文介绍了BeanNameAutoProxyCreator setBeanNames正则表达式不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的BeanNameAutoProxyCreator:

@Bean
public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
    BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
    beanNameAutoProxyCreator.setBeanNames("*service"); // If i put "*Service" instead, then i get an exception. 
    beanNameAutoProxyCreator.setInterceptorNames("loggingAdvice", "debugInterceptor");
    return beanNameAutoProxyCreator;
}

@Bean
public LoggingAdvice loggingAdvice() {
    return new LoggingAdvice();
}

@Bean
public DebugInterceptor debugInterceptor() {
    return new DebugInterceptor();
}

这是我的ClientService bean:

And this is my ClientService bean:

@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public ClientService clientService() {
    return new ClientServiceImpl(this.clientDAO);
}

所以,我不知道为什么,当我打电话给setBeanNames("*service")时,没有正确设置拦截器.如果我输入"* Service",则会出现此异常:

So, i don't know why, when i call setBeanNames("*service") is not setting properly the interceptors. If i put "*Service" then i get this exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy68 cannot be cast to org.springframework.format.support.FormattingConversionService
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.example.movies.service.Application.main(Application.java:9)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy68 cannot be cast to org.springframework.format.support.FormattingConversionService
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 17 common frames omitted
Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy68 cannot be cast to org.springframework.format.support.FormattingConversionService
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$f6264082.mvcConversionService(<generated>)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.getConfigurableWebBindingInitializer(WebMvcConfigurationSupport.java:492)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerAdapter(WebMvcConfigurationSupport.java:461)
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration.requestMappingHandlerAdapter(WebMvcAutoConfiguration.java:343)
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$f6264082.CGLIB$requestMappingHandlerAdapter$1(<generated>)
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$f6264082$$FastClassBySpringCGLIB$$37e805df.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
    at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$f6264082.requestMappingHandlerAdapter(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 18 common frames omitted

如果我输入"clientService",它可以正常工作,但这不是我想要的.我希望所有以服务"结尾的bean都可以被拦截.但是由于某种原因,"*服务"无法正常工作.

And if I put "clientService" it works fine but that is not what I want. I want that all beans that ends with "Service" then be intercepted. But for some reason, "*Service" is not working.

推荐答案

您必须设置

beanNameAutoProxyCreator.setProxyTargetClass(true);

,以便Spring使用CGLIB代理您的实例.否则,它将使用仅支持接口的JDK代理.

so that Spring uses CGLIB to proxy your instances. Otherwise, it uses JDK proxies which only support interfaces.

请注意,您当前正在(不知不觉中)代理可能由Web MVC配置注册的bean.这些不一定支持代理(例如final类).

Note that you're currently proxying (knowingly or unknowingly) beans that may be registered by your Web MVC configuration. These might not necessarily support proxying (final classes for example).

这篇关于BeanNameAutoProxyCreator setBeanNames正则表达式不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆