微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我会收到“无法自动装配找不到‘PersonService’类型的 bean”-错误?

如何解决为什么我会收到“无法自动装配找不到‘PersonService’类型的 bean”-错误?

我正在使用 Spring Boot 并且正在尝试创建 REST api,但无法解决错误: “无法自动装配。找不到‘PersonService’类型的bean。”

所以在我的项目中,我有以下结构:

▼ com.example
  ▼ person
    ▼ controller
        PersonController (class)
    ► dao
    ► datasouurce
    ► model
    ▼ service
        PersonService (class)
  ► car
  Carapplication (Spring Boot runnable class)

我有一个只有“car”文件夹的项目,Carapplication.class 位于“car”文件夹内,一切正常。现在我将 Person 文件添加到项目中并将 Carapplication.class 上移一级,因为现在它必须同时处理“汽车”和“人”。但是因为这样做,我在 PersonController.class 中得到了一个错误

这是我的 PersonService.class:

package com.example.person.service;

import ...

public class PersonService {

    private final PersonDao personDao;

    public PersonService(@Qualifier("postgres") PersonDao personDao) {
         this.personDao = personDao;
    }

    public List<Person> getAllPersons() {
        return personDao.selectAllPersons();
    }

    public Optional<Person> getPersonById(UUID id) {
        return personDao.selectPersonById(id);
    }
}

这是 Carapplication.class:

package com.example;

import ...

@SpringBootApplication
@EnableScheduling
@EnableAdminServer
public class Carapplication {

    public static void main(String[] args) {
        SpringApplication.run(Carapplication.class,args);
    }
}

最后但并非最不重要的是,这是我的 PersonController.class 发生错误的地方:

package com.example.person.controller;

import ...

@RequestMapping("api/person")
@RestController
public class PersonController {

    private final PersonService personService;

    @Autowired
    public PersonController(PersonService **personService**) {
        this.personService = personService;
    }

    @GetMapping
    public List<Person> getAllPersons() {
        return personService.getAllPersons();
    }

    @GetMapping(path = "{id}")
    public Person getPersonById(@PathVariable("id") UUID id) {
        return personService.getPersonById(id)
                .orElse(null);
    }
}

我在 PersonController.class 中用“**”包围了引发错误的变量。

这是我来自 application.properties 的数据源属性

spring.datasource.url=jdbc:postgresql://localhost:5432/persondb
spring.datasource.username=postgres
spring.datasource.password=password

我曾尝试使用@ComponentScan 来指定应该扫描哪些地图以获取bean,但它没有解决。此外,它应该已经扫描了正确的文件,因为 Carapplication.class 已经上移了。如果需要更多信息,请告诉我。

更新: 我在 PersonService.class 之上添加了 @Service,它实际上删除了 PersonController.class 中 eventService 变量的红色下划线。但是当我运行 Carapplication 时它仍然说:

文件 [C:...\com\capasystems\person\service\PersonService.class] 中创建名为 'eventService' 的 bean 时出错:通过构造函数参数 0 表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:在文件 [C:...\com\capasystems\person\dao\PersonDataAccessService.class] 中定义名称为“postgres”的 bean 创建时出错:通过构造函数参数表示的不满足的依赖关系0;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class] 中定义的名称为“flywayInitializer”的 bean 创建时出错:初始化方法调用失败;嵌套异常是 java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required

解决方法

你需要在你的个人服务类之上使用@Service

package com.example.person.service;

import ...

@Service
public class PersonService {

    private final PersonDao personDao;

    public PersonService(@Qualifier("postgres") PersonDao personDao) {
         this.personDao = personDao;
    }

    public List<Person> getAllPersons() {
        return personDao.selectAllPersons();
    }

    public Optional<Person> getPersonById(UUID id) {
        return personDao.selectPersonById(id);
    }
}
,

您是否尝试使用 @ComponentScan

package aaa.bbb.ccc;

@SpringBootApplication
@EnableScheduling
@EnableAdminServer
@ComponentScan({ "aaa.bbb.ccc.*" })
public class Application {}

确保您对 @Repository@Service 进行了适当的注释,并确保您的所有包都在 aaa.bbb.ccc. 下 在大多数情况下,此设置可以解决此类问题。

告诉我结果。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。