2

我有一个 Git 配置,spring 服务器指向该配置。然后我创建了一个具有以下配置的客户端.. 并期望在 Properties 对象中填充属性。某些原因它不适用于 Spring 云配置..但它确实适用于 application.yml 或 application.properties。我可以看到正确填充的栏..但不是属性。

任何想法?

@SpringBootApplication
@EnableAutoConfiguration
@RestController
@RefreshScope
@ComponentScan
@EnableConfigurationProperties
@ConfigurationProperties(prefix="datasource.mysql.jpa")

public class ConfigClientApplication {

    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    @Value("${datasource.mysql.jpa.hibernate.dialect:sss}")
    private String bar;
    @RequestMapping("/")
    public String home() {
        String a ="";
        try {
            a = (String)properties.get("datasource.mysql.jpa.hibernate.dialect");
        }catch (Exception e){
            a = e.getMessage();
        }
        return "Hello "+bar + a;
    }
4

1 回答 1

1

不要将@Value注释与@ConfigurationProperties这种方式结合起来。我很幸运地将属性组映射到内部类,如下所示:

test1.property1=...
test1.test2.property2=...
test1.test2.property3=...

将映射为:

import javax.validation.constraints.NotNull;

import lombok.Getter;
import lombok.Setter;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Getter
@Setter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(locations = "classpath:myapp.properties")
public class ApplicationProperties {

    private String property1;
    private Test2 test2;

    @Getter
    @Setter
    @ConfigurationProperties(prefix = "test2")
    public static class Test2 {
        @NotNull
        private String property2;
        @NotNull
        private String property3;
    }
}
于 2015-12-10T20:09:39.623 回答