春季生成了同一实例的两个豆子“ myrepository”

发布于 2025-02-08 18:12:42 字数 2204 浏览 4 评论 0 原文

我非常“新”弹簧启动,需要在Crudreposories方面提供一些帮助。

我的问题是以下内容:当我想启动春季启动应用程序时,它就可以很好,可以在数据库中创建所有表,但是以某种方式设法失败,因为春季创建了两个相同界面的bean:

请注意,我的软件包层次结构是我的软件包层次结构看起来有些不同。但是,Spring Boot引用了完全相同的接口,两次。

The bean 'IMyRepository', defined in com.package.IMyRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in com.package.IMyRepository defined in @EnableJdbcRepositories declared on JdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration and overriding is disabled.

为了澄清:我没有两个接口称为机智的同名我没有任何myrepositoryimpl class ,我没有多个具有相同的项目 :src/main /.../ com。

(只有一个

package com.package.api.components.account.repository;

import com.package.api.components.account.entity.Account;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface IMyRepository extends CrudRepository<Account, Long> {

    Account findByEmail(String email);

    Account findByEmailAndPassword(String email, String password);
}

层次结构。 在此课程中,我正在使用Imyrepository:

@Component
@AllArgsConstructor
public class AccountService implements IAccountService, IRegisterService, ILoginService {

    private final IAccountRepository accountRepository;

    private final IPasswordValidationService passwordValidationService;
    private final AAccountMapper accountMapper;

    // Code
}

这是stackoverflow上的一些答案&amp; CO我已经尝试过,它们对我有用:

非常感谢您阅读本文!

i'm fairly 'new' to Spring Boot and need a bit of help with CrudRepositories.

My problem is the following: When I want to start my Spring Boot Application, it starts just fine, creates all the tables in the database, but somehow manages to fail, because Spring creates two beans of the same interface:

Note that my package hierarchy looks a bit different. Spring Boot however is referencing the exact same interface, twice.

The bean 'IMyRepository', defined in com.package.IMyRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration, could not be registered. A bean with that name has already been defined in com.package.IMyRepository defined in @EnableJdbcRepositories declared on JdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration and overriding is disabled.

For clarification: I do not have two interfaces declared wit the same name, I do not have any MyRepositoryImpl classes, and I do not have multiple projects with the same hierarchy. (only one: src/main/.../com.(...).IMyRepository)

My Repository interface looks like this:

package com.package.api.components.account.repository;

import com.package.api.components.account.entity.Account;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface IMyRepository extends CrudRepository<Account, Long> {

    Account findByEmail(String email);

    Account findByEmailAndPassword(String email, String password);
}

And the only single time where, I am taking usage of IMyRepository is in this class:

@Component
@AllArgsConstructor
public class AccountService implements IAccountService, IRegisterService, ILoginService {

    private final IAccountRepository accountRepository;

    private final IPasswordValidationService passwordValidationService;
    private final AAccountMapper accountMapper;

    // Code
}

Here are some answers on StackOverflow & co I've already tried, which none off them worked for me:

Thanks you very much for reading this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小瓶盖 2025-02-15 18:12:42

不要将@repository(或@component)放在接口上,而仅在实现上。

Don't put @Repository (or @Component) on the interface, only on the implementation.

柳若烟 2025-02-15 18:12:42

问题是,(就我而言)我有两个 spring-boot-starter-data -... 依赖项,这两个都想创建同一类的bean /接口,具有相同的IMPN名称。

我通过按照他们的说明解决了问题:

  1. 我删除了依赖项 spring-boot-starter-data-jdbc
  2. 我还从项目中删除了库,classpath又有

另一个问题是,我的项目不会继续运行。我再次通过按照他们的说明解决了这个问题:

  1. 我添加了 spring-boot-starter-web 依赖关系

As stated in the comments of @M. Deinum, the problem was, that (in my case) I had two spring-boot-starter-data-... dependencies, both of which wanted to create beans of the same class/interface, with the same Impl name.

I solved the problem, by following their instructions:

  1. I deleted the dependency spring-boot-starter-data-jdbc
  2. I also deleted the library from the project and classpath

Another problem was, that my project wouldn't keep running. I, again, solved this problem by following their instructions:

  1. I added the spring-boot-starter-web dependency
悲念泪 2025-02-15 18:12:42

我正面临同样的问题,我能够在这里找到的帮助来解决它。但是在熟悉的情况下,我想出了其他东西。

在实体类中,我最近添加了我的项目,这导致了我错误地使用 @Table 的问题。我很想导入类 org.springframework.data.relations.core.core.mappe.table 而不是 jakarta.persistence.table.table

因此,当我更改为Corret注释(雅加达)时,即使将两个libs都放在我的pom.xml(spring-boot-starter-starter-starter-data-jpa和spring-boot-stoot-starter-starter-starter-data-jdbc)中,它也可以。

因此,请查看您是否没有使用错误的河口。

I was facing the very same issue and I was able to solve it with the help I found here. But aditionally I figured out something else.

In an entity class I added recently to my project, which caused the issue I was mistakenly using @Table. I happend to import class org.springframework.data.relational.core.mapping.Table instead of jakarta.persistence.Table.

So, when I changed to the corret annotation (jakarta´s) it worked fine even keeping both libs in my pom.xml (spring-boot-starter-data-jpa and spring-boot-starter-data-jdbc).

So check out if you are not using wrong annontations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文