Spring报错:Error creating bean with name的问题及解决
作者:一勺菠萝丶
这篇文章主要介绍了Spring报错:Error creating bean with name的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
解决Spring报错:Error creating bean with name
在使用springboot构建 oltu-oauth的时候启动项目报错
在网上找一下没找到 ,之前项目对比发现忘记在WebApplication中少加了个注解
加上之后问题解决,查了下这个注解(横线以下是对@MapperSecan的介绍):
之前是,直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦。
通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,比如:
@SpringBootApplication @MapperScan("com.lz.water.monitor.mapper") // 添加对mapper包扫描 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
同时,使用@MapperScan注解多个包
@SpringBootApplication @MapperScan({"com.kfit.demo","com.kfit.user"}) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
如果如果mapper类没有在Spring Boot主程序可以扫描的包或者子包下面,可以使用如下方式进行配置
@SpringBootApplication @MapperScan({"com.kfit.*.mapper","org.kfit.*.mapper"}) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
导入seata报错:Error creating bean with name ‘dataSourceProxyConfig‘
先去找配置的 registry.conf和file.conf有没有问题,如果没有问题那么就是你yml中配置了数据源
datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/gift_auth username: root password: xxx
同时在你这个服务中又配置了数据源 这里可以是mybatis-plus版也可以是mybatis版,导致和yml中配置的数据源冲突了。
package com.wangling.config; import com.alibaba.druid.pool.DruidDataSource; import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean; import io.seata.rm.datasource.DataSourceProxy; import org.mybatis.spring.transaction.SpringManagedTransactionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @Configuration public class DataSourceConfiguration { //mapper.xml路径 @Value("${mybatis-plus.mapper-locations}") private String mapperLocations; //手动配置bean @Bean @ConfigurationProperties("spring.datasource") public DataSource druidDataSource(){ return new DruidDataSource(); } @Bean public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception { //处理MybatisPlus MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); factory.setDataSource(dataSourceProxy); factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); //事务管理工厂 factory.setTransactionFactory(new SpringManagedTransactionFactory()); return factory; } @Bean public DataSourceProxy dataSource() { return new DataSourceProxy(druidDataSource()); } }
所以在自定义配置源里面加入@Primary 默认自定义配置先启动
package com.wangling.config; import com.alibaba.druid.pool.DruidDataSource; import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean; import io.seata.rm.datasource.DataSourceProxy; import org.mybatis.spring.transaction.SpringManagedTransactionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @Configuration public class DataSourceConfiguration { //mapper.xml路径 这里如果是mybatis就把mybatis-plus修改了同时修改yml中的配置 @Value("${mybatis-plus.mapper-locations}") private String mapperLocations; //手动配置bean @Bean @ConfigurationProperties("spring.datasource") @Primary public DataSource druidDataSource(){ return new DruidDataSource(); } @Bean public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception { //处理MybatisPlus MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); factory.setDataSource(dataSourceProxy); factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); //事务管理工厂 factory.setTransactionFactory(new SpringManagedTransactionFactory()); return factory; } @Bean public DataSourceProxy dataSource() { return new DataSourceProxy(druidDataSource()); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。