Spring Boot打包部署报错:UnsatisfiedDependencyException问题排查
问题描述
IntelliJ IDEA中运行Spring Boot项目完全正常,但打包成jar后,用本地同版本JDK(命令:C:\Users\MyUser\.jdks\openjdk-22.0.1\bin\java.exe -jar .\animalshelter.jar)运行或部署到Ubuntu 24.04服务器时,均启动失败,报错找不到EventRepository的Bean。
错误日志
Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: . ____ _ __ _ _ Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: /\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: \/ ___)| |_)| | | | | || (_| | ) ) ) ) Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: ' |____| .__|_| |_|_| |_\__, | / / / / Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: =========|_|==============|___/=/_/_/_/ Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: :: Spring Boot :: (v3.2.5) Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: 20:16:31.924 [main] INFO me.fazelukario.animalshelter.AnimalShelterApplication -- Starting AnimalShelterApplication using Java 22-ea with PID 36353 (/root/AnimalShelter/animalshelter.jar started by root in /root/AnimalShelter) Jun 01 20:16:31 raspberrypi5 AnimalShelter[36353]: 20:16:31.930 [main] INFO me.fazelukario.animalshelter.AnimalShelterApplication -- No active profile set, falling back to 1 default profile: "default" Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: 20:16:32.479 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext -- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eventService' defined in URL [jar:file:/root/AnimalShelter/animalshelter.jar!/me/fazelukario/animalshelter/services/EventService.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'me.fazelukario.animalshelter.repository.EventRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: 20:16:32.487 [main] ERROR org.springframework.boot.SpringApplication -- Application run failed Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eventService' defined in URL [jar:file:/root/AnimalShelter/animalshelter.jar!/me/fazelukario/animalshelter/services/EventService.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'me.fazelukario.animalshelter.repository.EventRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:795) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:237) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1355) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1192) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at me.fazelukario.animalshelter.AnimalShelterApplication.main(AnimalShelterApplication.java:10) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'me.fazelukario.animalshelter.repository.EventRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1880) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1406) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:904) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:782) Jun 01 20:16:32 raspberrypi5 AnimalShelter[36353]: ... 18 common frames omitted
涉及代码片段
EventRepository
@Repository public interface EventRepository extends JpaRepository<Event, String> { @NonNull Optional<Event> findById(@NonNull String id); }
EventService
@Service @Slf4j @Transactional(rollbackOn = Exception.class) @AllArgsConstructor public class EventService { @Autowired private EventRepository eventRepository; }
解决方案
1. 修复依赖注入冲突
EventService同时使用@AllArgsConstructor和@Autowired会导致Spring注入逻辑混乱:@AllArgsConstructor会生成带参数的构造函数,Spring优先用构造函数注入,但构造函数参数无注入注解;同时字段上的@Autowired会被忽略。二选一即可:
- 移除
@AllArgsConstructor,保留字段注入:
@Service @Slf4j @Transactional(rollbackOn = Exception.class) public class EventService { @Autowired private EventRepository eventRepository; }
- 移除
@Autowired,保留构造函数注入(推荐,符合Spring最佳实践):
@Service @Slf4j @Transactional(rollbackOn = Exception.class) @AllArgsConstructor public class EventService { private EventRepository eventRepository; }
2. 确保Spring能扫描到Repository
检查启动类AnimalShelterApplication是否在根包me.fazelukario.animalshelter下(Spring默认扫描启动类所在包及子包),如果不在,添加@EnableJpaRepositories指定Repository所在包:
@SpringBootApplication @EnableJpaRepositories(basePackages = "me.fazelukario.animalshelter.repository") public class AnimalShelterApplication { public static void main(String[] args) { SpringApplication.run(AnimalShelterApplication.class, args); } }
3. 检查打包配置
确保构建工具的Spring Boot插件配置正确,避免遗漏类:
- Maven:确认
spring-boot-maven-plugin版本与Spring Boot版本匹配:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.2.5</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- Gradle:确认已应用
spring-boot插件:
plugins { id 'org.springframework.boot' version '3.2.5' id 'io.spring.dependency-management' version '1.1.4' id 'java' }
4. 清理并重新打包
执行命令清理旧构建文件,重新生成jar包:
- Maven:
mvn clean package
- Gradle:
./gradlew clean bootJar
内容的提问来源于stack exchange,提问作者fazelukario
相关产品推荐
相关产品推荐

