如何在春季启动中从根键读取值



我试图从rootKey读取应用程序配置,但结果为null。

rootKey1:
childKey1: childValue1
childKey2: childValue2
childKey3: childValue3
rootKey2:
childKey1: childValue1
childKey2: childValue2
childKey3: childValue3
Environment environment;
getProperty(String key) {
environment.getProperty(key+".childKey1"); --> is giving childValue1
-------------------
environment.getProperty(key); --> is giving null
}

更正:rootKey是动态的。

尝试定义属性对象结构并注册适当的bean。

配置自定义数据源

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {
@Data
public static class RootProperty {
private List<ChildProperty> list;
}
@Data
public static class ChildProperty {
private String name;
private String childKey1;
private String childKey2;
private String childKey3;
}
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
@ConfigurationProperties("root-key")
public RootProperty rootProperty() {
return new RootProperty();
}
@Override
public void run(ApplicationArguments args) {
System.out.println(context.getBean("rootProperty"));
}
}

application.yml

root-key:
list:
- name: child-key1
childKey1: childValue1-1
childKey2: childValue1-2
childKey3: childValue1-3
- name: child-key2
childKey1: childValue2-1
childKey2: childValue2-2
childKey3: childValue2-3

Springboot默认情况下从/src/main/resources文件路径读取配置文件application.yml

DemoApplication.java

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);

}

}

TestService.java

package com.example.demo.service;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Autowired
private Environment env;
@PostConstruct
public void init() {
System.out.println(env.getProperty("rootKey1.childKey1"));
System.out.println(env.getProperty("rootKey2.childKey1"));
}
}

Application.yml

rootKey1:
childKey1: childValue1
childKey2: childValue2
childKey3: childValue3
rootKey2:
childKey1: childValue1
childKey2: childValue2
childKey3: childValue3

结果:

:: Spring Boot ::        (v2.3.5.RELEASE)
2020-11-01 16:20:04.514  INFO 15400 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on LAPTOP-TN3LBPJ2 with PID 15400 (C:codejavademotargetclasses started by steep in C:codejavademo)
2020-11-01 16:20:04.516  INFO 15400 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-11-01 16:20:05.576  INFO 15400 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-01 16:20:05.590  INFO 15400 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-01 16:20:05.590  INFO 15400 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-11-01 16:20:05.669  INFO 15400 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-01 16:20:05.669  INFO 15400 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1102 ms
childValue1
childValue1

最新更新