抛出异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pers.zww.grad.dao.IAdminDao pers.zww.grad.service.impl.AdminServiceImpl.adminDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pers.zww.grad.dao.IAdminDao] 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), @org.springframework.beans.factory.annotation.Qualifier(value=adminDao)}
此为自动生成的Mapper,我加了注解@Repository
这是Service里面注入
配置文件都已扫描过了,而且相关mybatis的如下:
去掉DAO接口上的注解
如果不成功,再去掉ServiceImpl 中@Qualifier注解
接口是不加注解的
mapper扫描会自动生成实现类,ServiceImpl中需要注入的是实现类,而不是接口
好的,我试试看。刚回到家
我试了去掉DAO接口上的注解,后来没成功,就又把ServiceImpl 中@Qualifier注解去掉了,还是不成。我就又把字段名称改成了 private IAdminDao iAdminDao ;也还是不成功,好纠结
@zhaoww: 把两个都删掉后的日志粘一下,首先试试修改iAdminDao的名字。java中尽量前两个字母小写。否则可能会产生bean相关的意外错误。再试试修改类名IAdminDao,I用My代替。名字尽量规范些吧。还是想看看日志,两次是否一样的错误。
@小风疏雨:
1.改名为原始的AdminMapper.java,
@Autowired
@Qualifier("adminMapper")
private AdminMapper adminMapper ;
Error creating bean with name 'adminService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pers.zww.grad.dao.AdminMapper pers.zww.grad.service.impl.AdminServiceImpl.adminMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pers.zww.grad.dao.AdminMapper] 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), @org.springframework.beans.factory.annotation.Qualifier(value=adminMapper)}
2.改名为原始的AdminMapper.java,
@Autowired
private AdminMapper adminMapper ;
异常与上面基本一致
(原始的异常在上面贴出,也基本一样)
当然<mapper namespace="pers.zww.grad.dao.AdminMapper">也是改了的
@小风疏雨: 不过所有情况我在如下的junit测试的时候,都是可以成功运行处结果的
@Test public void testSerTest() { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-mybatis.xml"); SpringAnnoService service=applicationContext.getBean(SpringAnnoService.class); service.testSer(); AdminMapper adminMapper =applicationContext.getBean(AdminMapper.class); Admin admin=adminMapper.selectByPrimaryKey("3"); System.out.println(admin); }
@zhaoww: sqlSessionFactory 中的mapperLocation 是扫描mapper.xml的吧,第二个又扫描了mapper。先去掉sqlSessionFactory的。
@小风疏雨: 去掉后就提示找不到mapper.xml了,你说的第二个是指basepackage那个吗?它找dao接口的。sqlSessionFactoryBeanName那个应该也没错
@zhaoww: 要下班了,先给你看看我以前写过的一个能用的配置文件吧
ApplicationContext-dao.xml
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 不要扫描spring mvc相关的controller --> <context:component-scan base-package="com.dtkj.sysup.mapper"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 数据库连接池配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <!-- <property name="maxPoolSize" value="100"></property> <property name="maxIdleTime" value="30"></property> <property name="checkoutTimeout" value="10000"></property> --> </bean> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> </bean> <!-- mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dtkj.sysup.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 别名配置 --> <typeAliases> <!-- 配置po类的包,别名就是类名,不区分大小写 --> <package name="com.dtkj.sysup.pojo" /> <package name="com.dtkj.sysup.povo"/> </typeAliases> </configuration>
还有另外三个,ApplicationContext-service.xml,ApplicationContext-trans.xml,Springmvc.xml与dao关联不大,如果不需要别名,SqlMapConfig.xml也不需要的
@小风疏雨: 好的ok ,3Q
@zhaoww: 明白了,3Q 漏写了上下文监听器
@注入DAO不是@qualifier這個吧...
谢谢,不过我用的是上面那个@Autowired ,(@Qualifier是用来指定具体哪一个的,以防@Autowired byType候选Bean>1的情况的)
@zhaoww: 你看爆出來的错、说那个private类型的。还有我记得要有setter或getter的吧
@g皓皓: 不过private类型没错,然后@autowired加在字段上是不需要setter的
@zhaoww: 額。書老夫愚鈍,好久沒用確實記不清了
@g皓皓: 不过还是谢谢了
@zhaoww: 不客氣