com.XXX 中的字段需要一个无法找到的类型的 bean

Nao*_*id2 5 java spring hibernate

我正在研究 Spring over Hibernate 项目,但我才刚刚开始。我正在尝试使用 SpringBootApplication 向 MsSql 写入一些 LogEntries 对象。我有一些不同的包:

在此输入图像描述

这是课程:

LogEntryFacadeImpl.class :

package com.tradingSystem.dataAccess;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tradingSystem.entity.LogEntry;

@Service
public class LogEntryFacadeImpl implements LogEntryFacade{
    @Autowired
    private LogEntryDAO logEntryDao;

    @Transactional
    @Override
    public Long addLogEntry(LogEntry log) {
        return this.logEntryDao.save(log).getId();
    }

    @Override
    public LogEntry getLogEntry(Long logId) {
        return this.logEntryDao.findOne(logId);
    }
}
Run Code Online (Sandbox Code Playgroud)

LogEntryDAO.类:

package com.tradingSystem.dataAccess;

import org.springframework.data.jpa.repository.JpaRepository;
import com.tradingSystem.entity.LogEntry;

public interface LogEntryDAO extends JpaRepository<LogEntry, Long> {

}
Run Code Online (Sandbox Code Playgroud)

我使用这个类作为测试器:

测试应用程序.类:

package com.testings;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.tradingSystem.dataAccess.LogEntryFacade;
import com.tradingSystem.entity.LogEntry;

@SpringBootApplication
@ComponentScan({"com.tradingSystem" })
public class TestApplication implements CommandLineRunner{
    @Autowired
    private LogEntryFacade logEntryFacade;



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

    @Override
    public void run(String... args) throws Exception {
    
        LogEntry log = new LogEntry(552266, "Testing of log entry save", 
            new Date(System.currentTimeMillis()), 
            new Date(System.currentTimeMillis()));
    
        System.err.println(log);
    
        Long id = logEntryFacade.addLogEntry(log);
    
        LogEntry log2 = logEntryFacade.getLogEntry(id);
    
        System.err.println(log2);
    }



}
Run Code Online (Sandbox Code Playgroud)

当我将其作为应用程序运行时,我在控制台中收到此消息:

应用程序无法启动

说明: com.tradingSystem.dataAccess.LogEntryFacadeImpl 中的字段 logEntryDao 需要类型为“com.tradingSystem.dataAccess.LogEntryDAO”的 bean,但无法找到。

行动:

考虑在您的配置中定义“com.tradingSystem.dataAccess.LogEntryDAO”类型的 bean。

我把@ComponentScan({"com.tradingSystem" })注释放在测试器中。但是,仍然收到此消息。(当我没有使用任何包分离时,一切正常......)

请帮我解决这个问题

谢谢

ahm*_*tin 0

您应该在 Repository 接口上方添加 @Repository 注释。您可以选择添加它,例如 @Repository(value="logEntryRepository")