admin管理员组

文章数量:1530890

springboot+mybatis 启动报错 Consider defining a bean of type ‘xxx’ in your configuration

    • 希望此解决方案可以解决你们的问题,没有解决也别气馁,继续加油!!!

第一次使用springboot来创建一个项目,并且还是多模块项目,因此在搭建过程中就遇到了如下问题:

具体错误信息内容:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field sysUserService in com.wt.RBACweb.ctrl.LoginController required a bean of type 'com.zw.lotus.services.TbSysUserService' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.zw.lotus.services.TbSysUserService' in your configuration.

因为我将项目分为了entities、mapper、dao、services、web层。刚开始我使用如下注解在application.java类上,并不能启动成功

@ComponentScans(value = { @ComponentScan(value = "com.zw.lotus.services.*") })
@MapperScans(value = { @MapperScan("com.zw.lotus.mapper"), @MapperScan("com.zw.lotus.dao") })

因为我的所有项目包名都是以com.zw.lotus开始,所以使用如下两种方案方法就解决了问题:

  1. springboot启动的application.java类中新增如下注解:
 import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;

@ComponentScans(value = { @ComponentScan(value = "com.zw.lotus.*") })
@MapperScans(value = { @MapperScan("com.zw.lotus.mapper"), @MapperScan("com.zw.lotus.dao") })
public class ZwlotusRBACWebApplication {

	public static void main(String[] args) {
		SpringApplication.run(ZwlotusRBACWebApplication.class, args);
	}

}

刚开始只指定了@ComponentScans注解,还是会报如上错误,然后又指定了mapper所在的位置,就没有在报错。

  1. 新增一个配置类,将注解写在配置类上,这个类还是要在applicaton.java同级或下级目录下,不然还是还有此问题出现
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;

@ComponentScans(value = { @ComponentScan(value = "com.zw.lotus.*") })
@MapperScans(value = { @MapperScan("com.zw.lotus.mapper"), @MapperScan("com.zw.lotus.dao") })
@Configuration
public class BeanConfig {
   //如果要使用此方法的话,一定要加@Configuration注解,不然启动还是会报如上错误
}

还有的说是要在springboot中的yml或者properties配置文件加 mapper-locations对应的路径,我这里倒是没有加,用以上两个方法的任一一种就已经启动成功了。

希望此解决方案可以解决你们的问题,没有解决也别气馁,继续加油!!!

本文标签: 报错definingSpringBootMyBatisxxx