收到错误,构造函数的参数0需要一个bean,但是找到了2个,但其中一个“找到的” bean被我删除了]]

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

我正在尝试使用jpa / jdbc建立h2数据库,在使用jpa而不是jdbc为查询接口创建了一个隐含符号后,我现在得到了错误:

com.nsa.charitystarter.service.CharityQueries中的构造函数的参数0需要一个bean,但是找到了2个:-charityRepoJDBC:定义在文件[C:\ Users \ V La Roche \ Desktop \ assessment-1-starter \ out \ production \ classes \ com \ nsa \ charitystarter \ data \ CharityRepoJDBC.class]中-charityRepoJPA:定义为null

不确定什么地方出了问题,也不确定从这里到哪里去,我还没办法在网上找到很多与我有类似问题的人。

我使用jdbc的实现

@Repository
public class CharityRepoJDBC implements CharityRepository {

    private JdbcTemplate jdbc;
    private RowMapper<Charity> charityMapper;

    @Autowired
    public CharityRepoJDBC(JdbcTemplate aTemplate) {
        jdbc = aTemplate;


        charityMapper = (rs, i) -> new Charity(
                rs.getLong("id"),
                rs.getString("name"),
                rs.getString("registration_id"),
                rs.getString("acronym"),
                rs.getString("purpose")
        );
    }

    @Override
    public List<Charity> findCharityBySearch(String searchTerm) {

        String likeSearch = "%" + searchTerm + "%";

        return jdbc.query(
                "select id, acronym, name, purpose, logo_file_name, registration_id  " +
                        "from charity " +
                        "where concat(name, acronym, purpose, registration_id) like ?",
                new Object[]{likeSearch},
                charityMapper);
    }

    @Override
    public Optional<Charity> findById(Long id) {
        return Optional.of(
                jdbc.queryForObject(
                        "select id, acronym, name, purpose, logo_file_name, registration_id  from charity where id=?",
                        new Object[]{id},
                        charityMapper)
        );
    }
}

慈善发现者实施:

@Service
public class CharityQueries implements CharityFinder {
    private CharityRepository charityRepository;


    public CharityQueries(CharityRepository repo) {
        charityRepository = repo;
    }

    public Optional<Charity> findCharityByIndex(Integer index) {
        return charityRepository.findById(index.longValue());
    }

    public List<Charity> findCharityBySearch(String searchTerm) {

        return charityRepository.findCharityBySearch(searchTerm);
    }
}

CharityFinder界面

public interface CharityFinder {

    public Optional<Charity> findCharityByIndex(Integer index);

    public List<Charity> findCharityBySearch(String searchTerm);

}

错误日志:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.nsa.charitystarter.service.CharityQueries required a single bean, but 2 were found:
    - charityRepoJDBC: defined in file [C:\Users\V La Roche\Desktop\assessment-1-starter\out\production\classes\com\nsa\charitystarter\data\CharityRepoJDBC.class]
    - charityRepoJPA: defined in null


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed


Process finished with exit code 0

我正在尝试使用jpa / jdbc来建立h2数据库,在使用jpa而不是jdbc为查询接口创建了一个障碍之后,我现在收到错误:com.nsa中构造函数的参数0。...

spring-boot spring-data-jpa spring-jdbc spring-data-jdbc
2个回答
0
投票

您目前具有以下定义,


0
投票

您似乎在类路径上有Spring Data JDBC入门,而Spring Data JPA入门有。

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