configuration - 弹簧型材。配置数据库问题
问题描述
我正在尝试探索 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;
}
}
因此,配置无法按教程中的预期工作- 当我启动项目时,日志中会出现以下消息:
- …………
- …………
- DEV H2 的数据库连接
- 无效的
- 无效的
但期望(在教程中)在日志中我得到 ** url ** 和 ** driver-class-name **:
我错过了什么?
解决方案
您需要为变量创建标准的 getter 和 setter
推荐阅读
- python - 我的桌面找到了 Python 包,但我的树莓派没有找到
- css - 如何使用 css 使 webkit 滚动条居中
- windows - WinEventHook 应用程序的 EVENT_OBJECT_DESTROY 案例被调用,即使应用程序没有被销毁?
- spring-boot - Spring-boot CRUD 应用程序无法以“启动 Tomcat 上下文时出错。异常:org.springframework.beans.factory.UnsatisfiedDependency
- ios - 为什么我的 PC 中出现证书不匹配错误并且错误域 = NSURLErrorDomain 代码 = -999?它如何在其他 mac 浏览器中工作?
- java - 将 Google App Engine 应用程序部署为单个 JAR 时如何指定实例类?
- node.js - 如何以角度表达重定向到特定路由器页面?
- python - mypy 错误:模块没有属性“__path__”
- java - getClass().getResource(..) 使图像图标突然为空
- facebook - Facebook 转换 API CAPI