首页 > 解决方案 > 弹簧型材。配置数据库问题

问题描述

我正在尝试探索 Spring 配置文件并确实选择了以下资源:

https://dzone.com/articles/spring-boot-profiles-1

因此,我重复了代码(只有一个 - 在 application.properties 文件中使用我的数据库设置),它显示在教程中:

应用程序属性:

spring.profiles.active=dev
spring.application.name=profiles
app.message=This is message ${spring.application.name}

应用程序-dev.properties:

app.message=This is message ${spring.application.name} for dev profile

#Database settings
spring.datasource.url=jdbc:mysql://localhost:3306/developerparse?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
spring.datasource.sql-script-encoding=UTF-8

配置文件:

@Configuration
@ConfigurationProperties("spring.datasource")
@SuppressWarnings("unused")

public class DBConfiguration {

    private String driverClassname;
    private String url;
    private String username;
    private String password;

    @Profile("dev")
    @Bean
    public String devDatabaseConnection(){
        String DBConnection = "DB connection for DEV H2";
        System.out.println(DBConnection);
        System.out.println(driverClassname);
        System.out.println(url);
        return DBConnection;
    }
}

因此,配置无法按教程中的预期工作- 当我启动项目时,日志中会出现以下消息:

但期望(在教程中)在日志中我得到 ** url ** 和 ** driver-class-name **:

在此处输入图像描述

我错过了什么?

标签: configurationspring-profiles

解决方案


您需要为变量创建标准的 getter 和 setter


推荐阅读