Javaweb项目中配置遇到的一个小问题 10C

这是问题提示:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomerServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.company.item.dao.CustomerMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4811)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.company.item.dao.CustomerMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, lookup=, authenticationType=CONTAINER)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
... 22 more

这是我的spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 引入属性文件 -->

<!-- 自动扫描(自动注入) -->

这是我的service层:
package com.company.item.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.company.item.dao.CustomerMapper;
import com.company.item.model.Customer;
import com.company.item.service.CustomerService;
import com.framework.utils.PrimaryKeyUtil;
@Service("CustomerServiceImpl")

public class CustomerServiceImpl implements CustomerService {
@Resource
private CustomerMapper customerMapper;
public boolean addCustomer(Customer customer) {
customer.setCustomerId(PrimaryKeyUtil.getPrimaryKey());
int i=customerMapper.insertSelective(customer);
return i>0?true:false;
}

}

13个回答

异常原因是找不到自动注入的CustomerMapper bean,看你的这个类代码是否有Repository注解,有的在配置扫描这个包.

注入失败,从异常可以看出来

'CustomerServiceImpl': 配置这个类的地方有问题,有个这类吗或者类名是不是不一致造成的

[com.company.item.dao.CustomerMapper] 从这个类里面@找autowired的对象,某个对象自动注入失败

直接原因:Bean对象的注解标签没有添加 ( 如 @Controller, @Service,@Repository等等),或者是注解的扫描是否打开,即在applicationContext.xml 上下文中是否配置了 : 或者 这二者选其一,有了前者就不用再加后者。这里需要说明的是为了不引起Service(业务层)事务不生效的问题,建议在applicationContext.xml 中只扫描注入除控制层以外的注解,而控制层(Controller)的注入交由MVC来进行扫描注入(servlet)。
光从上述异常中是无法看出注解是否启动扫描的。
间接原因:1. 代码编译问题导致。如,Myeclipse 或其他IDE工具。 这种clean 重新编译即可
2. JDK 与 Spring 版本不兼容 根据自己的JDK找相应支持的Jar
3. 缺少注解相关的Jar 文件 在网上找标准配置清单(网上一堆)
4. 扫描器配置冲突
检查web.xml ,applicationContext.xml 和aplicationContext-mvc.xml中注解相关配置是否正确。

weixin_41528225
weixin_41528225 1
接近 2 年之前 回复
qq_33259615
问道于途 我配置了注解扫描呀:<context:component-scan base-package="java.com.company.item.service.impl" />注解也全都写了
接近 2 年之前 回复

'CustomerServiceImpl': 配置这个类的地方有问题

我觉得:你把@Service("CustomerServiceImpl")后面的CustomerServiceImpl去掉。给你个例子
public interface LoginService {
int insertCokie(Map params);
}
@Service
public class LoginServiceImpl implements LoginService {
private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
@Autowired
LoginDao loginDao;
public int insertCokie(Map params) {
return loginDao.insertCokie(params);
}
}
//controll写法
@RestController
@RequestMapping("toLogin")
public class LoginController {
@Autowired
LoginService loginService;
}

代码编译问题导致。如,Myeclipse 或其他IDE工具。 这种clean 重新编译即可

CustomerMapper是否用注解标注了,然后,用@Autowired注解替换@Resource注解试试

CustomerServiceImpl”的bean错误:注入资源依赖失败;

共13条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!