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 ...
随机推荐
- Hadoop问题:The auxService:mapreduce_shuffle does not exist
问题描述:The auxService:mapreduce_shuffle does not exist INFO mapreduce.Job: Task Id : attempt_146180833 ...
- MYSQL优化派生表(子查询)在From语句中的
Mysql 在5.6.3中,优化器更有效率地处理派生表(在from语句中的子查询): 优化器推迟物化子查询在from语句中的子查询,知道子查询的内容在查询正真执行需要时,才开始物化.这一举措提高了性能 ...
- 使用异步方法在XAML中绑定系统时间
最近的工作需要在程序界面上显示实时的系统时间,网上查了查大部分都是用Timer或者线程来实现. 个人非常不喜欢用Timer,感觉这东西有点太耗资源,然后思考了下觉得更好的方法应该是使用异步的方法在委托 ...
- AI_深度学习概论
什么是是神经网络? 假如有6间房屋的数据集,已知房子的面积,单位是平方米或平方英尺,已知房子的价格.如果通过这6间房子的价格和房子的面积,预测房子的价格,首先要建立起一个数据模型 ,x轴为价格,y轴为 ...
- Linux或Window是修改snmp的默认端口
SNMP默认端口通讯使用 UDP 161,在安装一些监控软件的过程中,常常提示端口被占用等情况,下面说一下如何修改系统的默认SNMP端口 windows修改snmp端口 1 打开services文件 ...
- 自己用的一套reset.css,打算整理一下方便以后用,持续更新中,各位大神,不喜勿喷
*{margin: 0; padding: 0;border:none;}img{vertical-align: top;width: 100%;border: none;}ul,li{list-st ...
- js二维码插件总结
jquery.qrcode.js生成二维码插件&转成图片格式 http://blog.csdn.net/u011127019/article/details/51226104
- 集中式(SVN)和分布式(Git)版本控制系统的简单比较
集中式(SVN) 分布式(Git) 是否有中央服务器 有.开发人员需要从中央服务器获得最新版本的项目然后在本地开发,开发完推送给中央服务器.因此脱离服务器开发者是几乎无法工作的 没有中央 ...
- windows 7 wifi热点配置
自我总结,有什么不足或更好的解决方案,请告知,感激不尽! 目的:闲来无事的童鞋,可以试一试自己配置wifi热点. ps:其实wifi热点配置是系统存在的功能,只不过需要配置. 现在win桌面wifi热 ...
- [JavaScript]自执行函数
最近在接触mui的时候,遇到了一段代码: (function($, doc) { $.init({ statusBarBackground: '#f7f7f7' }); $.plusReady(fun ...