Reputation: 77
just started learning Spring.
Trying to learn JPA by creating a person class, PersonRepository
, PersonDAO
and main
.
I've already looked on StackOverflow for answers but I think I did everything all right...
This is the exception:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'beans.PersonDAO' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) at com.example.SrpingDataJPA.main.main(main.java:15)
This is my classes:
Person.class:
package beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="workers")
public class Person {
@Id
@Column(name="name")
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
PersonRepo.class:
package beans;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepo extends CrudRepository<Person, String>{
List<Person> findByName(String name);
}
PersonDAO.class:
package beans;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class PersonDAO {
@Autowired
private PersonRepo personRepo;
public PersonDAO() {
}
public void createPerson(Person person){
personRepo.save(person);
}
public List<Person> find(String name){
return personRepo.findByName(name);
}
}
main.class:
package com.example.SrpingDataJPA;
import javax.transaction.Transactional;
import org.jboss.jandex.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import beans.PersonDAO;
@Transactional
public class main {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Main.class, args);
PersonDAO personDAO = context.getBean(PersonDAO.class);
System.out.println(personDAO .find("daniel"));
}
}
Any help guys?
Upvotes: 0
Views: 4714
Reputation: 3814
I think there are more things that you need. After making all the changes you were advised, your class should look something like this. Also, scan the packages if your main class is in some other package (for some reason). And enable JPA repositories.
@SpringBootApplication(scanBasePackages = "${com.example.*}")
@EnableJpaRepositories("your.repository.package")
public class Main {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Main.class, args);
PersonDAO personDAO = context.getBean(PersonDAO.class);
System.out.println(personDAO .find("daniel"));
}
}
Don't autowire a repository to a repository. It just feels wrong. Do a @Component, and it will get picked by spring boot as well:
@Component
public class PersonDAO {
@Autowired
private PersonRepo personRepo;
// your methods
}
Also, if you are using Spring Boot, you can get your beans simply like this:
@Autowired
private PersonDAO personDAO;
// your code here
@SpringBootApplication annotation is like having @ComponentScan, @Configuration and @EnableAutoConfiguration. Reference here
Upvotes: 0
Reputation: 653
PersonDAO is not getting scanned here, please add @ComponentScan
with beans
package in your main class and also you forgot to add @SpringBootApplication
annotation to main class.
@ComponentScan("beans")
Upvotes: 1
Reputation: 6808
Put @SpringBootApplication annotation in your main class, it will work.
Upvotes: 0