Spring-启动 Cloug 配置客户端无法自动装配
Spring-Boot Cloug Config Client unable to Autowire
我在这里学习了教程: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
我在我的 noob 数据库中创建了这些表:
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
:
@Value("${datasource-driver-class-name}")
private transient String driverClassName;
I took your advice. I believe it is supposed to work. I want my result to be MyDriverClass but I am getting another error. "Field dataSource in com.example.demo.MainController required a bean of type 'javax.sql.DataSource' that could not be found."
..然后,你使用"maria",在教程中使用"mysql" db,请调整相应的maven依赖:
pom.xml:
<!-- dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<!-- version>1.1.7</version -->
</dependency>
我在这里学习了教程: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
我在我的 noob 数据库中创建了这些表:
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
:
@Value("${datasource-driver-class-name}")
private transient String driverClassName;
I took your advice. I believe it is supposed to work. I want my result to be MyDriverClass but I am getting another error. "Field dataSource in com.example.demo.MainController required a bean of type 'javax.sql.DataSource' that could not be found."
..然后,你使用"maria",在教程中使用"mysql" db,请调整相应的maven依赖:
pom.xml:
<!-- dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<!-- version>1.1.7</version -->
</dependency>