- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序已将嵌套属性存储在 application.yml 文件中。
我想在应用程序启动时将这些属性映射到 POJO。
Application.yml:
demo:
- A:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
- B:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
为了实现这一点,我使用以下注释:
@配置
@EnableConfigurationProperties
@ConfigurationProperties("演示")
类演示:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("demo")
public class Demo {
@JsonProperty("A")
private List<A> a = null;
@JsonProperty("B")
private List<B> b = null;
@JsonProperty("A")
public List<A> getA() {
return a;
}
@JsonProperty("A")
public void setA(List<A> a) {
this.a = a;
}
@JsonProperty("B")
public List<B> getB() {
return b;
}
@JsonProperty("B")
public void setB(List<B> b) {
this.b = b;
}
@Override
public String toString() {
return "Demo [a=" + a + ", b=" + b + "]";
}
}
A类:
public class A {
@JsonProperty("type")
private String type;
@JsonProperty("prop1")
private Integer prop1;
@JsonProperty("prop2")
private Integer prop2;
@JsonProperty("proop3")
private Integer proop3;
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@JsonProperty("prop1")
public Integer getProp1() {
return prop1;
}
@JsonProperty("prop1")
public void setProp1(Integer prop1) {
this.prop1 = prop1;
}
@JsonProperty("prop2")
public Integer getProp2() {
return prop2;
}
@JsonProperty("prop2")
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
@JsonProperty("proop3")
public Integer getProop3() {
return proop3;
}
@JsonProperty("proop3")
public void setProop3(Integer proop3) {
this.proop3 = proop3;
}
@Override
public String toString() {
return "A [type=" + type + ", prop1=" + prop1 + ", prop2=" + prop2 + ", proop3=" + proop3 + "]";
}
}
B类
public class B {
@JsonProperty("type")
private String type;
@JsonProperty("prop1")
private Integer prop1;
@JsonProperty("prop2")
private Integer prop2;
@JsonProperty("proop3")
private Integer proop3;
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@JsonProperty("prop1")
public Integer getProp1() {
return prop1;
}
@JsonProperty("prop1")
public void setProp1(Integer prop1) {
this.prop1 = prop1;
}
@JsonProperty("prop2")
public Integer getProp2() {
return prop2;
}
@JsonProperty("prop2")
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
@JsonProperty("proop3")
public Integer getProop3() {
return proop3;
}
@JsonProperty("proop3")
public void setProop3(Integer proop3) {
this.proop3 = proop3;
}
@Override
public String toString() {
return "B [type=" + type + ", prop1=" + prop1 + ", prop2=" + prop2 + ", proop3=" + proop3 + "]";
}
}
主类
@SpringBootApplication
@ComponentScan(basePackages = {"com.microservice.*"})
@EnableJpaRepositories("com.microservice.*")
@EntityScan("com.microservice.*")
public class MainApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MainApplication.class);
app.run();
System.out.println("step 1");
Demo config = new Demo();
System.out.println("name: " + config);
}
public void run(String... args) throws Exception {
System.out.println("step 2");
}
}
但我低于o/p:
第 1 步
名称:演示 [a=null, b=null]
最佳答案
当您手动创建属性 POJO 的实例时,Spring 不知道它,并且属性绑定(bind)不会发生。
SpringApplication app = new SpringApplication(MainApplication.class);
app.run();
System.out.println("step 1");
Demo config = new Demo(); // Not a Spring managed bean!
System.out.println("name: " + config);
}
您可以将 Demo
作为一个 bean,而不是使用 @EnableConfigurationProperties
注释配置,如 Type-safe Configuration Properties 所示。 .
@Component
@ConfigurationProperties("demo")
public class Demo {
...
}
然后你可以从上下文中获取Demo
bean:
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
Demo demo = (Demo) context.getBean("demo");
System.out.println(demo.getName());
}
}
UPD:“a”和“b”之前不能有连字符:
demo:
a:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
b:
- type: B
prop1: 1
prop2: 2
proop3: 3
UPD2:回复评论。您可以使用 ObjectMapper
从 Demo
bean 构建 JSON:
@SpringBootApplication
public class MainApplication {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
public static void main(String[] args) throws JsonProcessingException {
...
ObjectMapper objectMapper = (ObjectMapper) context.getBean("objectMapper");
System.out.println(objectMapper.writeValueAsString(demo));
}
}
使用 spring-boot-starter-web
不需要额外的依赖项。否则,您可以添加 jackson :
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.1</version>
</dependency>
关于java - 如何从 spring-boot 应用程序中的 application.yml 文件读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58047778/
我已经成功创建了我的第一个 django 项目。 我的项目 foo 和 foobar 中有两个应用程序。 我在每个应用程序文件夹中创建了一个名为“fixtures”的文件夹。我没有在我的setting
我遵循了 cap + nginx + unicorn 上的文档,但在理解如何正确进行数据库部署时遇到了一些问题。 /config/database.yml 不应该在 git repo 中(最好) 在/
GitLab server can't start .原因很可能是 gitlab.yml 配置文件不正确。 用什么工具检查yml语法是否正确? 我试过 Notepad++ 和 SublimeText,
我们有一个站点范围的 config.yml 文件,它联系 api key 等... 我的另一个 YML 文件能否访问 config.yml 中的值? 配置文件: development: th
我正在尝试在我的第一个测试应用程序中设置用户和安全管理,但我已经有点迷失了,不知道什么是做什么的。 到目前为止我的设置:Symfony 2.5、SonataUserBundle(以及 FOSUserB
我需要在 application.yml 中使用 yaml anchor 引用和字符串连接对于 Spring Boot 应用程序。动机是重用现有配置而不是复制它们。例如,我们有以下 applicati
我需要将单个 YML 文件拆分为多个 YML 文件: 微服务.yml: #------------------------------------------------------------- #
在我的 Symfony2 config.yml 文件中,我想导入一些我希望收集在单独的 yml 文件中的配置。 我用过: imports: - { resource: parameters.yml }
我是新的 docker 用户。在不同的手册中,我通常发现 docker-compose.yml 文件用于描述 docker 作业,但在 docker 站点上为此目标使用了 docker-stack.y
我不明白这两种在 Symfony2 中设置全局常量的方法之间的区别。是否只能在 config.yml (+configuration.php) 中设置默认值和类型? 最佳答案 参数.yml 文件是所有
在 config.yml 中,我看到了 monolog、web_profiler 等根元素。那些服务可以在 service.yml 中配置吗?换句话说,我在 service.yml 中定义的服务是否可
我阅读了这个文档:https://serverless.com/framework/docs/providers/google/guide/services/ users/ serverless.
我正在从事 CI/CD 项目(使用 circleci 管道),目前,我坚持让我的“create_infrastructure”工作正常工作。下面是作业 # AWS infrastructure
编辑:原始标题“文本环境:”平台“sqlite”不支持函数“year”” 将 beberlei\DoctrineExtensions 合并到测试环境中会产生 Uncaught PHP Exceptio
abc.yml: d_lab: 192.168.1.1 d_location: /ephemeral ema: apple: 10.0.0.1 orange: 10.0.0.2
我的 springboot 应用程序有一些 yml 文件(每个文件用于各种配置文件 - dev、prod)来加载配置。我正在将配置移至数据库。 示例配置如下: admin: id: user05
我有一个大型剧本,它使用多个角色来设置新服务器。我想重新使用剧本,但为了退役阶段而不是调用 role_name/tasks/main.yml 并有很多 when: 语句,我想要告诉 Ansible 调
使用 bookdown 创作文档时,我知道有四个选项可以放置配置选项: _bookdown.yml _output.yml 第一个 .Rmd 的 yaml header 文档 传递给 bookdown
我们希望将我们的 azure pipeline build .ymls 模块化。对于完整的应用程序构建,将包括不同组件的构建 .yml 到主 .yml 中,将它们全部构建在一起。对于单独的组件,我们将
假设我的网站上有一个简单的传统联系表单,我希望它在发送电子邮件时在开发环境中使用主题“Test: (subject_field value)”,在生产环境中使用“(subject_field_valu
我是一名优秀的程序员,十分优秀!