这是问题提示:
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;
}
}
注入失败,从异常可以看出来
'CustomerServiceImpl': 配置这个类的地方有问题,有个这类吗或者类名是不是不一致造成的
异常原因是找不到自动注入的CustomerMapper bean,看你的这个类代码是否有Repository注解,有的在配置扫描这个包.
[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中注解相关配置是否正确。
'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错误:注入资源依赖失败;
看了大家的回答,基本问题还是bean的注入失败,但是具体的程序问题还需要你自己找,多看看细节的地方,注解之类的。
你的Spring配置文件,并不能看到你的具体配置,只给了开头,所以没法帮你看。
另外有一点要提示的,service层的@service后面建议不写()中的东西,默认是customerServiceImpl,首字母要小写。
链接的问题,资源载入失败
No matching bean of type [com.company.item.dao.CustomerMapper] found for dependency
CustomerMapper 这个不存在吧,检查一下这个类,另外检查一下这个类是不是加了注解