Spring Boot生成项目中System.out.println输出两次问题咨询
问题:Spring Boot项目main方法日志输出两次,自定义类未被调用
我通过start.spring.io生成了一个Spring Boot项目,运行时主类main方法中的System.out.println("Demo")语句会输出两次,且创建的其他类未被调用,特此咨询原因及解决办法。
POM配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example.demo</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-groovy-templates</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-docker-compose</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter-test</artifactId> <version>3.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.3</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
主类代码
package com.example.demo.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.example.demo") // Add the package of your component public class DemoApplication { public static void main(String[] args) { System.out.println("Demo"); SpringApplication.run(DemoApplication.class, args); } }
相关截图


原因分析及解决办法
1. 日志输出两次的核心原因
问题出在spring-boot-devtools依赖的重启类加载器机制:DevTools为实现热部署,会使用两个类加载器——一个加载Spring核心库等不变的基础类,另一个加载应用代码的重启类加载器。启动过程中,由于DevTools的初始化逻辑,main方法会被执行两次。
2. 解决日志重复输出的方案
- 禁用DevTools重启功能:在
application.properties中添加配置:spring.devtools.restart.enabled=false - 移除DevTools依赖:如果不需要热部署,直接删除POM中的
spring-boot-devtools依赖项 - 调整启动方式:使用Maven命令
mvn spring-boot:run启动项目,避免IDE对DevTools的兼容性问题导致重复执行
3. 自定义类未被调用的解决要点
- 自定义类必须添加Spring组件注解(如
@Component、@Service、@Repository等),否则无法被Spring容器扫描管理 - 确认自定义类的包路径在
com.example.demo或其子包下,与@ComponentScan指定范围匹配 - 若需在启动时执行自定义逻辑,可实现
ApplicationRunner/CommandLineRunner接口,或在类方法上添加@PostConstruct注解
内容的提问来源于stack exchange,提问作者guradio
相关产品推荐
相关产品推荐

