5

I am trying to read properties from application.yml in spring-bean.xml like this:

<bean name="#{bean.name}" />

Is it possible ? or I should specify location of my application.yml file?

2
  • 1
    You are already using Spring Boot which loads those files. Use ${bean.name} instead of #{bean.name}. The latter is a SpEL expression.
    – M. Deinum
    Commented Sep 7, 2017 at 11:41
  • @M.Deinum - Can you please guide here : www.greatytc.com/questions/73784166/…?
    – PAA
    Commented Sep 21, 2022 at 6:59

1 Answer 1

8

Yes It's Possible

For YAML Properties

  1. You have to use YamlPropertiesFactoryBean

    <bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
        <property name="resources" value="classpath:application.yml"/>
    </bean>
    
    <context:property-placeholder properties-ref="yamlProperties"/>
    
  2. Then define your property in src/main/resource/application.yaml

    bean:
       name: foo
    
  3. Now use can use the property in xml to create a bean

    <bean name="${bean.name}"
    class="net.asifhossain.springmvcxml.web.FooBar"/>
    

Here's my complete XML config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
        <property name="resources" value="classpath:application.yaml"/>
    </bean>

    <context:property-placeholder properties-ref="yamlProperties"/>

    <bean name="${bean.name}" class="net.asifhossain.springmvcxml.web.FooBar"/>
</beans>
4
  • Thank you for your answer, but I want read values from YAML, not from *.properties.
    – sanny
    Commented Sep 7, 2017 at 11:50
  • How can we read multiple properties from yaml?
    – PAA
    Commented Sep 20, 2022 at 8:37
  • Can you one please guide here: www.greatytc.com/questions/73784166/…?
    – PAA
    Commented Sep 20, 2022 at 9:05
  • How can we read as a Map from Application.properties
    – Jeff Cook
    Commented Sep 28, 2022 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.