待整理的错误

发布时间:2023年12月21日

Could not autowire. No beans of ‘DruidDataSource’ type found.

手写一个bean即可


import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;


@Configuration

public class DataSourceConfig {
    @Value("${spring.datasource.druid.url}")
    String url;

    @Value("${spring.datasource.druid.username}")
    String username;

    @Value("${spring.datasource.druid.password}")
    String password;

    @Value("${spring.datasource.druid.driver-class-name}")
    String driverClassName;

    @Bean
    public DruidDataSource druidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();

        // Configure DruidDataSource properties
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);

        // Add additional configurations if needed
        return dataSource;
    }
}
文章来源:https://blog.csdn.net/qq_45418837/article/details/135136632
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。