Over a million developers have joined DZone.

Create a Spring Bean Using YAML

DZone's Guide to

Create a Spring Bean Using YAML

In this quick article, we give you a look at how XML and YAML can be used to portray the same data, and how to programmatically make YAML and XML files.

· Java Zone ·
Free Resource

Bring content to any platform with the open-source BloomReach CMS. Try for free.

Spring is a highly adopted annotation-based configuration and it is happily accepted by developer communities. And why not? No one wants to struggle with XML tags, but that's not enough when there is no XML configuration or the external file configuration is not as powerful as an XML configuration would be; especially when we write an application that has to alter behaviors without compilation.

But still, writing an XML configuration is not readable and not easily understood by beginners, so I've written experimental plugins for Spring Boot that convert YAML definitions to Spring Beans.

Here is an example of a bean definition in XML and the bean definition in YAML.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

    <bean id="customerImpl" class="com.example.yamlbeanExample.Customer">
        <property name="firstName">
        <property name="lastName">
        <property name="phone">
        <property name="address" ref="addressImpl"></property>
    <bean id="addressImpl" class="com.example.yamlbeanExample.Address">
        <property name="address1">
            <value>gyan nagar</value>
        <property name="address2">
            <value>sector 4</value>
        <property name="pinCode">
        <property name="city">
        <property name="state">
        <property name="country" ref="countryImpl"></property>
    <bean id="countryImpl" class="com.example.yamlbeanExample.Country">
        <property name="countryName">
        <property name="countryCode">
        <property name="currency">

  class: com.example.yamlbeanExample.Customer
    firstName: Rohit
    lastName: jain
    phone: 203428304230
    address: ref::addressImpl
  class: com.example.yamlbeanExample.Address
    address1: gyan nagar
    address2: sector 4
    pinCode: 313001
    city: pune
    state: Maharastra
    country: ref::countryImpl
  class: com.example.yamlbeanExample.Country
    countryName: India
    countryCode: 91
    currency: INR

You can check out a project on GitHub.

Note ->  YAML-spring-beans is a just experimental library, there are lots of features not included. Feedback will be much appreciated!

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

spring 4.1 ,yaml ,spring beans ,java ,spring boot ,xml

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}