'Field in com.XXX required a bean of type that could not be found

I'm working on Spring over Hibernate project an i'm only in the beginning. I'm tryng to hav a SpringBootApplication which writes to MsSql some LogEntries objects. I have some different packages: enter image description here

here is the classes:

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> {

}

and I use this class as tester:

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



}

wher i run this as application i get this message in console:

APPLICATION FAILED TO START

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.

I put the @ComponentScan({"com.tradingSystem" }) annotation in the tester as you can see. however, still get this message. (when I didnt use any packages separation, everything works fine...)

Please help me solve this

Thanks



Solution 1:[1]

You should add @Repository annotation above your Repository interface. Optionally you can add it like @Repository(value="logEntryRepository")

Solution 2:[2]

the default scan path is package of @SpringBootApplication class, so you must declare three scan path, but it's seems like that you missing two scan config, you need add

@EnableJpaRepositories(basePackages = "com.tradingSystem.dataAccess")
@EntityScan(basePackages = "com.tradingSystem.entity")
@ComponentScan(basePackages = "com.tradingSystem.dataAccess")

to the TestApplication class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ahmetcetin
Solution 2 smileis2333