【问题标题】:spring boot compilation cannot find symbol componentscanspring boot 编译找不到符号componentscan
【发布时间】:2020-12-21 09:15:14
【问题描述】:

我在我的小型 Spring Boot 项目中遇到找不到符号组件扫描错误。知道我哪里出错了。

基类:

@ComponentScan("com.example.test.lambda")
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

配置类

@Configuration
@EnableWebMvc
@Profile("lambda")
public class Config {

    
    @Bean
    public HandlerMapping handlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    
    @Bean
    public HandlerAdapter handlerAdapter() {
        return new RequestMappingHandlerAdapter();
    }

    
    @Bean
    public HandlerExceptionResolver handlerExceptionResolver() {
        return new HandlerExceptionResolver() {

            @Override
            public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
                return null;
            }
        };
    }

}

依赖树:

它显示弹簧上下文类已经加载..

+- org.springframework:spring-context:jar:4.3.13.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.3.13.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.3.13.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.3.13.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:4.3.13.RELEASE:compile

错误:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project spring-boot-lambda: Compilation failure
[ERROR] spring-boot-lambda-test-jenkins/lambda-service/spring-boot-lambda/src/main/java/com/example/test/lambda/Application.java:[7,2] cannot find symbol
[ERROR]   symbol: class ComponentScan

【问题讨论】:

  • 很难用代码 sn-ps 说。你导入了吗?
  • 我也发现很难解决这个问题。我实际上从一个开放的 repo 中检查了代码
  • 它可能无法解决依赖关系,您可以更新 Application 类以查看包和导入吗?
  • 你能分享 repo 而不是代码 sn-p 吗?这样我们就可以自己编译项目了。
  • Repo 是最简单的版本..不使用组件扫描

标签: java spring spring-boot aws-lambda


【解决方案1】:

这是一个多余的声明... SpringBootApplication 还为您提供了一个componentScan...如果您真的想使用@ComponentScan,请将您的@SpringBootApplication 更改为更小的东西,例如@Configuration...。

但是这样做你会失去 Springboot 提供的所有自动配置。

Springboot configuration annotation explanation

更新

我已经克隆了你的 repo,它构建得很好,除了你的类配置之外没有任何改变

Maven build succesfully

import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class Application extends SpringBootServletInitializer {

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

【讨论】:

  • 是的,我知道.. 即使我想使用 import.. 它说找不到符号.. 这对我来说很奇怪.. 我用 RestController 注释声明了多个文件,但它只是在上下文中占用一个控制器..无法读取任何其他控制器..
  • 请查看我对我的帖子所做的更新以及考虑事项
  • 也许您必须提交并推送您的更改,以便我可以评估...因为我看到构建错误出现在您的 HEAD 上现在不存在的包中。我的本地代码源中没有这个包 /src/main/java/com/example/test/lambda/Application.java
猜你喜欢
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多