使用SpringMVC框架时引发org.springframework.beans.factory.BeanCreationException

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name ‘xz_productController’:
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private org.xz_lss.com.service.Xz_productService org.xz_lss.com.controller.Xz_productController.xz_productService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.xz_lss.com.service.Xz_productService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
在这里插入图片描述
controller代码:

package org.xz_lss.com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xz_lss.com.model.Product;
import org.xz_lss.com.service.Xz_productService;

@Controller
public class Xz_productController {

	@Autowired
	private Xz_productService xz_productService;
	
	/**添加商品*/
	@RequestMapping("/add_Product")
	public String add_Product(HttpServletRequest request,HttpSession session) throws Exception{
		String id = "P_"+request.getParameter("id");
		String name = request.getParameter("name");
		double price = Double.valueOf(request.getParameter("price"));
		int num = Integer.valueOf(request.getParameter("num"));
		String dp_name = request.getParameter("dp_name");
		String lable = request.getParameter("lable");
		
		if(xz_productService.find_ProductById(id)==1) {  //如果商品的id号已经存在,返回添加页面并提示
			return "add_Product";
		}else {                                   //否则向数据库添加商品
			Product product = new Product();
			product.setId(id);
			product.setName(name);
			product.setPrice(price);
			product.setNum(num);
			product.setDp_name(dp_name);
			product.setLable(lable);
			xz_productService.add_Product(product);  //添加成功返回商家主页面
			return "S_main";
		}	
	}
}

service代码:

package org.xz_lss.com.service.impl;

import javax.annotation.Resource;

import org.xz_lss.com.mapper.Xz_productMapper;
import org.xz_lss.com.model.Product;
import org.xz_lss.com.service.Xz_productService;

public class Xz_productServiceImpl implements Xz_productService{

	@Resource
	private Xz_productMapper xz_productMapper;
	
	@Override
	public int find_ProductById(String id) {
		
		return xz_productMapper.find_ProductById(id);
	}

	@Override
	public void add_Product(Product product) {
		xz_productMapper.add_Product(product);
		
	}

}

分析一下原因
1、 Injection of autowired dependencies failed

大概意思是说Bean容器创建异常,@autowired的注解注入Service层失败了,同时在后面又报出了Controller层也是没有注解注入!也就是说本质问题应该是@autowired不能自动注入
2、Spring 的 Service注入
Spring通过@Service(来自SpringMVC的注解)来实现Service层的实现类注入到spring容器中。
而此时这里的@Autowired的DAO层接口的代理对象并没有报错,当时在网上查找了一番,以为是因为DAO层没有用@Compent或者@Repository这样的注解造成的错误,后来思考了一下,发现因为是用Mapper映射做的DAO层接口的实现类,所以完全没有必要去做这个注解,那么问题也就与DAO层无关了。
而@Controller这里的注解是没有错误的
3、Controller为什么报错
但是Controller层报错的信息是注入到它里的Service层,也就是@Autowired这个注解。可是为什么@Autowired会失效呢?那么仔细想一下肯定是Service层并没有注入到Spring容器中去!所以追溯到Service的实现类去。也就是说Service的实现类并没有成功注入到Spring容器中。

仔细审查才发现 自己Service的实现类竟然没有加@Service注解,虽然Service接口写了注解,但是他的实现类没有写。这种低级错误,希望以后注意。
在这里插入图片描述
注解加上就好使了。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值