Announcement Announcement Module
Collapse
No announcement yet.
Spring Application Context Configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Application Context Configuration

    Hey guys! I am kinda new to Spring but so far loving it!

    I came to this situation, is it possible to set a bean property value to a constant defined in another bean or class?

    For example if I have bean A and bean B, but bean A could use a property with the value of B.CONSTANT_VALUE and maybe bean B has a property of type integer which I need to configure in the application context xml file as Integer.MAX_VALUE, how can I do that without having to use the real values but just calling the Constant or if possible a method from other bean to set the value?

    I hope it makes sense!


  • #2
    Use "util" namespace:

    Code:
     xmlns:util="http://www.springframework.org/schema/util
    
    	<bean class="com.example.A">
    		<property name="max">
    			<util:constant static-field="java.lang.Integer.MIN_VALUE"/>
    		</property>
    	</bean>

    Comment


    • #3
      Thanks for the info!

      Thank you Andrei! It worked fine but I was just missing to add the xsi:schemaLocation.

      The beans tag definition ended up like this:

      Code:
      <beans
             xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:util="http://www.springframework.org/schema/util"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                                              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

      Comment

      Working...
      X