【问题标题】:Error when using @RequestScope to create Final Class Bean使用@RequestScope 创建最终类 Bean 时出错
【发布时间】:2020-04-09 09:33:04
【问题描述】:

使用@RequestScope 创建String Bean 会报错。但是使用 @Scope("request") 不会。 @RequestScope 不是 @Scope("request") 的快捷方式吗?

下面是代码sn-p。

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @RequestScope
    @Bean(name = "requestBean")
    public String hiRequest() {

        return new String("Hi Request");

    }

}

以下是错误信息。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestBean' defined in BeanDefinition defined in com.test.SpringTest.Application: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class java.lang.String: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class java.lang.String
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:584) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:827) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
    at com.test.SpringTest.Application.main(Application.java:12) [classes/:na]

感谢您的回复!

【问题讨论】:

  • 现在清楚为什么这不起作用了吗? @MichaelOuyang
  • 谢谢,我需要看看 ScopedProxyMode。

标签: spring cglib spring-ioc


【解决方案1】:

documentation@RequestScope中可以看到代理模式是:

ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;

创造

基于类的代理(使用 CGLIB)。

使用 CGLIB Spring 将能够创建一个代理,其类是目标类的子类。

对于@Scope("request"),根据documentation,代理模式为:

ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;

意思是

    /**
     * Default typically equals {@link #NO}, unless a different default
     * has been configured at the component-scan instruction level.
     */
    DEFAULT,

    /**
     * Do not create a scoped proxy.
     * <p>This proxy-mode is not typically useful when used with a
     * non-singleton scoped instance, which should favor the use of the
     * {@link #INTERFACES} or {@link #TARGET_CLASS} proxy-modes instead if it
     * is to be used as a dependency.
     */
    NO,

这就是它适用于@Scope("request") 而不适用于@RequestScope 的原因

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 2012-12-16
    • 1970-01-01
    • 2013-01-25
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 2016-06-24
    相关资源
    最近更新 更多