Java Spring Social - 无法生成 CGLIB 子类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20819468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:35:10  来源:igfitidea点击:

Spring Social - Could not generate CGLIB subclass

javaspringspring-social

提问by skajake

I am getting the following error when attempting to navigate to the /connect endpoint of the spring social web module.

尝试导航到 spring 社交网络模块的 /connect 端点时出现以下错误。

What I am getting:

我得到了什么:

[Request processing failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'scopedTarget.connectionRepository' 
defined in ServletContext resource [/WEB-INF/appContext.xml]: 
Initialization of bean failed; nested exception is 
org.springframework.aop.framework.AopConfigException: 
Could not generate CGLIB subclass of class 
[class org.springframework.social.connect.jdbc.JdbcConnectionRepository]: 
Common causes of this problem include using a final class or a non-visible class; 
nested exception is java.lang.IllegalArgumentException: 
Superclass has no null constructors but no arguments were given]

Relevant portions of web.xml:

web.xml 的相关部分:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/appContext.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Relevant portion of appContext.xml:

appContext.xml 的相关部分:

<bean id="connectionFactoryLocator" 
          class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
        <property name="connectionFactories">
            <list>
                <bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                    <constructor-arg value="${facebook.clientId}" />
                    <constructor-arg value="${facebook.clientSecret}" />                
                </bean>
            </list>
        </property>
    </bean>

    <bean id="usersConnectionRepository" 
          class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository">
        <constructor-arg ref="dataSource" />
        <constructor-arg ref="connectionFactoryLocator" />
        <constructor-arg ref="textEncryptor" />
    </bean>

    <bean id="connectionRepository" factory-method="createConnectionRepository" 
          factory-bean="usersConnectionRepository" scope="request">
        <constructor-arg value="#{request.userPrincipal.name}" />
        <aop:scoped-proxy proxy-target-class="false" />
    </bean>

Thank you for any responses.

感谢您的任何回复。

回答by Krishnendu

I find this link useful https://github.com/socialsignin/socialsignin-showcase/issues/6, It solved my problem.

我发现这个链接很有用https://github.com/socialsignin/socialsignin-showcase/issues/6,它解决了我的问题。

Basically in this case problem is with AOP proxy, for some reason CGLIB proxying not working in this case. so you have to turn it off and switch to JDK proxy. So what i did was just make this change in my context.xml -

基本上在这种情况下问题出在 AOP 代理上,由于某种原因,CGLIB 代理在这种情况下不起作用。所以你必须关闭它并切换到JDK代理。所以我所做的只是在我的 context.xml 中进行此更改 -

<tx:annotation-driven transaction-manager="transactionManager" 
    proxy-target-class="true"/>

to

<tx:annotation-driven transaction-manager="transactionManager" 
    proxy-target-class="false"/>

By making proxy-target-class="false"spring will use JDK proxy (don't ask why i have no idea).

通过使proxy-target-class="false"spring 将使用 JDK 代理(不要问为什么我不知道)。

It's solved my problem.

它解决了我的问题。

But if it doesn't work for you, then also change this:

但如果它不适合你,那么也改变这个:

<security:global-method-security proxy-target-class="false" >

and this:

和这个:

<bean id="connectionFactoryLocator" class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
    <property name="connectionFactories">
        <list>
            <bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                <constructor-arg value="xxxxxxxxxxx" />
                <constructor-arg value="xxxxxxxxxxxxxxxxxxxxxxxxxxx" />             
            </bean>
        </list>
    </property>
    <aop:scoped-proxy proxy-target-class="false"/>
</bean>

Hope this helped. Happy coding :)

希望这有帮助。快乐编码:)

回答by Vadim Kirilchuk

I had similar issue, however I didn't use any of xml configs, everything was autoconfigured by spring-boot.

我有类似的问题,但是我没有使用任何 xml 配置,一切都是由 spring-boot 自动配置的。

Here is what I got trying to do a /connect/facebook:

这是我尝试执行 /connect/facebook 的操作:

There was an unexpected error (type=Internal Server Error, status=500). Error creating bean with name 'scopedTarget.connectionRepository' defined in class path resource [org/springframework/social/config/annotation/SocialConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.springframework.social.connect.jdbc.JdbcConnectionRepository]: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null

出现意外错误(类型=内部服务器错误,状态=500)。在类路径资源 [org/springframework/social/config/annotation/SocialConfiguration.class] 中定义名称为“scopedTarget.connectionRepository”的 bean 创建时出错:bean 初始化失败;嵌套异常是 org.springframework.aop.framework.AopConfigException:无法生成类 [class org.springframework.social.connect.jdbc.JdbcConnectionRepository] ​​的 CGLIB 子类:此问题的常见原因包括使用最终类或不可见班级; 嵌套异常是 org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null

The root cause was having spring-boot-devtoolsin the classpath. Removing devtools solved the problem. I also found a PR opened by some guy, please leave a comment there to bring more attention to the issue. pull request

根本原因是类路径中有spring-boot-devtools。删除 devtools 解决了这个问题。我还发现了一个人打开的 PR,请在那里发表评论以引起更多关注。 拉取请求

Upd. the PR was merged and starting from 2.0.0.M1 there is no issue with dev-tools anymore.

更新。PR 已合并,从 2.0.0.M1 开始,开发工具不再有问题。

回答by Leandy

there is two methods to slove this problem for springboot project:

springboot项目有两种方法可以解决这个问题:

  1. removing spring-boot-debtoolsdependency.
  2. setting spring.aop.proxy-target-class=falsein your application.properties.
  1. 删除spring-boot-debtools依赖项。
  2. spring.aop.proxy-target-class=false在 application.properties 中设置。