Create a Spring Bean Using YAML

综合编程 2018-06-14 阅读原文

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.



    
        
            Rohit
        
        
            jain
        
        
            203428304230
        
        
    
    
        
            gyan nagar
        
        
            sector 4
        
        
            313001
        
        
            Pune
        
        
            Maharastra
        
        
    
    
        
            India
        
        
            +91
        
        
            INR
        
    
#spring-beans

customerImpl:
  class: com.example.yamlbeanExample.Customer
  properties:
    firstName: Rohit
    lastName: jain
    phone: 203428304230
    address: ref::addressImpl
addressImpl:
  class: com.example.yamlbeanExample.Address
  properties:
    address1: gyan nagar
    address2: sector 4
    pinCode: 313001
    city: pune
    state: Maharastra
    country: ref::countryImpl
countryImpl:
  class: com.example.yamlbeanExample.Country
  properties:
    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!

Javalobby

责编内容by:Javalobby阅读原文】。感谢您的支持!

您可能感兴趣的

Spring Webflux – Kotlin DSL Spring Webflux has introduced a feature for defining functional application endpoints using a very intuitive K...
org.springframework.util.StopWatch:简洁的耗时统计小工具... 想知道一个代码块执行耗时多久,通常做法是执行前记录当前时间A,执行后用当前时间减去A就是耗时了。spring库中有个统计耗时的小工具:StopWatch类,它可以帮我们做这些事情,甚至做得更好,咱们直接上代码实战吧: 创建项目 我这...
SSM 框架快速整合实例–学生查询 SSM 框架即 Spring 框架、SpringMVC 框架、MyBatis 框架,关于这几个框架的基础和入门程序,我前面已经写过几篇文章作为基础和入门介绍了。对于这 3 个框架还不熟悉的同学,可以参考一下几篇文章: ...
Spring之RestTemplate中级使用篇 Spring之RestTemplate中级使用篇 前面一篇介绍了如何使用 RestTemplate 发起post和get请求,然而也只能满足一些基本的场景,对于一些特殊的如需要设置请求头,添加认证信息等场景,却没有...
Data Source Spring Data neo4j not filled I have a simple relationship (Account)-->(Identity), defined like this @RelatedTo(type = "IDENTIFIED_BY", direct...