Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 配置属性批注存在问题_Java_Spring_Spring Boot_Spring Cloud_Spring Properties - Fatal编程技术网

Java 配置属性批注存在问题

Java 配置属性批注存在问题,java,spring,spring-boot,spring-cloud,spring-properties,Java,Spring,Spring Boot,Spring Cloud,Spring Properties,我有一个Git配置,spring服务器指向该配置。然后,我创建了一个具有以下配置的客户端。。以及要填充到properties对象中的预期属性。由于某些原因,它不适用于Spring云配置。但它适用于application.yml或application.properties。我可以看到酒吧里的人都很正常。。但不是属性 有什么想法吗 @SpringBootApplication @EnableAutoConfiguration @RestController @RefreshScope @Compo

我有一个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;
    }

不要以这种方式将
@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;
    }
}

按照您的设置方式,将进入
properties
var的属性需要以
datasource.mysql.jpa.properties.*
作为前缀。我没有明白。您的意思是说@ConfigurationProperties(prefix=“datasource.mysql.jpa”)更改为@ConfigurationProperties(prefix=“datasource.mysql.jpa.properties.*)我就是这么说的。正如上面的代码所示,这是它工作的唯一方式。需要
.properties
,因为类中的变量名为
properties
。感谢您的回复。我只是映射了要验证的值,是属性问题还是值问题。。我同意了。我的实际代码没有值注释。但这仍然不起作用。。有财产。。它像任何东西一样工作,而不是与属性对象。。这就是为什么我感到困惑。顺便说一句当我加载属性时,它工作正常。。但当我将它与云配置服务器连接时,它就不起作用了。