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

编程知识 更新时间:2023-05-02 05:31:34

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对应的路径,我这里倒是没有加,用以上两个方法的任一一种就已经启动成功了。

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

更多推荐

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

本文发布于:2023-04-26 06:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/59b3eb91fe97f2a875ac5f7f6e8439bb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报错   defining   springboot   mybatis   xxx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 104583文章数
  • 26216阅读数
  • 0评论数