在Maven父子项目中搭建了一个SpringBpoot的Web模块工程,项目当中的DAO和Service层的接口和实现类都集成公共基类,但是启动报错
在Maven父子项目当中的的搭建了一个SpringBpoot的Web模块工程,项目当中的DAO和Service层的接口和实现类都集成公共基类,但是启动报错
项目结构 包名
system
|-system-modle com.system.model
|-system-common com.system.common
|-system-dao com.system.dao mybatis文件 resource/main/mybatis/mapper/
|-system-dao-common com.system.dao.common
|-system-dao-user com.system.dao.user
|-system-service com.system.service
|-system-service-common com.system.service.common
|-system-service-user com.system.service.user
|-system-web (springboot项目启动类工程) com.system(本项目的启动类SystemWebApplication就放在咋换个包的下面)
|-system-web-common com.system.web.common
|-system-web-user com.system.web.user
项目启动类
@EnableAutoConfiguration
@SpringBootApplication(
exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages={"com.system.dao",
"com.system.service", "com.system.web.controller"})
@MapperScan(basePackages={"com.system.dao"})
public class SystemWebApplication {
public static void main(String[] args) {
SpringApplication.run(SystemWebApplication.class, args);
}
}
由于此项目的DAO、Service、Web层接口、配置文件和实现类都在其他工程包下面,所以SpringBoot启动类扫描不到这些代码,
所以,我把com.system.dao,com.system.service,com.system.web.controller这几个包都用@ComponentScan配置了出来,
还有DAO层包还有@MapperScan单独配置了出来
application.yml配置文件
server:
port: 8090
spring:
mvc:
view:
prefix: /jsp/
suffix: .jsp
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/system?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath:mybatis/mapper/*/*.xml
公共DAO接口
public interface IBasicDao<T> {
public int insert(T obj) throws Exception;
public int batchInsert(List<T> list) throws Exception;
public int delete(T obj) throws Exception;
public int deleteById(@Param(value = "id") int id) throws Exception;
public int batchDelete(Integer[] list) throws Exception;
public int update(T obj) throws Exception;
public int batchUpdate(List<T> list) throws Exception;
public T select(@Param(value = "id") int id) throws Exception;
public List<T> selectAll() throws Exception;
public List<T> selectList(T obj) throws Exception;
public T selectNew() throws Exception;
public List<T> selectNewList(@Param(value = "count") int count) throws Exception;
public int getCount(Map<String, Object> param) throws Exception;
public List<T> getSplitList(Map<String, Object> param) throws Exception;
}
用户信息接口
@Mapper
@Component("userBaseDao")
public interface IUserBaseDao extends IBasicDao<UserInfo> {
}
Service层公共接口
public interface IBaseService<T> {
public int add(T obj) throws LogicException;
public int delete(T obj) throws LogicException;
public int deleteById(int id) throws LogicException;
public int update(T obj) throws LogicException;
public T find(int id) throws LogicException;
public List<T> findAll() throws LogicException;
public List<T> findList(T obj) throws LogicException;
}
Service层公共接口实现类
public class BaseServiceImpl<T, I extends IBasicDao<T>> implements IBaseService<T> {
public I getBasicDao() {
return basicDao;
}
public void setBasicDao(I basicDao) {
this.basicDao = basicDao;
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = LogicException.class)
public int add(T obj) throws LogicException {
return basicDao.insert(obj);
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = LogicException.class)
public int delete(T obj) throws LogicException {
return basicDao.delete(obj);
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = LogicException.class)
public int deleteById(int id) throws LogicException {
return basicDao.deleteById(id);
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = LogicException.class)
public int update(T obj) throws LogicException {
return basicDao.update(obj);
}
public T find(int id) throws LogicException {
return basicDao.select(id);
}
public List<T> findAll() throws LogicException {
return basicDao.selectAll();
}
public List<T> findList(T obj) throws LogicException{
return basicDao.selectList(obj);
}
}
用户信息Service层接口
public interface IUserBaseService extends IBaseService<UserBase> {
}
用户信息实现类
@Service("userBaseService")
public class UserBaseServiceImpl extends
BaseServiceImpl<UserBase, IUserBaseDao> implements IUserBaseService {
private Log log = LogFactory.getLog(this.getClass());
@Autowired
@Qualifier("userBaseDao")
private IUserBaseDao userBaseDao;
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public int add(UserBase user) throws LogicException {
return userBaseDao.insert(user);
}
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public int delete(UserBase user) throws LogicException {
return userBaseDao.delete(user);
}
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public int deleteById(int id) throws LogicException {
return userBaseDao.deleteById(id);
}
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public int update(UserBase user) throws LogicException {
return userBaseDao.update(user);
}
@Override
public UserBase find(int id) throws LogicException {
return userBaseDao.select(id);
}
@Override
public List<UserBase > findAll() throws LogicException {
return userBaseDao.selectAll();
}
@Override
public List<UserBase> findList(UserInfo user) throws LogicException {
return userBaseDao.selectList(user);
}
}
SpringBoot项目启动报错
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. main [36mo.s.boot.SpringApplication
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userBaseServiceImpl': Unsatisfied dependency expressed through field 'userBaseDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userBaseDao' defined in file [E:\workspace\system\system-dao-user\target\classes\com\system\dao\user\IUserBaseDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanF
百度了网上的做法,让公共基础类IBasicDao继承SqlSessionFactory 这个接口,可以解决这个问题
public interface IBasicDao<T> extends SqlSessionFactory {
这个做之后,项目又报了新的错误
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
***************************
APPLICATION FAILED TO START
***************************
Description:
file [E:\workspace\system\system-dao\target\classes\com\system\dao\IBasicDao.class] required a single bean, but 3 were found:
- userBaseDao: defined in file [E:\workspace\system\system-dao-user\target\classes\com\system\dao\user\IUserBaseDao.class]
- userInfoDao: defined in file [E:\workspace\system\system-dao-user\target\classes\com\system\dao\user\IUserInfoDao.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
我将注解@Qualifier加到DAO接口,以及Service注入的类上面,SpringBoot项目启动之后,还是报同样的错误
我将注解@Primary分别加到userBaseDao、userInfoDao和接口上面,SpringBoot项目启动之后,报
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-06-27 17:33:43.620 ERROR 212 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userBaseServiceImpl': Unsatisfied dependency expressed through field 'userBaDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userBaseDao' defined in file [E:\workspace\system\system-dao-user\target\classes\com\system\dao\user\IUserDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more than one 'primary' bean found among candidates: [userInfoDao]
现在这些问题我真是无法解决了,请各位中对SpringBoot和Maven整合比较熟悉的大牛和高人帮忙看看,这个问题如何解决???