gpt4 book ai didi

java - spring boot编译找不到符号componentscan

转载 作者:行者123 更新时间:2023-12-05 07:01:57 24 4
gpt4 key购买 nike

我在我的小型 spring boot 项目中遇到 connot find symbol component 扫描错误。知道我哪里出错了。

基类:

@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;
}
};
}

}

依赖树:

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

+- 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

最佳答案

这是一个多余的声明...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);
}
}

关于java - spring boot编译找不到符号componentscan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63701511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com