Spring 应用程序未正确扫描 Bean

问题描述 投票:0回答:1

我正在为一个副项目创建一个小型 spring 应用程序来练习 spring,并且不断遇到同样的问题:我认为它没有正确扫描我的组件/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);
    }

}

我的控制器:

@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
@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
public interface MessageRepository extends CrudRepository<Message, Long> {
}

我的消息模型对象:

@RequiredArgsConstructor
@AllArgsConstructor
@Builder
@Data
@Entity
public class Message {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String message;
}

我的应用程序.属性:

#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 注释、使用 @springapplication 弃用的 3 个注释,确保根文件夹中的应用程序以及根文件夹内的子文件夹中的其他所有内容的打包正确。我相当有信心我已经正确配置了所有类,因为这几乎不是我第一次创建 Spring 应用程序,但我只是看不出这个有什么问题。我确信 spring 没有正确初始化 bean。

spring-boot spring-mvc spring-data-jpa spring-data
1个回答
0
投票

@RequiredArgsConstructor
中删除
MessageServiceImpl

@Service
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) {

    }
}

更重要的是:从你的bean中删除所有

@RequiredArgsConstructor
注释(例如MessageController),除非你完全理解依赖注入是如何工作的,什么是参数构造函数和非参数构造函数。

© www.soinside.com 2019 - 2024. All rights reserved.