Spring Bean Configuration Examples

  • XML file that can be developed either based on DTD rules or Schema rules. The XMLfile of certain software technology based applications either based on DTD or Schema rules supplied by that technology.
  • Contains Spring Bean configuration to make Spring Containers to recognize Spring.
  • Contains configuration for setter injections and constructor injections.
  • Any filename.xml can be taken as Spring Bean configuration file name and also contains     some misecllanious configurations related to Spring Beans.

Setting the Bean Properties :

public class ExampleBean {

      private String s;
      private int i;

      public void setStringProperty (String s) { this.s = s; }
      public void setIntegerProperty (int i) { this.i = i; }
  }



  <bean id="exampleBean" class="ExampleBean">
       <property name="stringProperty"><value>Hi!</value></property>
       <property name="integerProperty"><value>1</value></property>
  </bean>


Example:
<property name="intProperty"><value>7</value></property>

<property name="doubleProperty"><value>0.25</value></property>

<property name="booleanProperty"><value>true</value></property>
 

<property name="colorProperty"><value>0,255,0</value></property>

java.awt.Color is initialized with RGB values.

Bean Constructor Configuration:

public class ExampleBean {
      private AnotherBean beanOne;
      private YetAnotherBean beanTwo;
      private int i;
      public ExampleBean (AnotherBean b1, YetAnotherBean b2, int i) {
          this.beanOne = b1;
          this.beanTwo = b2;
          this.i = i;
      }
  }

  <bean id="exampleBean" class="ExampleBean">
      <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
      <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
      <constructor-arg><value>1</value></constructor-arg>
  </bean>

  <bean id="anotherExampleBean" class="AnotherBean"/>
  <bean id="yetAnotherBean" class="YetAnotherBean"/>



Using Java.util.Properties Example:

<property name="propertiesProperty">
       <value>
           foo=1
           bar=2
           baz=3
       </value>
  </property>

  <property name="propertiesProperty">
       <props>
           <prop key="foo">1</prop>
           <prop key="bar">2</prop>
           <prop key="baz">3</prop>
       </props>
  </property>


List Example:

<property name="listProperty">
      <list>
           <value>a list element</value>
           <ref bean="otherBean"/>
           <ref bean="anotherBean"/>
      </list>
  </property>


Set Example:
 
  <property name="setProperty">
      <set>
           <value>a set element</value>
           <ref bean="otherBean"/>
           <ref bean="anotherBean"/>
      </set>
  </property> 


Map Example:

<property name="mapProperty">
       <map>
           <entry key="JavaStuff">
                <value>Spring Example</value>
           </entry>
           <entry key="yup a ref">
                <ref bean="otherBean"/>
           </entry>
       </map>
  </property> 


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email