这种情况在1.5.3.RELEASE中没问题。
我有外部属性文件external.yml,它看起来像这样
app:
datasources:
- name: sourceOne
driver: xxx
url: some_url
username: u1
password: p1
- name: sourceTwo
driver: xxx
url: some_url2
username: u2
password: p2
我添加了@EnableConfigurationProperties({DatabaseProperties.class})
该课程如下:
@Component
@PropertySource("classpath:database.yml")
@ConfigurationProperties(prefix = "app") // prefix app, find app.* values
public class DatabaseProperties {
private List<DataSourceFromProperties> datasources;
// getter and setters
当我启动应用程序时,抛出了这个错误:
Failed to bind properties under 'app' to ...DatabaseProperties:
Property: app
Value:
Origin: "app" from property source "class path resource [database.yml]"
Reason: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.boot.context.properties.ConfigurationProperties ...DatabaseProperties]
Action:
Update your application's configuration
似乎在2.0.0-RELEASE
中更改了绑定知道在这种情况下我应该更新/更改什么?
这是pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.156</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.2.6.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
以这种方式解决:
PropertyScource仅适用于.properties文件,因此我将.yml更改为.properties
app.datasources [0] .name = aaa app.datasources [0] .driver = aaa app.datasources [0] .url = aaa app.datasources [0] .username = aaa app.datasources [0] .password = aaa
app.datasources [1] .name = bbb app.datasources [1] .driver = bbb app.datasources [1] .url = bbb app.datasources [1] .username = bbb app.datasources [1] .password = bbb
问题出在数据库属性中
内部类必须是静态的
public class DatabaseProperties{
private List<InnerClass> datasources;
public List<InnerClass> getDatasources() {
return datasources;
}
.
.
.
static class InnerClass{}
}
答案 1 :(得分:0)
@PropertySource
不支持现成的YAML文件。
Marten Deinum汇总了a really great article,演示了如何添加YAML支持。为了简洁起见,这是他的示例的简化版本:
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
Properties propertiesFromYaml = factory.getObject();
return new PropertiesPropertySource(name, propertiesFromYaml);
}
}
有关更完整的代码示例,请参阅他的文章。