Spring boot repository is not creating under groovy.
environment:
java openjdk 17 2021-09-14
groovy 4.0.14
spring boot 2.7.18
Code:
@SpringBootApplication
class Main {
@Entity
static class User {
@Id
public String id
public String name
}
@Repository
interface UserRepository extends JpaRepository<User, String> {
}
@RestController
static class Contr {
public final UserRepository userRepository
Contr(@Autowired UserRepository userRepository) {
this.userRepository = userRepository
}
@GetMapping('/')
Object home() {
userRepository.save(new User(id: '1', name: 'Alice'))
userRepository.save(new User(id: '2', name: 'Bob'))
return userRepository.findAll()
}
}
static void main(String[] args) {
SpringApplication.run Main, args
}
}
properties:
server:
port: 8080
spring:
datasource:
url: "jdbc:sqlite::memory:"
username:
password:
driver-class-name: org.sqlite.JDBC
hikari:
maximum-pool-size: 1
max-lifetime: 0
jpa:
database-platform: org.sqlite.hibernate.dialect.SQLiteDialect
show-sql: false
properties:
hibernate:
transaction:
jta:
platform: org.springframework.boot.orm.jpa.hibernate.SpringJtaPlatform
jdbc:
time_zone: UTC
open-in-view: false
error:
No qualifying bean of type 'org.example.Main$UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
sample project: untitled.zip
the same code with the same dependencies version works fine written in java
Comment From: msangel
Converted to java got the same error. in fact the error is: repository is internal class. and in move it to own class this working
Comment From: msangel
adding @EnableJpaRepositories(considerNestedRepositories = true) resolved issue