Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 18065

Can't Autowire Repository Interface in Spring Data JDBC

$
0
0

I took my first steps with Spring Data JDBC. I encountered the Problem of using my Repository Interface in my Service Class.But lets take a Look at the Code:

@Table("users")public class User {@Idprivate Long id;private String name;private String email;public Long getId() {    return id;}public void setId(Long id) {    this.id = id;}public String getName() {    return name;}public void setName(String name) {    this.name = name;}public String getEmail() {    return email;}public void setEmail(String email) {    this.email = email;}}

My Repository:

@Repositorypublic interface UserRepository extends CrudRepository<User, Long> {@Query("SELECT * FROM users WHERE email = :email")User findByEmail(String email);}

And my Service Class:

@Servicepublic class UserService {@AutowiredUserRepository userRepository;public String getInfo() {    User user = userRepository.findByEmail("john@example.com");    return user.getName();}}

And that's the Error I get:

Field userRepository in com.example.demo.services.UserService required a bean of type 'com.example.demo.jdbc.UserRepository' that could not be found.

The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.example.demo.jdbc.UserRepository' in your configuration.

I also tried to add an AppConfig as follows:

@Configuration@ComponentScan(basePackages = {"com.example.demo"})@EnableJdbcRepositories("com.example.demo.jdbc.UserRepository")public class AppConfig {}

Maybe somebody got an idea?:)


Viewing all articles
Browse latest Browse all 18065

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>