Tomcat:9 JDK:OpenJDK8(OPENJ9) spring-boot:2.0.5
This is the configuration file you want to read ↓ ↓ ↓
[Project root]/src/main/resources/test.yml
value:
item1:
aaa: 12345
bbb: 22345
item2:
dog: poti
cat: tama
Java description is this ↓ ↓ ↓
xxxxx.java
DefaultResourceLoader resourceLoader;
InputStreamReader reader;
try {
resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:test.yml");
reader = new InputStreamReader(resource.getInputStream());
Yaml yaml = new Yaml();
map = (Map<String, Object>) yaml.load(reader);
System.out.println(ValueMap.toString());
} catch (Exception e) {
e.printStackTrace();
}
I thought I should use ResourceLoader, and initially
@Autowired
ResourceLoader resourceLoader;
I tried the declaration, but it failed with NullPointerException, probably because the Autowired annotation is not working. So, I got it by using the DefaultResourceLoader class, which is a wicked or brute force method.
`* This process worked in eclipse in the development environment, but when I fixed it in War and placed it in Tomcat, it stopped working probably because the directory structure changed. ``
This is the configuration file you want to read ↓ ↓ ↓
[Project root]/src/main/resources/test.yml
value:
item1:
aaa: 12345
bbb: 22345
item2:
dog: poti
cat: tama
Java description is this ↓ ↓ ↓
xxxxx.java
InputStreamReader reader;
try {
reader = new InputStreamReader(ClassLoader.getSystemResourceAsStream("test.yml"));
Yaml yaml = new Yaml();
Map<String, String> ValueMap = (Map<String, String>) yaml.load(reader);
System.out.println(ValueMap.toString());
} catch (Exception e) {
e.printStackTrace();
}
The contents set in ValueMap look like this ↓ ↓ ↓
I was able to get it like that.
I was able to get it in the above form, so when I thought "Which war should I use?"
Recommended Posts