Spring使用component-scan时报JDK版本错误,如何解决?
我正在学习Spring,跟着YouTube上的注解教程实践。在spring.xml文件中使用component-scan时,出现如下错误提示:
Context namespace element 'component-scan' and its parser class
[org.springframework.context.annotation.ComponentScanBeanDefinitionParser]
are only available on JDK 1.5 and higher
我的spring.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- <bean id="vehicle" class="com.spring.Bike"></bean> --> <context:component-scan base-package="com.spring.Car"></context:component-scan> </beans>
我使用的Java版本信息:
java 23.0.1 2024-10-15 Java(TM) SE Runtime Environment (build 23.0.1+11-39) Java HotSpot(TM) 64-Bit Server VM (build 23.0.1+11-39, mixed mode, sharing)
编辑器为VSCode,当前无法继续学习,该如何解决?
我的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.spring</groupId> <artifactId>first</artifactId> <version>1.0-SNAPSHOT</version> <name>first</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>23</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>5.3.39</version> <type>pom</type> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC03</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
1. 清理冲突的Spring依赖
pom.xml里同时引入了Spring 5.3.39(pom类型)和Spring 2.5.6.SEC03,版本冲突导致老版本代码被加载,和高版本JDK不兼容。删除2.5.6.SEC03的依赖,换成具体的Spring核心模块(比如spring-context):
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.39</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>
2. 统一Maven编译版本
properties里编译版本设为1.8,但JDK是23,版本不统一导致环境不一致。修改为匹配JDK的设置:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>23</maven.compiler.source> <maven.compiler.target>23</maven.compiler.target> <java.version>23</java.version> </properties>
同时升级maven-compiler-plugin到支持JDK23的版本:
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.1</version> </plugin>
3. 修正component-scan的包路径
context:component-scan的base-package写的是com.spring.Car,如果Car是类而非包,会导致扫描不到组件。确认你的注解类所在包,比如类在com.spring下就改为com.spring,在com.spring.car包下就写对应路径。
4. 重新构建项目
在VSCode终端执行命令,清理旧依赖并重新编译:
mvn clean install
做完以上步骤后,重新运行Spring应用即可解决问题。
内容的提问来源于stack exchange,提问作者Rutwick Gangurde

