Java 属性文件更改后,Maven无法生成spring boot应用程序(无法解析配置类)
我正在使用Spring Boot构建一个小型web应用程序。在尝试运行应用程序后,我得到一个错误。 它工作得很好,但后来我更改了属性文件中的一些值,现在Maven无法构建应用程序。它无法使用Intelij IDEA和命令行运行它。我在这里搜索了很多,但是找不到任何对我的案子有用的东西 以下是堆栈跟踪:`Java 属性文件更改后,Maven无法生成spring boot应用程序(无法解析配置类),java,spring,maven,spring-boot,Java,Spring,Maven,Spring Boot,我正在使用Spring Boot构建一个小型web应用程序。在尝试运行应用程序后,我得到一个错误。 它工作得很好,但后来我更改了属性文件中的一些值,现在Maven无法构建应用程序。它无法使用Intelij IDEA和命令行运行它。我在这里搜索了很多,但是找不到任何对我的案子有用的东西 以下是堆栈跟踪:` 2019-01-27 18:10:57.773 INFO 2144 --- [ main] c.f.FriendlyFireChessApplication
2019-01-27 18:10:57.773 INFO 2144 --- [ main] c.f.FriendlyFireChessApplication : Starting FriendlyFireChessApplication on DESKTOP-PC4K83Q with PID 2144 (C:\Users\Svidic\IdeaProjects\friendly-fire-chess\target\classes started by Svidic in C:\Users\Svidic\IdeaProjects\friendly-fire-chess)
2019-01-27 18:10:57.781 INFO 2144 --- [ main] c.f.FriendlyFireChessApplication : No active profile set, falling back to default profiles: default
2019-01-27 18:10:57.922 INFO 2144 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@21507a04: startup date [Sun Jan 27 18:10:57 EET 2019]; root of context hierarchy
2019-01-27 18:10:58.515 WARN 2144 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.ffirechess.FriendlyFireChessApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer
2019-01-27 18:10:58.536 ERROR 2144 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.ffirechess.FriendlyFireChessApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at com.ffirechess.FriendlyFireChessApplication.main(FriendlyFireChessApplication.java:22) [classes/:na]
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:169) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:393) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:318) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
... 12 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_144]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_144]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_144]
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:158) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
... 17 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_144]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_144]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_144]
... 21 common frames omitted
Process finished with exit code 1
`
pom.xml:`
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ffc</groupId>
<artifactId>friendly-fire-chess</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>friendly-fire-chess</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
`
更新:
将整个项目从本地历史备份到最后一个正确的州解决了这个问题。但我仍然不知道是什么导致了这个应用程序的失败
更新2:
好的,我知道了。它失败了,因为我的本地MySQL实例中没有与我在application.properties中提供的值相对应的用户。一旦我创建了这样的用户,一切都很好
更新3:
真正的原因不在MySQL中。代码和依赖项也是正确的。问题出现在我的运行配置中。我通过在运行配置的命令行参数中添加springboot:run来修复它。
希望它能帮助别人。问题是你有一个提供的依赖关系
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
***<scope>provided</scope>*** <!--to be removed-->
</dependency>
它与spring boot starter web一起提供
除非您正在使用tomcat,否则不需要提供它,在这种情况下,您应该将它从SpringBootStarterWeb中排除
-更新
如果您使用的是tomcat,那么在这种情况下,您需要排除SpringBootStarterWeb中打包的tomcat依赖项,如下所示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--add this exclusion to your pom -->
<exclusions>
**<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>**
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
问题是您有一个提供的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
***<scope>provided</scope>*** <!--to be removed-->
</dependency>
它与spring boot starter web一起提供
除非您正在使用tomcat,否则不需要提供它,在这种情况下,您应该将它从SpringBootStarterWeb中排除
-更新
如果您使用的是tomcat,那么在这种情况下,您需要排除SpringBootStarterWeb中打包的tomcat依赖项,如下所示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--add this exclusion to your pom -->
<exclusions>
**<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>**
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
原因:java.lang.ClassNotFoundException:javax.servlet.ServletContext在您的类路径中似乎缺失,请尝试mvn clean Install我已在下面发布了anwser原因:java.lang.ClassNotFoundException:javax.servlet.ServletContext在您的类路径中似乎缺失,请尝试mvn clean Install我已在下面发布了anwser,我用的是tomcat-9。不确定您应该将其从SpringBootStarterWeb中排除是什么意思。我应该从这里排除什么:org.springframework.boot-springbootstarterweb不工作。正如我提到的,在我更改application.properties中的spring.datasource.username和spring.datasource.password之前,一切都运行良好。我没有更改代码或pom.xml。因此,I gues问题在application.properties中的某个位置。堆栈跟踪与配置问题无关,而是与生成问题有关,请检查源代码管理或本地历史记录以确定更改。今天进行的上一次成功生成后的唯一更改是application.properties中前两行的值。在那之后,出现了一些问题。在从本地历史备份了整个项目之后,它又开始工作了@CodeIsLife谢谢你的帮助!是的,我用的是tomcat-9。不确定您应该将其从SpringBootStarterWeb中排除是什么意思。我应该从这里排除什么:org.springframework.boot-springbootstarterweb不工作。正如我提到的,在我更改application.properties中的spring.datasource.username和spring.datasource.password之前,一切都运行良好。我没有更改代码或pom.xml。因此,I gues问题在application.properties中的某个位置。堆栈跟踪与配置问题无关,而是与生成问题有关,请检查源代码管理或本地历史记录以确定更改。今天进行的上一次成功生成后的唯一更改是application.properties中前两行的值。在那之后,出现了一些问题。在从本地历史备份了整个项目之后,它又开始工作了@CodeIsLife谢谢你的帮助!