简体   繁体   English

使用 Repository 注释时 Spring NoSuchBeanDefinitionException

[英]Spring NoSuchBeanDefinitionException while using Repository annotation

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>WebStore</display-name> <servlet> <servlet-name>WebServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/servlet-config.xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>WebServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
 <?xml version="1.0" encoding="UTF-8"?> <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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation-driven> <context:component-scan base-package="com.packt"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> </beans>

I am new to spring, and I getting errors while adding @Repository annotation我是 spring 的新手,在添加@Repository注释时出错

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.packt.webstore.domain.repository.ProductRepository' available: expected at least 1 bean which qualifies as autowire candidate. 
    Dependency annotation {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="ProductRepository")}

The code is given below :代码如下:

package com.packt.webstore.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.packt.webstore.domain.repository.ProductRepository;

@Controller
@ComponentScan(basePackages= {"com.packt.webstore.domain.repository.impl","com.packt.webstore.domain.repository"})
public class ProductController {

    @Autowired
    @Qualifier("ProductRepository")
    private ProductRepository productRepository;


    @RequestMapping("/products")
    public String list(Model model) {
        model.addAttribute("products",productRepository.getAllProducts());
        return "products";
    }
}



package com.packt.webstore.domain.repository.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.packt.webstore.domain.Product;
import com.packt.webstore.domain.repository.ProductRepository;

@Component
@Repository
public class ProductRepositoryImpl implements ProductRepository {


    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private void setDataSource(DataSource dataSource) {
        this.jdbcTemplate=new NamedParameterJdbcTemplate(dataSource);
    }


    @Override
    public List<Product> getAllProducts() {
        Map<String, Object>params=new HashMap<String,Object>();
        List<Product>result=jdbcTemplate.query("SELECT * FROM PRODUCTS", params, new ProductMapper());
        return result;
    }

    private static final class ProductMapper implements org.springframework.jdbc.core.RowMapper<Product> {
        public Product mapRow(ResultSet rs,int rownum) throws SQLException{
            Product product=new Product();
            product.setName(rs.getString("name"));
            return product;
        }
    }

}

Either keep @Component or @Repository in the ProductRepositoryImpl .ProductRepositoryImpl保留@Component@Repository Both is not possible.两者都不可能。

In addition to that, keep camelcase like this @Qualifier("productRepository") .除此之外,像这样@Qualifier("productRepository")保持驼峰命名。

Remove @ComponentScan from the Controller.从控制器中删除@ComponentScan It should be in the Java Config.它应该在 Java 配置中。

Example of Java - Based Config :基于 Java 的配置示例:

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

}

If using XML-Based Config, then do the down below :如果使用基于 XML 的配置,请执行以下操作:

Update applicationContext.xml or any servlet.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>

If this is not working, then check your custom servlet XML files or applicationContext.xml with web.xml for configuration errors.如果这不起作用,请检查您的自定义 servlet XML 文件或带有web.xml applicationContext.xml有配置错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring NoSuchBeanDefinitionException 存储库进入服务 - Spring NoSuchBeanDefinitionException repository into service Spring-将存储库自动装配到服务中时出现NoSuchBeanDefinitionException - Spring - NoSuchBeanDefinitionException when autowiring Repository into Service 在Spring中实现JpaRepostiory时使用存储库批注 - Using repository annotation when implementing JpaRepostiory in Spring 使用Spring Roo的BeanCreationException NoSuchBeanDefinitionException - BeanCreationException NoSuchBeanDefinitionException using Spring roo 在春季测试中使用@Autowired的NoSuchBeanDefinitionException - NoSuchBeanDefinitionException using @Autowired in spring test 构建简单的Spring JPA项目时出现NoSuchBeanDefinitionException - NoSuchBeanDefinitionException while building a simple Spring JPA project Spring NoSuchBeanDefinitionException:没有可用类型 [@Repository class] 的 Qualifying Bean - Spring NoSuchBeanDefinitionException: No Qualifying Bean of Type [@Repository class] available 尝试在控制器Spring Boot上@Autowire存储库时获取NoSuchBeanDefinitionException? - Get NoSuchBeanDefinitionException when trying to @Autowire repository on controller Spring Boot? 将Spring MVC与JPA一起使用时,NoSuchBeanDefinitionException - NoSuchBeanDefinitionException when using spring mvc with JPA 我在使用SpEL时获取NoSuchBeanDefinitionException - Getting NoSuchBeanDefinitionException while I am using SpEL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM