Reputation: 11
Here is my app.groovy code:
package org.test
import javax.persistence.*
import org.springframework.data.jpa.repository.JpaRepository
@Grab('spring-boot-starter-data-jpa')
@Grab('mysql-connector-java')
@Entity
@Table (name="owner")
class Owner {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
Long ownerId;
@Column(name = 'Name')
String name;
@Column(name = 'DateOfBirth')
Date dateOfBirth;
@Column(name = 'Address')
String address;
@Override
String toString() {
return String.format(
"Owner[id=%d, Name='%s', DateOfBirth='%s']",
id, name, dateOfBirth);
}
}
interface OwnerRepository extends JpaRepository<Owner, Long> {
List<Owner> findByName(String name);
}
class Runner implements CommandLineRunner {
@Autowired
private DataSource ds;
@Autowired
private OwnerRepository repository
void run(String... args) {
for (owner in repository.findAll()) {
println owner
}
}
}
application.properties code:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ng4
spring.datasource.username=user_name
spring.datasource.password=********
spring.jpa.show-sql=false
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=validate
After running $ spring run app.groovy, I got following error messages:
Field repository in org.test.Runner required a bean of type 'org.test.OwnerRepository' that could not be found.
Action:
Consider defining a bean of type 'org.test.OwnerRepository' in your configuration.
...
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'runner': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.test.OwnerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I have tried to test inject JdbcTemplate using the same code statement, and it works fine, so I have no idea what's wrong in those code.
Please give me some suggestions, thanks very much !!
Upvotes: 1
Views: 537
Reputation: 1581
Actually the problem is that you also don't have @SpringBootApplication
on top of your Runner
class.
you're missing @Repository
annotation on top of your OwnerRepository
. That might be it.
Upvotes: 1