4 回答
TA贡献1853条经验 获得超9个赞
当您手动创建属性 POJO 的实例时,Spring 不知道它,并且属性绑定不会发生。
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);
}
@EnableConfigurationProperties您可以创建一个 bean,而不是使用 注释配置Demo,如类型安全配置属性中所示。
@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:回答评论。Demo您可以使用以下方法从 bean构建 JSON ObjectMapper:
@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>
TA贡献1860条经验 获得超9个赞
您的文件中的内容不正确。您yml可以在此处阅读链接上的Merging YAML lists参考。
我写了一个演示,它可以工作。
demo:
a:
b:
prop1: prop1
prop2: prop2
blist:
- prop1: prop1
prop2: prop2
alist:
- b:
prop1: prop1
prop2: prop2
blist:
- prop1: prop1
prop2: prop2
- b:
prop1: prop1
prop2: prop2
``
@ConfigurationProperties(prefix = "demo")
public class Demo {
private A a;
private List<A> alist;
// omitted getter/setter
}
``
public class A {
private B b;
private List<B> blist;
// omitted getter/setter
}
``
public class B {
private String prop1;
private String prop2;
// omitted getter/setter
}
TA贡献1877条经验 获得超6个赞
如果您想从.yml或.properties文件读取属性,我建议您创建一个类,可以PropertiesBooter在其中保存从这些文件检索到的所有值。要从属性文件中检索值,您可以编写
@Value("${value}")
private String
TA贡献1808条经验 获得超4个赞
从 Mikhail 的回答中,只需使用 Jackson ObjectMapper 编写 json,即可获得 json 格式:
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(YamlTestApplication.class, args);
Demo demo = (Demo) context.getBean("demo");
System.out.println("name: " + demo);
ObjectMapper mapper = new ObjectMapper();
try {
String test = mapper.writeValueAsString(demo);
System.out.println("json: "+test);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
输出:
name: Demo [a=[A [type=A, prop1=1, prop2=2, proop3=3], A [type=B, prop1=1, prop2=2, proop3=3]], b=[B [type=A, prop1=1, prop2=2, proop3=3], B [type=B, prop1=1, prop2=2, proop3=3]]]
json: {"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}]}
添加回答
举报