Spring報(bào)錯(cuò):Error creating bean with name的問(wèn)題及解決
解決Spring報(bào)錯(cuò):Error creating bean with name
在使用springboot構(gòu)建 oltu-oauth的時(shí)候啟動(dòng)項(xiàng)目報(bào)錯(cuò)

在網(wǎng)上找一下沒(méi)找到 ,之前項(xiàng)目對(duì)比發(fā)現(xiàn)忘記在WebApplication中少加了個(gè)注解

加上之后問(wèn)題解決,查了下這個(gè)注解(橫線以下是對(duì)@MapperSecan的介紹):
之前是,直接在Mapper類上面添加注解@Mapper,這種方式要求每一個(gè)mapper類都需要添加此注解,麻煩。
通過(guò)使用@MapperScan可以指定要掃描的Mapper類的包的路徑,比如:
@SpringBootApplication
@MapperScan("com.lz.water.monitor.mapper")
// 添加對(duì)mapper包掃描
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); }
}同時(shí),使用@MapperScan注解多個(gè)包
@SpringBootApplication
@MapperScan({"com.kfit.demo","com.kfit.user"})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
} 如果如果mapper類沒(méi)有在Spring Boot主程序可以掃描的包或者子包下面,可以使用如下方式進(jìn)行配置
@SpringBootApplication
@MapperScan({"com.kfit.*.mapper","org.kfit.*.mapper"})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
} 導(dǎo)入seata報(bào)錯(cuò):Error creating bean with name ‘dataSourceProxyConfig‘
先去找配置的 registry.conf和file.conf有沒(méi)有問(wèn)題,如果沒(méi)有問(wèn)題那么就是你yml中配置了數(shù)據(jù)源
?datasource: ? ? driver-class-name: com.mysql.jdbc.Driver ? ? url: jdbc:mysql://localhost:3306/gift_auth ? ? username: root ? ? password: xxx
同時(shí)在你這個(gè)服務(wù)中又配置了數(shù)據(jù)源 這里可以是mybatis-plus版也可以是mybatis版,導(dǎo)致和yml中配置的數(shù)據(jù)源沖突了。
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;
? ? //手動(dòng)配置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));
? ? ? ? //事務(wù)管理工廠
? ? ? ? factory.setTransactionFactory(new SpringManagedTransactionFactory());
? ? ? ? return factory;
? ? }
? ? @Bean
? ? public DataSourceProxy dataSource() {
? ? ? ? return new DataSourceProxy(druidDataSource());
? ? }
}所以在自定義配置源里面加入@Primary 默認(rèn)自定義配置先啟動(dòng)
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修改了同時(shí)修改yml中的配置
? ? @Value("${mybatis-plus.mapper-locations}")
? ? private String mapperLocations;
? ? //手動(dòng)配置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));
? ? ? ? //事務(wù)管理工廠
? ? ? ? factory.setTransactionFactory(new SpringManagedTransactionFactory());
? ? ? ? return factory;
? ? }
? ? @Bean
? ? public DataSourceProxy dataSource() {
? ? ? ? return new DataSourceProxy(druidDataSource());
? ? }
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java并發(fā)編程實(shí)戰(zhàn)之從線程池到CompletableFuture詳解
文章總結(jié)了Java并發(fā)編程的關(guān)鍵點(diǎn),包括線程池的最佳實(shí)踐和監(jiān)控、CompletableFuture的異步編程、并發(fā)集合與工具類的使用、原子類與CAS、Fork/Join框架等內(nèi)容,強(qiáng)調(diào)了理解happens-before規(guī)則和內(nèi)存模型的重要性2026-04-04
Java筆記之從IO模型到Netty框架學(xué)習(xí)初識(shí)篇
Netty作為一個(gè)已經(jīng)發(fā)展了十多年的框架,已然非常成熟了,其中有大量的細(xì)節(jié)是普通使用者不知道或者不關(guān)心的,本文帶你查缺補(bǔ)漏掌握Netty的使用2022-03-03
springboot如何通過(guò)controller層實(shí)現(xiàn)頁(yè)面切換
在Spring Boot中,通過(guò)Controller層實(shí)現(xiàn)頁(yè)面切換背景,Spring Boot的默認(rèn)注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody會(huì)將返回值轉(zhuǎn)換為字符串返回,因此無(wú)法實(shí)現(xiàn)頁(yè)面切換,將@RestController換成@Controller2024-12-12
springboot 高版本后繼續(xù)使用log4j的完美解決方法
這篇文章主要介紹了 springboot 高版本后繼續(xù)使用log4j的解決方法,需要的朋友可以參考下2017-12-12

