首页 文章

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

提问于
浏览
0

我必须修复以下错误 . 有人可以帮忙

SEVERE:StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException:创建名为'searchController'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配方法: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
}

service-context.xml

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

website-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>

EDIT

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

org.springframework.beans.factory.BeanCreationException:创建名为'recordingServiceImpl'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.website.dev.repository.RecordingRepository com.website.dev.service.RecordingServiceImpl.recordingRepository;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[com.website.dev.repository.RecordingRepository]的限定bean用于依赖:预期至少有1个bean有资格作为此依赖项的autowire候选者 . 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

2 回答

  • 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>
    
  • 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
    }
    

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

相关问题