Java Spring在使用存储库注释时出现NoSuchBeanDefinitionException异常

Java Spring在使用存储库注释时出现NoSuchBeanDefinitionException异常,java,spring,spring-mvc,Java,Spring,Spring Mvc,网上商店 WebServlet org.springframework.web.servlet.DispatcherServlet 上下文配置位置 /WEB-INF/config/servlet-config.xml WebServlet / 将@组件或@存储库保存在ProductRepositoryImpl中。两者都不可能 除此之外,保持camelcase类似于@限定符(“productRepository”) 从控制器上卸下@ComponentScan。它应该在Java配置中 基于Java


网上商店
WebServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/config/servlet-config.xml
WebServlet
/

@组件
@存储库
保存在
ProductRepositoryImpl
中。两者都不可能

除此之外,保持camelcase类似于
@限定符(“productRepository”)

从控制器上卸下
@ComponentScan
。它应该在Java配置中

基于Java的配置示例:

@Configuration
@ComponentScan(basePackages = "com.packt")
public class Config {

}
如果使用基于XML的配置,请执行以下操作:

更新
applicationContext.xml
或任何
servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring Application Context File -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Added support for component scanning -->
    <context:component-scan base-package="com.packt" />
</beans>


如果这不起作用,请检查自定义servlet XML文件或
applicationContext.XML
web.XML
的配置错误。

ProductRepositoryImpl
中保留
@Component
@Repository
。两者都不可能

除此之外,保持camelcase类似于
@限定符(“productRepository”)

从控制器上卸下
@ComponentScan
。它应该在Java配置中

基于Java的配置示例:

@Configuration
@ComponentScan(basePackages = "com.packt")
public class Config {

}
如果使用基于XML的配置,请执行以下操作:

更新
applicationContext.xml
或任何
servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring Application Context File -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Added support for component scanning -->
    <context:component-scan base-package="com.packt" />
</beans>


如果这不起作用,请检查自定义servlet XML文件或
applicationContext.XML
web.XML
的配置错误。

您是否可以共享发生错误的代码,并尝试在代码块中发布代码<代码>类似这样的东西显然是您面临的依赖注入问题。在无法注入依赖项的地方共享代码这是我的代码:这不是代码,这是错误。另外,请共享您的配置classI添加的代码。请查看它。您是否可以共享您的代码,错误发生的位置,并尝试在代码块中发布您的代码<代码>类似这样的东西显然是您面临的依赖注入问题。在无法注入依赖项的地方共享代码这是我的代码:这不是代码,这是错误。另外,分享您的配置classI添加的代码请查看。评论不用于扩展讨论;此对话已结束。评论不用于扩展讨论;这段对话已经结束。