Spring应用Bean扫描异常:无法找到MessageRepository Bean
Spring无法扫描Repository Bean导致启动失败
启动应用时出现如下错误:
*************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.example.redacted.redacted.service.MessageServiceImpl required a bean of type 'com.example.redacted.redacted.repositories.MessageRepository' that could not be found. Action: Consider defining a bean of type 'com.example.redacted.redacted.repositories.MessageRepository' in your configuration. Process finished with exit code 0
相关代码文件
主应用类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class EmmiesprojectApplication extends SpringBootServletInitializer implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(EmmiesprojectApplication.class, args); } }
Controller类
@RestController @RequiredArgsConstructor public class MessageController { private final MessageService messageService; @LogEndoint @GetMapping("/message") public ResponseEntity<Message> getMessage(){ return new ResponseEntity<>(messageService.getMessage(), HttpStatus.OK); } @LogEndoint @GetMapping("/status") public ResponseEntity<String> getStatus(){ return new ResponseEntity<>("Service is good to go",HttpStatus.OK); } }
Service实现类
@Service @RequiredArgsConstructor public class MessageServiceImpl implements MessageService{ private final MessageRepository messageRepository; @Override public Message getMessage() { long id = 1; String message = String.valueOf(messageRepository.findById(id)); return new Message(); //return message.orElse(null); } @Override public void setMessage(String message) { } }
Repository接口
@Repository public interface MessageRepository extends CrudRepository<Message, Long> { }
Message实体类
@RequiredArgsConstructor @AllArgsConstructor @Builder @Data @Entity public class Message { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String message; }
application.properties配置
#Security ------------------------------------------------- security.ignored=/** security.basic.enabled=false management.security.enabled=false #Database ------------------------------------------------- spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto=none spring.jpa.hibernate.show-sql=true spring.datasource.url=jdbc:postgresql://localhost:5432/projects spring.datasource.username=postgres spring.datasource.password= #SQL ------------------------------------------------------- #create sql for getting and setting messages #Config ---------------------------------------------------- spring.web.resources.add-mappings = false
build.gradle配置
plugins { id 'java' id 'org.springframework.boot' version '3.2.4' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.example.redacted' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-batch' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.2.4' implementation 'javax.persistence:javax.persistence-api:2.2' implementation 'jakarta.persistence:jakarta.persistence-api:3.0.0' implementation 'org.hibernate:hibernate-core:7.0.0.Alpha1' //swagger implementation 'io.springfox:springfox-swagger-ui:3.0.0' //spotless implementation 'com.diffplug.spotless:spotless-lib:2.45.0' implementation "com.diffplug.spotless:spotless-plugin-gradle:6.25.0" compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.mysql:mysql-connector-j' runtimeOnly 'org.postgresql:postgresql' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.batch:spring-batch-test' } tasks.named('test') { useJUnitPlatform() }
已尝试的方法
- 使用
@ComponentScan注解指定扫描路径 - 替换
@SpringBootApplication为@Configuration+@EnableAutoConfiguration+@ComponentScan组合注解 - 确保主应用类位于根包,其他类在子包下
解决方案
1. 移除DataSourceAutoConfiguration排除配置
你在@SpringBootApplication中排除了DataSourceAutoConfiguration.class,但项目依赖JPA和PostgreSQL数据源,这个自动配置类是Spring Boot初始化数据源、JPA相关Bean的核心,排除它会导致Spring无法生成Repository代理Bean。
修改主应用类:
@SpringBootApplication public class EmmiesprojectApplication extends SpringBootServletInitializer implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(EmmiesprojectApplication.class, args); } }
2. 清理冲突的JPA依赖
Spring Boot 3.x使用Jakarta EE规范(对应jakarta.persistence包),但你同时引入了旧Java EE规范的javax.persistence:javax.persistence-api:2.2,两者冲突会导致JPA注解无法被正确识别。另外手动引入的org.hibernate:hibernate-core:7.0.0.Alpha1是测试版,和spring-boot-starter-data-jpa自带的Hibernate版本不兼容,需要移除。
修改后的dependencies部分:
dependencies { implementation 'org.springframework.boot:spring-boot-starter-batch' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' //swagger implementation 'io.springfox:springfox-swagger-ui:3.0.0' //spotless implementation 'com.diffplug.spotless:spotless-lib:2.45.0' implementation "com.diffplug.spotless:spotless-plugin-gradle:6.25.0" compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.postgresql:postgresql' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.batch:spring-batch-test' }
3. 确认包扫描路径
主应用类EmmiesprojectApplication所在包com.example.redacted,MessageRepository所在包com.example.redacted.redacted.repositories属于其子包,Spring Boot默认会扫描主类所在包及子包,无需额外配置@ComponentScan。
4. 验证步骤
- 清理构建缓存:执行
./gradlew clean - 重新构建启动:执行
./gradlew bootRun - 先访问
/status确认服务启动,再测试/message接口验证Repository是否正常工作
内容的提问来源于stack exchange,提问作者mrsteel2935
相关产品推荐
相关产品推荐

