简体   繁体   English

Spring 找不到要自动装配的服务或存储库 bean

[英]Spring can't find Service or Repository beans to Autowire

I'm trying to run spring boot with spring data as basically as possible with swing.我正在尝试使用swing尽可能基本地使用spring数据运行spring boot。

However, even though all seems to be properly configured, when I try to run it, I get an error message saying it couldn't find my Service bean.但是,即使所有似乎都已正确配置,但当我尝试运行它时,我收到一条错误消息,指出它找不到我的 Service bean。

package db.westworld.dao;

import db.westworld.entities.RobotEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RobotRepository extends CrudRepository<RobotEntity, Integer> {
}
package db.westworld.service;

import db.westworld.entities.RobotEntity;

import java.util.Optional;

public interface IRobotService {
    Optional<RobotEntity> findById(int id);
}
package db.westworld.service;

import db.westworld.dao.RobotRepository;
import db.westworld.entities.RobotEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class RobotService implements IRobotService {

    private final RobotRepository robotRepository;

    @Autowired
    RobotService(RobotRepository robotRepository) {
        this.robotRepository = robotRepository;
    }

    @Override
    public Optional<RobotEntity> findById(int id) {
        return robotRepository.findById(id);
    }

    public void saveRobot(RobotEntity robot) {
        robotRepository.save(robot);
    }
}
package db.westworld;

import db.westworld.view.RegisterRobot;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.awt.*;

@SpringBootApplication
public class WestworldApplication {

    public static void main(String[] args) {
        var ctx = new SpringApplicationBuilder(RegisterRobot.class).headless(false).run(args);
        EventQueue.invokeLater(() -> {
            var ex = ctx.getBean(RegisterRobot.class);
            ex.setVisible(true);
        });
    }
}
package db.westworld.view;

import db.westworld.entities.RobotEntity;
import db.westworld.service.RobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.swing.*;
import java.awt.event.*;
import java.util.Date;

@Controller
public class RegisterRobot extends JDialog {
private RobotService robotService;

    @Autowired
    public void setRobotService (RobotService robotService) {
        this.robotService = robotService;
    }
    private void onOK() {
        RobotEntity robot = new RobotEntity();
        robot.setCreatedAt(new Date());
        robot.setId(1);
        robotService.saveRobot(robot);
        dispose();
    }
}

Error message:错误信息:

Parameter 0 of method setRobotService in db.westworld.view.RegisterRobot required a bean of type 'db.westworld.service.RobotService' that could not be found.


Action:

Consider defining a bean of type 'db.westworld.service.RobotService' in your configuration.

(the JDialog implementation just includes the basics) (JDialog 实现只包括基础知识)

The same also happens when I try to autowire the repository.当我尝试自动装配存储库时也会发生同样的情况。 Also, in case needed, here's my pom.xml另外,如果需要,这是我的 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 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>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>    <groupId>db</groupId>
    <artifactId>westworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>westworld</name>
    <description>westworldSpringBoot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

new SpringApplicationBuilder()的参数必须是带有@SpringBootApplication注释的类,如我见过的每个 Spring Boot 示例中所示,例如“入门 - 使用 Spring Boot 构建应用程序”指南中的创建应用程序类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM