【发布时间】:2019-06-27 03:08:06
【问题描述】:
我在这里学习了一个教程:https://www.jianshu.com/p/2ef6a9259112。
我无法从我的数据库中自动装配密钥。
@RefreshScope
@RestController
public class MainController {
@Value("${key}")
private String sql;
@Autowired
private DataSource dataSource;
@RequestMapping("/showConfig")
@ResponseBody
public String showConfig() {
String configInfo = "sql key-value pair" + sql;
return configInfo;
}
客户端 bootstrap.properties:
spring.application.name=config-client
# This is the default:
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false
spring.cloud.config.label=master
spring.cloud.config.profile=test
server.port=7777
服务器属性
server.port=8888
spring.datasource.url=jdbc:mariadb://localhost:3306/noob?
createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=abc123
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.platform= mysql
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.maximum-pool-size=10
spring.profiles.active= jdbc
spring.jpa.hibernate.ddl-auto=create-drop
spring.cloud.config.server.default-profile=production
spring.cloud.config.server.default-label=latest
spring.cloud.config.server.jdbc.sql=SELECT `key`, `value` FROM `properties`
WHERE `application`=? AND `profile`=? AND `label`=?;
spring.cloud.config.server.jdbc.order=0
我的新手数据库中有这些表:
CREATE TABLE `properties` (
`application` varchar(200) DEFAULT NULL,
`profile` varchar(200) DEFAULT NULL,
`label` varchar(200) DEFAULT NULL,
`key` varchar(200) DEFAULT NULL,
`value` varchar(200) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO `properties` (`key`, `value`, `application`, `profile`,
`label`)
VALUES ('datasource-driver-class-
name','MyDriverClass','appplication1','production','latest');
org.springframework.beans.factory.BeanCreationException:创建名为“scopedTarget.mainController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${key}”中的占位符“key”
在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
在 ...
...省略了28个常用框架
【问题讨论】:
-
你确定键 @Value("${
key}") 应该用反引号括起来吗? -
如果您按照教程进行操作,并且使用基础数据,我希望唯一的
@Value是:${datasource-driver-class-name},我希望是"MyDriverClass"... :) !? -
你应该删除反引号
-
@xerx593 我接受了你的建议。我相信它应该工作。我希望我的结果是 MyDriverClass 但我收到另一个错误。 “com.example.demo.MainController 中的字段 dataSource 需要一个找不到的 'javax.sql.DataSource' 类型的 bean。”我认为这是进步。
-
啊,好(小进步)! nxt 问题:在教程中他使用“mysql”db,你使用“maria”......我知道它是“同一个家庭”,但驱动程序类(名称)不同! (在另一个 jar/依赖项中)...-> pom.xml(找到 mysql 替换为正确的驱动程序依赖项...google!)
标签: java spring-boot jdbc spring-cloud-config