Announcement Announcement Module
Collapse
No announcement yet.
How to provide default values for bean if properties file contains null value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to provide default values for bean if properties file contains null value

    Hi,

    How to handle exception at bean creating i. e if setter method is throwing exception I need to catch that exception and provide the default values in xml file.

    <bean id="resolver"
    class="ServiceBrokerResolver">
    <property name="serviceBrokerHosts" value="${serviceBroker.hosts}" />
    <property name="regDomain" value="${ois.domain}" />
    <property name="regServices">
    <list>
    <value>mail</value>
    <value>indexing</value>
    </list>
    </property>
    </bean>

    In the above bean if setter method of serviceBrokerHosts throws exception, because serviceBroker.hosts value is null (values are picked from properties file), so I need to set some default values if value is null.

    Please let me know how can i do this or point me to appropriate document that explains it.

    Thanks,
    Bhanu.

  • #2
    You can use PropertyOverrideConfigurer.
    First you define your properties with the name of the bean and the name of the properties you want to override.

    connection.properties:
    Code:
    resolver.regDomain=some.custom.domain
    Then you define the PropertyOverrideConfigurer:
    Code:
    <context:property-override location="classpath:connection.properties" />
    And then you define your bean with the default values:
    Code:
    <bean id="resolver" class="ServiceBrokerResolver"> 
    	<property name="serviceBrokerHosts" value="myDefaultHost" />
    	<property name="regDomain" value="myDefaultDomain" /> 
    	<property name="regServices">
    		<list>
    			<value>mail</value> 
    			<value>indexing</value> 
    		</list>
    	</property>
    </bean>
    In this example, the property regDomain in the bean resolver will be overriden with the property resolver.regDomain from the properties file, but the property serviceBrokerHosts will remain the same cause there is no resolver.serviceBrokerHosts in the properties file.

    I agree, the way it works is not completely clear in the docs.

    Comment

    Working...
    X