2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

snakeyamlでYAMLをbeanにロードする

Last updated at Posted at 2014-10-01

アプリケーションの設定ファイルにプロパティファイルではなくYAMLを使いたくなったので。
注意点としては、Beanをinner classにするとClass Not Foundになること。

conf.yml
--- !!Bean
name: ABC
bean
public class Bean {
  private String name;

  public Bean() {
  }

  public String getName() {
    return name;
  }

  public void setName(String Name) {
    this.name = Name;
  }
}
loader
import java.io.InputStream;
import org.yaml.snakeyaml.Yaml;

public class Loader {
  public Bean load() {
    Yaml yaml = new Yaml();
    InputStream is = getClass().getResourceAsStream("/conf.yml");
    Yaml yaml = new Yaml();
    return (Bean)yaml.load(is);
  }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?