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

How to autowire a service and a repository together

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

SEVERE: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.website.dev.controller.SearchController.setRecordingService(com.website.dev.service.RecordingService); nested exception is 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: {}

@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: Error creating bean with name 'recordingServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.website.dev.repository.RecordingRepository com.website.dev.service.RecordingServiceImpl.recordingRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.website.dev.repository.RecordingRepository] 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)}

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 class 注释为服务

@Service
public class RecordingServiceImpl implements RecordingService

并从

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

    //methods
}

RecordingServiceImpl 将由 Spring container 管理,spring 将能够 create the bean.

我在 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>