Results 1 to 7 of 7

Thread: Spring to understand properties in YAML

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow Spring to understand properties in YAML

    YAML makes a lot of sense for properties, and I am using it currently on the project, but have difficulties to inject properties in a

    Code:
    <property name="productName" value="${client.product.name}" />
    fashion.

    Anything I am missing, or I should create a custom YamlPropertyPlaceholderConfigurer ?

    Thank you,
    /Anatoly
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    What sort of difficulties are you facing? Seems that you're using the PropertyPlaceholder configurer which reads a java.util.Properties object. YAML is used for serialization so as long as the deserialization creates a valid entry inside the source Properties, things should work.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow

    @Costin,

    Thank you for replying. Let me try to me more specific on what I would like to achieve. Let's say I have this datasource YAML config:

    datasource.yml:
    Code:
        datasource:
            url: jdbc:hsqldb:file:hsqldb/sample
            username: sa
            password:
            configuration:
                driver: org.hsqldb.blabla
    I would like to be able to use "property-placeholder":

    Code:
    <context:property-placeholder location="classpath:META-INF/env/props/datasource.yml"/>
    and I would like to access properties in a normal "property-placeholder" way:

    Code:
    <bean id="datasource" class="...">
        <property name="url" value="${datasource.url}"/>
        <property name="username" value="${datasource.username}"/>
        .... .... ....
    </bean>
    Thank you,
    /Anatoly
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    There is no YAML reader out of the box in Spring. Potentially the JSON/YAML Java libraries out there allow these types of files to be read into properties in which case you could plug this into PPC.
    I would recommend this approach since all you need to do is change the way the Properties file is stored rather then the PPC itself (i.e. YamlPropertyPlaceholderConfigurer).
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Apr 2008
    Location
    Philadelphia, US
    Posts
    198

    Arrow

    @Costin,

    I understand it is beyond a Spring scope, but would you know whether there are any Java library that is able to convert YAML to properties / XML? I looked at snakeyaml, and others, but they do not have such capabilities.

    Again, I understand this is not a Spring related question, but once we have an answer, the Spring will kick in immediately

    Would appreciate any help on this. Thank you,
    /Anatoly
    Humans are stateful and mutable beings that have no problems processing many things concurrently and share state with others + they are usually "coupled"

  6. #6
    Join Date
    Oct 2009
    Posts
    55

    Default

    I second the need for a YAML PPC. It's possible the Grails folks have already produced code to make this possible.

  7. #7
    Join Date
    Nov 2004
    Location
    San Diego, CA
    Posts
    19

    Default

    There's no need to implement YamlPropertyPlaceholderConfigurer. I posted article to my dev blog explaining how to read Yaml properties into Spring here http://goo.gl/qg0Oi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •