java - 不能@Autowired接口(interface)

标签 java spring autowired

我正在尝试 Autowiring Controller bean 内的接口(interface)

在我放置的上下文配置文件中

<context:annotation-config />

<bean id="viewVerbale" class="com.sirfe.controller.VerbaliController" />

我的 Controller 类是

@Controller
public class VerbaliController {


    @Autowired
    VerbaliRepository repository;

    private static final Logger logger = LoggerFactory.getLogger(VerbaliController.class);


    @RequestMapping(value = "/sirfe/verbale/{sequVerbale:.+}", method = RequestMethod.GET)
    public ModelAndView viewVerbale(@PathVariable("sequVerbale") String sequVerbale) {

        logger.debug("welcome() - sequVerbale {}", sequVerbale);

        Verbali verbale = repository.findOne(Long.parseLong(sequVerbale));


        ModelAndView model = new ModelAndView();
        model.setViewName("sirfe/verbali/viewVerbale");
        model.addObject("sequVerbale", sequVerbale);

        return model;

    }

}

我的界面签名是

public interface VerbaliRepository extends CrudRepository<Verbali, Long>  { }

当我启动我的应用程序时,我得到

 Could not autowire field: com.sirfe.repository.VerbaliRepository com.sirfe.controller.VerbaliController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sirfe.repository.VerbaliRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 

最佳答案

看起来您正在尝试使用 Spring JPA 存储库。

为了让 Spring 为您的存储库接口(interface)创建 bean,您需要在 applicationContext.xml 中声明要扫描的包

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

这样做,Spring将为您生成实现该接口(interface)的bean。

参见Spring JPA Repositories

关于java - 不能@Autowired接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142679/

相关文章:

java - Spring 多个 : crash early 类型的 bean

java - 更改特定项目的微调器背景颜色

java - 评估异常: The method 'start' is not supported by this command processor

java - 如何使用 xpathexpression 获取 <tr style ="">

spring - 使用 Spring 运行 JUnit 测试时为空服务

java - 基于命令行参数注入(inject)实现

Java 枚举 : Gathering info from another enums

java - Spring Scheduler - 当存在循环依赖时,计划的方法不会在事务中启动

java - 移动<mvc :annotation-driven/>: no declaration can be found for element 'mvc:annotation-driven' 后出错

java - 如何将速度模板中的数据绑定(bind)到 Spring Controller ?