1

I am having an issue in which a defined repository is not being correctly interpreted as a bean on server startup. The class with @SpringBootApplication is in a higher directory than the defined repository, so I cannot find why it does not configure.

@SpringBootApplication:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
    System.out.println("http://localhost:8080");
}
}

Repository

package lab14.panoslab.Repositories;

import lab14.panoslab.Models.Account;
import org.apache.catalina.User;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<Account,Long> {
    @NotFound(action = NotFoundAction.IGNORE)
List<User> findByUsername(String username);
}

Error code:

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

Description:

Field userRepository in lab14.panoslab.Controllers.RegisterController required a bean of type 'lab14.panoslab.Repositories.UserRepository' that could not be found.

Action:

Consider defining a bean of type 'lab14.panoslab.Repositories.UserRepository' in your configuration.

Process finished with exit code 1

  • https://stackoverflow.com/questions/29221645/cant-autowire-repository-annotated-interface-in-spring-boot – Tommy Brettschneider Sep 18 '18 at 00:00
  • by looking this error, in this controller class `RegisterController` you autowired `UserRepository` bean, have you declared this bean in your config? or can you post the complete code? – Ryuzaki L Sep 18 '18 at 00:29
  • Just a general comment, for package names, typically they are all lowercase. So this package should be lowercase. `lab14.panoslab.Repositories` – tom Sep 18 '18 at 01:45

2 Answers2

0

Are you sure that your class Account implements the interface User?

And try to remove the annotation @Repository and add annotations @EntityScan and @EnableJpaRepositories in your DemoApplication class:

@SpringBootApplication
@EntityScan({"lab14.panoslab.Models"})    
@EnableJpaRepositories({"lab14.panoslab.Repositories"})
public class DemoApplication {...}

Also, I would advise you to rename all your packages into lowercase and return a value of List<Account>, not List<User>.

Artiow
  • 351
  • 1
  • 9
0

I did face similar issue. What I have done the mistake is, I have placed my controller/repository and other component packages outside the Main Class package. So, Spring boot not able to identify my components,

For Ex: main class package is package com.example.demo;

Controller package like, package com.example.controller;

Repository package like, package com.example.repository;

Below are the two different ways to solve this problem,

Explicitly defining my component packages in @ComponentScan, like @ComponentScan(basePackages="com.example.controller,com.example.repository") with base packages of required components.

Otherwise, You can create Controller/repository packages inside the main package. So, you no need to define @ComponentScan and all. For ex,

main class package is package com.example.demo;

Controller package like, package com.example.demo.controller;

Repository package like, package com.example.demo.repository;

Nallamachu
  • 1,420
  • 18
  • 34