我在我的小型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;
}
};
}
}
依赖树:
它显示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配置注释解释
更新
我已经克隆了你的repo,它构建得很好,除了你的类配置之外,没有任何其他改变
Maven成功构建
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);
}
}