如何一起自动装配服务和存储库

时间:2015-07-27 12:26:09

标签: spring spring-mvc jpa repository autowired

我必须修复以下错误。任何人都可以提供帮助

  

严重:StandardWrapper.Throwable   org.springframework.beans.factory.BeanCreationException:错误   创建名为'searchController'的bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire方法:public void   com.website.dev.controller.SearchController.setRecordingService(com.website.dev.service.RecordingService);   嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   [com.website.dev.service.RecordingService]类型的限定bean   找到依赖:预计至少有1个bean符合条件   autowire候选人这种依赖。依赖注释:{}

@Controller
public class SearchController {

    private RecordingService recordingService;

    @Autowired
    public void setRecordingService(RecordingService recordingService) {
        this.recordingService = recordingService;
    }

    @RequestMapping("/search")
    public String showSearch(){
        return "search";
    }
}

@Service("recordingService")
public interface RecordingService  {

    //methods
}


public class RecordingServiceImpl implements RecordingService  {

    @Autowired
    private RecordingRepository recordingRepository;

    //methods that use recordingRepository
}

public interface RecordingRepository {


}

@Repository
public class RecordingJpaRepository implements RecordingRepository {

    @PersistenceContext
    private EntityManager entityManager;

   //methods that use entityManager
}

服务context.xml中

<context:annotation-config></context:annotation-config>
        <context:component-scan
           base-package="com.website.dev.service">
        </context:component-scan>
</beans>

网站-servlet.xml中

<context:component-scan 
    base-package="com.website.dev.controller"> // searchcontroller is in this package
</context:component-scan>

的web.xml

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

修改

如果我自动跟踪RecordingServiceImpl,我会得到以下内容

  

org.springframework.beans.factory.BeanCreationException:错误   创建名为'recordingServiceImpl'的bean:注入autowired   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private com.website.dev.repository.RecordingRepository   com.website.dev.service.RecordingServiceImpl.recordingRepository;   嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   合格的bean类型   找到依赖项的[com.website.dev.repository.RecordingRepository]:   预计至少有1个豆有资格成为autowire候选人   这种依赖。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

2 个答案:

答案 0 :(得分:1)

我在service-context.xml中添加了以下配置,这解决了我的问题

   <context:annotation-config></context:annotation-config>
   <context:component-scan
        base-package="com.website.dev.service">
    </context:component-scan>
    <context:component-scan 
        base-package="com.website.dev.repository">
    </context:component-scan>
    <context:component-scan 
        base-package="com.website.dev.repository.jpa">
    </context:component-scan>

答案 1 :(得分:0)

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.website.dev.service.RecordingService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

如果我们查看异常,我们会发现spring容器无法创建或实例化com.website.dev.service.RecordingService类型的bean。

这是因为POJO class不由spring container管理。

@Autowire annotation仅适用于由spring管理的对象(即由spring容器创建)。

您应该将RecordingServiceImpl类注释为Service

@Service
public class RecordingServiceImpl implements RecordingService

并从

中删除@Service("recordingService")
public interface RecordingService  {

    //methods
}

RecordingServiceImplSpring container管理,春天可以create the bean