-1

I am working with Spring and Spring Data JPA. When deploying my application in Tomcat I'm getting the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'myController': Injection of autowired dependencies failed; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
 private com.service.MyService com.controller.MyController.myService; nested exception 
 is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'MyService': Injection of autowired dependencies failed; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
 private com.repository.MyRepository com.service.MyService.myRepository; nested exception is 
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
 [com.repository.MyRepository] 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)}

The following are my code:

MyController.java

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.domain.MyEntity;
import com.service.MyService;

@RestController
@RequestMapping(MyController.ROOT_RESOURCE_PATH)
public class MyController{

    public static final String ROOT_RESOURCE_PATH = "/test";

    @Autowired
    private MyService myService;

    @RequestMapping(value="/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<MyEntity> getAll() {
        return myService.getAll();
    }
}

MyService.java

package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.domain.MyEntity;
import com.repository.MyRepository;

@Service(value = "MyService")
public class MyService {

    @Autowired
    private MyRepository myRepository;

    public List<MyEntity> getAll() {
        return myRepository.findAll();
    }
}

MyRepository.java

package com.repository;

import java.util.List;

import org.springframework.data.repository.Repository;

import com.domain.MyEntity;

public interface MyRepository extends Repository<MyEntity, Long> {

    public List<MyEntity> findAll();
}

}

MyApplication-context.xml

<jpa:repositories base-package="com.repository" /> 

<context:component-scan base-package="com.service" />

<context:component-scan base-package="com.controller" />

<context:annotation-config />
Gnik
  • 7,120
  • 20
  • 79
  • 129
  • did you annotated implementation class of `MyRepository` with `@Repository`? – Arpit Aggarwal Jan 10 '17 at 17:50
  • Add the @Repository annotation to your repository and give it a go. Since you're using the old school means of component scanning your repositories, you'll need to identify them to Spring – lane.maxwell Jan 10 '17 at 17:50
  • @Arpit: OP uses Spring Data, which generates the implementation. He cannot annotate the repository. – dur Jan 10 '17 at 17:52
  • 1
    Shouldn`t it be public List findAll(); when Repository of User Type is defined. why List ?? – Barath Jan 10 '17 at 18:02
  • 1
    How is this related to Spring Boot? Also adding `` doesn't add a thing as that is already covered by ``. – M. Deinum Jan 10 '17 at 19:33
  • @dur with Spring Data, the annotation should be on the interface – eis Jan 10 '17 at 21:50
  • Getting error not because of my description in my question. – Gnik Jan 28 '17 at 13:58

1 Answers1

3

I'm not seeing your repository annotated. That might be the reason why Spring couldn't create a bean for MyRepository during component scan. Annotate it with @Repository

Ashish
  • 117
  • 1
  • 13
jAvA
  • 557
  • 1
  • 8
  • 22