Field dataSource in com.security1.config.SecurityConfig required a bean of type ‘javax.sql.DataSourc

我的是Spring Security项目
Field dataSource in com.security1.config.SecurityConfig required a bean of type ‘javax.sql.DataSourc_第1张图片
Field dataSource in com.security1.config.SecurityConfig required a bean of type ‘javax.sql.DataSourc_第2张图片

我的依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.7.2version>
		<relativePath/> 
	parent>
	<groupId>comgroupId>
	<artifactId>security1artifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>security1name>
	<description>Demo project for Spring Bootdescription>
	<properties>
		<java.version>1.8java.version>
	properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-securityartifactId>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		<dependency>
			<groupId>org.mybatis.spring.bootgroupId>
			<artifactId>mybatis-spring-boot-starterartifactId>
			<version>2.2.2version>
		dependency>


		<dependency>
			<groupId>mysqlgroupId>
			<artifactId>mysql-connector-javaartifactId>
			<scope>runtimescope>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>org.springframework.securitygroupId>
			<artifactId>spring-security-testartifactId>
			<scope>testscope>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

project>

yml配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/security?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: asd123456
  security:
    user:
      name: user # ?????????
      password: 123456 # ??????????
      roles: admin,user # ??????
server:
  port: 8066
logging:
  level:
    com.security1.mapper: debug

配置类找不到Datasource
新建DataSourceConfig配置类

package com.security1.config;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
@Configuration
//@Component
public class DataSourceConfig {
    @Bean
    public DataSource dodb(){
        DataSourceBuilder dataSourceBuilder=DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://localhost:3306/security?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("asd123456");
        return dataSourceBuilder.build();
    }
}

@Bean类型找不到解决了,但是出现了新的问题

Field dataSource in com.security1.config.SecurityConfig required a bean of type ‘javax.sql.DataSourc_第3张图片
说循环依赖问题,默认是关闭的,只能添加配置让他开启解决报错
Field dataSource in com.security1.config.SecurityConfig required a bean of type ‘javax.sql.DataSourc_第4张图片

再次启动成功运行,并测试项目功能ok
Field dataSource in com.security1.config.SecurityConfig required a bean of type ‘javax.sql.DataSourc_第5张图片

你可能感兴趣的:(spring,boot,sql,spring,boot,mybatis,spring)