[英]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.