当前位置: 首页 > 知识库问答 >
问题:

如何将服务和存储库自动连接在一起

贺亦
2023-03-14

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

严重:StandardWrapper.Throwable org.SpringFramework.Beans.Factory.BeanCreationException:创建名为“Search Controller”的bean时出错:注入autowired依赖项失败;嵌套异常为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:需要至少一个符合此依赖项自动候选条件得bean.依赖项批注:{}

@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:annotation-config></context:annotation-config>
        <context:component-scan
           base-package="com.website.dev.service">
        </context:component-scan>
</beans>
<context:component-scan 
    base-package="com.website.dev.controller"> // searchcontroller is in this package
</context:component-scan>
<context:component-scan 
    base-package="com.enepath.dev.controller">
</context:component-scan>

如果自动调用RecordingServiceImpl,则会得到以下信息

org.springframework.beans.factory.BeanCreationException:创建名为“Recording ServiceImpl”的bean时出错:注入autowired依赖项失败;嵌套异常为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:需要至少一个符合此依赖项自动候选条件得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}

共有1个答案

郑承恩
2023-03-14

我在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>
 类似资料:
  • 当我们在spring mvc中使用crud存储库时,会出现以下错误

  • 我有一个名为UserRepository的通用存储库接口。然后我有一个接口,它从MyUserRepository扩展而来。它处理一个MyUser类,该类扩展了User。 我还有一个名为UserService的服务接口和一个名为MyUserServiceImpl的类。 该服务需要UserRepository的实例,我虽然可以使用某种注释,如@Qualifer,但它不起作用。 应用程序无法启动 说明:

  • 我已经用@Autowired注释为相应的存储库定义了服务类 存储库接口定义为从JpaReepository扩展 应用程序自动编译服务类 运行时,我得到以下错误

  • 我正在为我的应用程序使用模拟存储库 以下是服务的外观片段: 以下是存储库代码: 当我使用执行main()时,它工作正常。 但是,当我想运行测试类: 它会因以下stacktrace而失败: 原因:org。springframework。豆。工厂NoSuchBean定义异常:没有类型为“edu”的合格bean。勒勒亚克。存储库。WeatherStationRepositoryMock’可用:至少需要1

  • 问题内容: 我需要将两个对象注入。其中一个是的实例,我得到这样的信息: 那么如何在我的services.yml中声明呢?这是服务: 问题答案: 我找到了此链接,这对我有用:

  • 2014-12-20 15:35:52错误TestContextManager:334-允许TestExecutionListener[org.springframework.test.context.support.dependencyInjectionTestExecutionListener@5af97850]准备测试实例[com.amsb.bariz.base.test.usertest@