【问题标题】:Spring-Exception in thread "main" java.lang.IllegalStateException [duplicate]线程“主”java.lang.IllegalStateException中的Spring-Exception [重复]
【发布时间】:2020-11-07 18:58:58
【问题描述】:

SportConfig.java

package luv2code;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;


@Configuration

public class SportConfig {

//  define bean for our sad fortune service

    @Bean
    public Fortune sadfortuneService() {
         return new SadFortuneService();
    }
    
//  define bean for our swim coach AND inject dependency
    
    @Bean 
    public Coach swimCoach() {
        return new SwimCoach(sadfortuneService());
    }
}

当我运行 main 方法时,出现以下异常:

Exception in thread "main" java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or remove the following @Configuration bean definitions: [sportConfig]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:214)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:145)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:65)
    at luv2code.JavaConfiguartionDemoApp.main(JavaConfiguartionDemoApp.java:9)

【问题讨论】:

  • 错误信息从字面上解释了解决问题应该采取的措施...
  • 请发布您的构建脚本
  • 在创建 'swimCoach' 作为方法参数时,你能自动装配 'sadfortuneService' bean 吗?

标签: java spring spring-boot


【解决方案1】:

我猜您没有使用 Maven 或 Gradle 等适当的构建工具以最简单的方式设置您的 Spring Boot 项目,请参阅以下问题:
CGLIB is required to process @Configuration classes 我建议你从头开始,按照经典教程设置一个新的 Spring Boot 项目,这样你就不会遇到 IllegalStateException。

如果您可以使用 IDE(Eclipse 或 IntelliJ)来设置 Spring Boot 项目,请尝试这种方式。在我的德语博客中,我编写了使用 Eclipse 的分步教程:
https://agile-coding.blogspot.com/2020/09/microservices-mit-spring-boot-erstellen.html

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多