Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案
果然不看教程直接使用在遇到问题会懵逼,连解决问题都得搜半天还不一定能帮你解决了。。。
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.demo.service.impl.UserServiceImpl required a bean of type 'com.demo.mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.demo.mapper.UserMapper' in your configuration.
SpringBoot启动失败,告诉我Bean配置失败,楼主看了看 该用的注解都用上了 这是咋的回事嘞?
mapper(Dao层)
package com.demo.mapper; import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository; import com.demo.domain.User; //@Component
@Repository
public interface UserMapper { public User gYeMian(User u); public int sYeMian(User u); }
service
package com.demo.service.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.demo.domain.User;
import com.demo.mapper.UserMapper;
import com.demo.service.UserService; @Service(value = "userService")
public class UserServiceImpl implements UserService{ @Autowired
private UserMapper mapper; @Override
public User gYeMian(User u) {
User user = mapper.gYeMian(u);
return user;
} @Override
public int sYeMian(User u) {
int i = mapper.sYeMian(u);
return i;
} }
controller
package com.demo.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.demo.domain.User;
import com.demo.service.UserService; @Controller
@RequestMapping(value = "/uc")
public class UserController { @Autowired
private UserService userService; @ResponseBody
@RequestMapping("/stemp.htm")
private String sYeMian(String muBan, HttpServletRequest request){ User u = new User();
u.setMuBan(muBan);
System.out.println("muBan=" + muBan);
int i = userService.sYeMian(u); if (i>0){
return "存储成功";
}
return "存储失败";
} }
后来在网上看到网友说要用@Mapper注解,这才把问题解决了 至于具体原因,楼主还需要好好看看文档再来解释。
解决方案一:
mapper(Dao层)
package com.demo.mapper; import org.apache.ibatis.annotations.Mapper; import com.demo.domain.User; @Mapper
public interface UserMapper { public User gYeMian(User u); public int sYeMian(User u); }
解决方案二:
Application(启动类)
package com.demo; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan(value = "com.demo.mapper")
public class App
{
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
原因:在mybatis-spring-boot-autoconfigure的jar包中有一个类 MybatisAutoConfiguration,在这个类中的registerBeanDefinitions方法告诉了我们
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { logger.debug("Searching for mappers annotated with @Mapper"); ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); try {
if (this.resourceLoader != null) {
scanner.setResourceLoader(this.resourceLoader);
} List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
if (logger.isDebugEnabled()) {
for (String pkg : packages) {
logger.debug("Using auto-configuration base package '{}'", pkg);
}
} scanner.setAnnotationClass(Mapper.class);
scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(packages));
} catch (IllegalStateException ex) {
logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
}
}
Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案的更多相关文章
- 记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration
今天遇到的一个问题: 代码检查了好几次,都没有错误,但是启动时就会报错Consider defining a bean of type ''' in your configuration. 启动类在c ...
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...
- 【spring boot】mybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper' in your configuration. 【Mapper类不能被找到】@Mapper 和@MapperScan注解的区别
启动报错: 2018-05-16 17:22:58.161 ERROR 4080 --- Disconnected from the target VM, address: '127.0.0.1:50 ...
- Spring Cloud ZooKeeper集成Feign的坑1,错误:Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
错误如下: ERROR 31473 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** A ...
- Consider defining a bean of type 'com.lvjing.dao.DeviceStatusMapper' in your configuration.
"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:C:\Program Files\JetBra ...
- springboot 工程启动报错之Consider defining a bean of type ‘XXX’ in your configuration.
一.前言: 使用springboot自动注入的方式搭建好了工程,结果启动的时候报错了!!!,错误如下图: Description: Field userEntityMapper in com.xxx. ...
- Consider defining a bean of type 'package' in your configuration [Spring-Boot]
https://stackoverflow.com/questions/40384056/consider-defining-a-bean-of-type-package-in-your-config ...
- Consider defining a bean of type 'XX.XX.XX.XX.mapper.XXMapper' in your configuration.
今天构建一个springboot 项目,采用mybatis+mysql 然后就出现了这种错误....浪费我半天时间 Description: Field loginLogMapper in com.g ...
- Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration
https://www.cnblogs.com/EasonJim/p/7546136.html 错误如下: ERROR 31473 --- [ main] o.s.b.d.LoggingFailure ...
随机推荐
- Git-远程仓库的使用
Git修改远程仓库地址 1.修改命令 git remote set-url origin [url] 例如:$ git remote set-url origin gitlab@gitlab.chum ...
- junit4X系列--Rule
原文出处:http://www.blogjava.net/DLevin/archive/2012/05/12/377955.html.感谢作者的无私分享. 初次用文字的方式记录读源码的过程,不知道怎么 ...
- lambda高级进阶--延迟执行
前面的整理我们也已经说到了,使用lambda表达式的主要原因是,将代码的执行延迟到一个合适的时间点.在使用lambda表达式的时候务必记住一点就是说lambda表达式都是延迟执行的. 延迟执行代码的原 ...
- JAVA中实现让程序等待一段时间的方法
JAVA中想让代码等待一段时间再继续执行,可以通过让当前线程睡眠一段时间的方式. 方法一:通过线程的sleep方法. Thread.currentThread().sleep(1000); 在需要程序 ...
- strstr()与find()
- 如何更改wampserver的网站根目录
我之前把网站根目录自定义为:D:/demo,现在想改为:D:www, 过程就是打开httpd.conf文件,搜索demo,然后把demo改为www,之后更改虚拟配置文件,记住D盘下一定要有www目录 ...
- Python 定位桌面
通过注册表寻找桌面路径: (用内置的winreg) import winreg key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, \ ...
- bzoj1193: [HNOI2006]马步距离
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MB Description 在国际象棋和中国象棋中,马的移动规则相同,都是走&q ...
- CSS :befor :after 伪元素的妙用
本篇重点介绍CSS中的:befor.:after创建的伪元素几种使用场景,如填充文本.作为iconfont.进度线.时间线以及几何图形. 1. 介绍 1.1 说明 CSS中的:befor.:after ...
- 史上最全的JFinal源码分析(不间断更新)
打算 开始 写 这么 一个系列,希望 大家 喜欢,学习 本来就是 一个查漏补缺的过程,希望大家能提出建议.本篇 文章 是整个目录的向导,希望 大家 喜欢.本文 将以 包的形式跟大家做向导. Handl ...