0

I have a Java Spring boot project that I'm creating a post request in. The code looks like this:

Main:

@SpringBootApplication
public class Main

{

public static void main(String[] args)
{

    SpringApplication.run(Main.class,args);

}

}

Java bean:

@Data
@Entity
public class Image {

private @Id @GeneratedValue Long id;
private String imageNo;
private String name;

private Image(){}

public Image(String imageNo, String name){
    this.imageNo = imageNo;
    this.name = name;

 }

}

Repository:

public interface ImageRepository extends CrudRepository<Image, Long> {

 }

DatabaseLoader:

@Component
public class DatabaseLoader implements CommandLineRunner {

private final ImageRepository repository;

@Autowired
public DatabaseLoader(ImageRepository repository) {
    this.repository = repository;
}

@Override
public void run(String... strings) throws Exception {
    this.repository.save(new Image("1", "Baggins"));
}
}

However when I run the project I get the following error:

    Description:

Parameter 0 of constructor in com.face.DatabaseLoader required a bean of type 'com.face.ImageRepository' that could not be found.

Action:

Consider defining a bean of type 'com.face.ImageRepository' in your configuration.

Grateful for any help with this! Many thanks,

Nespony
  • 1,253
  • 4
  • 24
  • 42
  • Possible duplicate of [Consider defining a bean of type 'package' in your configuration \[Spring-Boot\]](https://stackoverflow.com/questions/40384056/consider-defining-a-bean-of-type-package-in-your-configuration-spring-boot) – xingbin Mar 18 '18 at 12:22

1 Answers1

0

Your spring container will not scan Image repository, since it's not annotated. To fix this you should annotate the ImageRepository with @Repository annotation as below -

@Repository
public interface ImageRepository extends CrudRepository<Image, Long> {

 }
amyst
  • 616
  • 1
  • 10
  • 22
  • You shouldn't need this if the repository is in the package below the main class. Spring Boot will scan for repository interfaces automatically. – Strelok Mar 19 '18 at 04:51