错误信息如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.github.abel533.mapper.Mapper com.hlyd.user.service.impl.BaseServiceImpl.mapper; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.github.abel533.mapper.Mapper] is defined: expected single matching bean but found 2: accountMapper,userMapper
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
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.BeanCreationException: Could not autowire field: private com.github.abel533.mapper.Mapper com.hlyd.user.service.impl.BaseServiceImpl.mapper; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.github.abel533.mapper.Mapper] is defined: expected single matching bean but found 2: accountMapper,userMapper
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:518)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
... 22 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.github.abel533.mapper.Mapper] is defined: expected single matching bean but found 2: accountMapper,userMapper
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:874)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:490)
... 24 more
这是工程目录
依赖关系已经弄好
我自己定义的Mapper继承通用Mapper:
package com.hlyd.user.mapper;
import com.github.abel533.mapper.Mapper;
import com.hlyd.user.entity.Account;
public interface AccountMapper extends Mapper<Account> {
}
package com.hlyd.user.mapper;
import com.github.abel533.mapper.Mapper;
import com.hlyd.user.entity.User;
public interface UserMapper extends Mapper<User> {
}
然后service借口,写了个baseService:
package com.hlyd.user.service;
import java.util.List;
public interface BaseService<T> {
/**
* 根据主键ID查询一条数据
* @param id
* @return
*/
public T queryById(Object id);
/**
* 查询所有数据
* @return
*/
public List<T> queryAll();
/**
* 根据条件查询总记录数
* @param t
* @return
*/
public Integer queryCountByWhere(T t);
/**
* 根据条件查询列表
* @param t
* @return
*/
public List<T> queryListByWhere(T t);
/**
* 分页查询
* @param page
* @param rows
* @return
*/
public List<T> queryByPage(Integer page, Integer rows);
/**
* 根据对象属性查询一条数据
* @param t
* @return
*/
public T queryOne(T t);
/**
* 添加数据,不忽略Null值
* @param t
*/
public void save(T t);
/**
* 添加数据,忽略Null值得
* @param t
*/
public void saveSelective(T t);
/**
* 根据主键修改数据,不忽略Null值
* @param t
*/
public void updateById(T t);
/**
* 根据主键修改数据,忽略Null值的
* @param t
*/
public void updateByIdSelective(T t);
/**
* 根据主键删除数据
* @param id
*/
public void deleteById(Object id);
/**
* 根据多个主键ID删除数据
* @param ids
*/
public void deleteByIds(List<Object> ids);
}
然后在自己也实现写了实现baseServiceImpl
public class BaseServiceImpl<T> implements BaseService<T> {
@Autowired
private Mapper<T> mapper;
// 指定的T的泛型
private Class clazz;
public BaseServiceImpl() {
// 指定父类的类型type
Type type = this.getClass().getGenericSuperclass();
// 根据这个父类的type类型转成泛型
ParameterizedType pType = (ParameterizedType) type;
// 获取T的泛型
clazz = (Class<T>) pType.getActualTypeArguments()[0];
}
@Override
public T queryById(Object id) {
T t = mapper.selectByPrimaryKey(id);
return t;
}
.........
}
然后我开始写自己的业务service借口 和实现类
public interface AccountService extends BaseService<Account> {
}
public interface UserService extends BaseService<User> {
}
@Service
public class AccountServiceImpl extends BaseServiceImpl<Account> implements AccountService {
}
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
}
配置文件:通用Mapper 我是在SqlMapConfig.xml中配的:
<!-- 配置插件 -->
<plugins>
<!-- 配置分页插件的时候,必须在配置通用Mapper插件之上 -->
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
<!-- 配置通用的Mapper插件 -->
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!--主键自增回写方法,默认值MYSQL,详细说明请看文档-->
<property name="IDENTITY" value="MYSQL"/>
<!--通用Mapper接口,多个通用接口用逗号隔开-->
<property name="mappers" value="com.github.abel533.mapper.Mapper"/>
</plugin>
</plugins>
在spring配置文件中也加载了mybatis的配置文件 扫描了Mapper包,基本配置没什么问题
但是我在用本地的tomcat8启动时就会爆错:错误信息在最上面,
但是用maven插件启动就不报错,配的是tomcat7插件。求大神指点,看错误信息是说通用Mapper有两个bean spring不知道注入哪个了,但是插件启动为什么不会报错呢,我检查过jar包 都有 且一模一样
自动注入失败
你注入的接口存在多个实现类,无法指定注入哪个实现类
,你可以使用@Resouce方式注入,或者对实现类加primary
@Qualifier @Primary
这两个注解都可以
第一个注解到注入对象上,指定一个自动注入类的beanName
第二个注解指定到一个service实现类为自动注入的首选
回复 @eric_qin:你把注入类型限定为你自己继承的接口试试看报错是:我在BaseServiceImpl中注入通用Mapper,然后我有两个自定义Mapper继承通用Mapper,指定了泛型。然后spring不知道注入谁了,但是我继承的Mapper已经指定泛型了,用的也是spring4.1.3,已经支持泛型注入了啊@abel533
没人知道怎么解决吗,我在maven的配置文件里面配置的jdk8 本地也是jdk8的环境。然后用maven配的tomcat7插件启动就不报错。
mark我也遇到了同样的问题,你找到了解决方案没?
因为题主的这句话, 让我想到一个办法,在需要注入自定义mapper的地方,
@Autowired(required=false)protectedMapper<T>mapper;
然后就没问题能运行了
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。