gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:04:02 26 4
gpt4 key购买 nike

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

enter image description here

这是类(class):

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);
}
}

LogEntryDAO.class:

package com.tradingSystem.dataAccess;

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

public interface LogEntryDAO extends JpaRepository<LogEntry, Long> {

}

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

TestApplication.class:

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);
}



}

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

应用程序无法启动

Description:Field logEntryDao in com.tradingSystem.dataAccess.LogEntryFacadeImpl required a bean of type 'com.tradingSystem.dataAccess.LogEntryDAO' that could not be found.

Action:

Consider defining a bean of type 'com.tradingSystem.dataAccess.LogEntryDAO' in your configuration.

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

请帮我解决这个问题

谢谢

最佳答案

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

关于java - com.XXX 中的字段需要一个无法找到的类型的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44474367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com