1
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.DAO.PersonDAO com.controller.PersonController.personDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.DAO.PersonDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at com.example.DemoMvcApplication.main(DemoMvcApplication.java:18) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_79]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_79]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_79]

****Here Is My PersonController.java****

@Controller public class PersonController {

@Autowired 
public PersonDAO personDao;

@RequestMapping("/person")
public String getPerson(Model model){
    User p=new User();
    p.setUserName("user");
    p.setPassword("password");

    model.addAttribute("login", p);
    return "personview";
}

@RequestMapping(value="/userLogin",method=RequestMethod.POST)

public String Login(User objUser,ModelMap model){
    if(objUser.getUserName().equals("user")&&objUser.getPassword().equals("password")){
        model.put("userName", objUser.getUserName());
        System.out.println("in@@@@@@@@@");
        return "TestPage";
    }else{
        System.out.println("in@@@@@@@@@");
        model.put("Error", "Failed to Login");
        return "personview";
    }

}

 @RequestMapping("/create")
  @ResponseBody
  public String create(String firstName, String lastName,int age) {
    String userId = "";
    try {
      Person user = new Person(firstName,lastName,age);
// personDao.save(user); userId = String.valueOf(user.getId()); } catch (Exception ex) { return "Error creating the user: " + ex.toString(); } return "User succesfully created with id = " + userId; } }

**Here is My personDAO.java**
@Repository //@Qualifier("PersonDAO") @Component //@EnableAutoConfiguration public interface PersonDAO extends CrudRepository {

} 

Here is my Model

 @Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy=GenerationType.AUTO) Integer id; String firstName; String lastName; int age;

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public Person(String firstName, String lastName, int age) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
}
}

Here is my application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.username=root spring.datasource.password=mysql 
spring.datasource.testWhileIdle = true
 spring.datasource.validationQuery = SELECT 1 spring.jpa.show-sql = true
 spring.jpa.hibernate.ddl-auto = update 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Shafeeque
  • 2,039
  • 2
  • 13
  • 28
Vaibhav
  • 485
  • 6
  • 12
  • 1
    com.DAO.PersonDAO is not defined as a bean – Matias Elorriaga May 27 '16 at 12:50
  • how can i define as bean can you please give me a sample code – Vaibhav May 28 '16 at 06:54
  • how can i define as bean can you please give me a sample code i am using spring version 3.5 and i cant do with autowired – Vaibhav May 28 '16 at 07:18
  • Please remove excess comment and fix the formatting, it is pain to eyes to read. And this question is bit too long to read, you can probably make it shorter. Ex, full entity content is hardly to be relevant, etc. – no id May 31 '16 at 12:45

2 Answers2

0

personDAO should be declared as a spring bean.either you can use @component or @service annotations.Otherwise you can done this using xml config

Ajay
  • 87
  • 7
0

Make sure you have '@EnableJpaRepositories' at your configuration class

no id
  • 1,642
  • 3
  • 24
  • 36