Spring Framework - XML Configuration

Initialize the Spring Container

Initialize Spring container with multiple configuration files

ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"service.xml", "data.xml"});

UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);

Spring Bean configuration file - service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="userService"
        class="com.innotrekker.app.service.UserServiceImpl">
    <property name="userDao" ref="userDao"/>
  </bean>
</beans>

Spring Bean configuration file - data.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="userDao"
      class="com.innotrekker.app.data.UserDao">
  </bean>
</beans>

Spring Construction Based Dependency Injection

Spring POJO - userServiceImpl

public class UserServiceImpl {

  private UserDao userDao;

  public UserServiceImpl(UserDao data) {
      this.userDao = data;
  }
}

Spring Dependency Injection using constructor by passing another Spring Bean

<beans>
  <bean id="userService" class="com.innotrekker.app.service.UserServiceImpl">
      <constructor-arg ref="userDao"/>
  </bean>

  <bean id="userDao" class="com.innotrekker.app.data.UserDao"/>
</beans>

Spring Dependency Injection using constructor and a type

<bean ...>
<constructor-arg type="int" value="10"/>
<constructor-arg type="java.lang.String" value="passed"/>
</bean>

Spring Dependency Injection using constructor and index parameter

<bean ...>
<constructor-arg index="0" value="10"/>
<constructor-arg index="1" value="passed"/>
</bean>

Dependency Injection Using Constructor Parameter Name

<bean ...>
<constructor-arg name="amount" value="10"/>
<constructor-arg name="result" value="passed"/>
</bean>

Or defined the name of the parameter using Annotation

public class UserServiceImpl {
  @ConstructorProperties({"amount", "result"})
  public UserServiceImpl(int p1, String p2) {
      ....
  }
}

Setter Based Spring Dependency Injection

Spring POJO

public class UserServiceImpl {
  private UserDao userDao;

  public void setUserDao(UserDao userDao) {
      this.userDao = userDao;
  }

}

Spring Bean & Value based bean injection

<bean id="userService" class="com.innotrekker.app.service.UserServiceImpl">
  <property name="userDao" ref="userDao"/>
  <property name="amount" value="1"/>
</bean>

Short cut for value based property using "p:"

<bean id="userService" class="com.innotrekker.app.service.UserServiceImpl" 
   p:amount="1"/>

To perform additional deployment time validation using idref

<property name="someProperty">
 <!-- a bean with id 'someTarget' must exist. Otherwise an exception will be thrown -->
 <idref local="someTarget"/>
</property>

Spring Bean Placeholder (Externalize Bean Configuration)

Define a Spring placeholder file and initialize Spring Bean with the configuration file

<context:property-placeholder location="classpath:com/innotrekker/config.properties,classpath:com/innotrekker/config2.properties"/>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:com/innotrekker/config.properties"/>
</bean>

<bean id="mySetting" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="k1" value="${config.v1}"/>
</bean>

config.properties

config.v1=10

Spring Inner Bean Injection

<bean id="..." class="...">
<!-- Embedded a bean within another bean -->
<property name="target">
  <bean class="..."> 
     <property name="name" value="example"/>
  </bean>
</property>
</bean>

Inject Collection Type Property to a Spring Bean (Property, Set, List, Map)

<bean id="myBean" class="...">
<!-- setMyProperty(java.util.Properties) -->
<property name="myProperty">
  <props>
      <prop key="key1">v1</prop>
      <prop key="key2">v2</prop>
  </props>
</property>
<!-- setMyList(java.util.List) -->
<property name="myList">
  <list>
      <value>someValue</value>
      <ref bean="someBean" />
  </list>
</property>
<!-- setMyMap(java.util.Map) -->
<property name="myMap">
  <map>
      <entry key="key1" value="v1"/>
      <entry key ="key2" value-ref="someBean"/>
  </map>
</property>
<!-- setMySet(java.util.Set) -->
<property name="mySet">
  <set>
      <value>v1</value>
      <ref bean="someBean" />
  </set>
</property>
</bean>

Spring Collection Merging

The items in "child" will be merged with "parent"

<beans>
<bean id="parent" abstract="true" class="...">
  <property name="items">
      <props>
          <prop key="k1">v1</prop>
      </props>
  </property>
</bean>
<bean id="child" parent="parent">
  <property name="items">
      <props merge="true">
          <prop key="k2">v2</prop>
      </props>
  </property>
</bean>
<beans>

Auto Wiring

<bean id="myBean" autowire="byName" class="..."/>

Bean injection can be done automatically with autowire using one of the following configuration

Spring Auto Wiring Description
Wire by name Use the property name as the name of a Spring bean id
Wire by Type Wire the property by the Class type. Exception throw if more than 1 bean trying to use auto Wire by constructor Similar to by type applied to the constructor argument

Spring Bean Initialization & Destroy Method

Spring Initialization Method when Spring bean is created

<bean id="myBean" class="com.innotrekker.app.MyBean" init-method="init"/>
public class MyBean {
  public void init() {
      ...
  }
}

Spring Destroy Method when Spring bean is destroyed

<bean id="myBean" class="..." destroy-method="cleanup"/>

Define a project wise Spring init or destroy method

<beans default-init-method="init">
  <bean ...>
  </bean>
</beans>

Spring Bean using Inheritance

Spring parent and child bean

<bean id="parentBean" abstract="true"
    class="...">
  <property name="k1" value="v1"/>
</bean>

<bean id="childBean"
      class="..."
      parent="parentBean">

  <property name="k1" value="override"/>
</bean>

Spring Bean Scoping

Define a Spring Bean Scope

<bean id="..." class="..." scope="request"/>
Scope value Description
singleton (default) One bean with each Spring container
prototype A new instance
request HTTP request scope
session HTTP session
global session Global HTTP session for a global Portlet application

Inject request/session beans

<bean id="userData" class="..." scope="session">
        <!-- Required to inject to another bean for request/session scope be -->
        <aop:scoped-proxy/>
  </bean>

  <bean id="user" class="...">
      <property name="userData" ref="userData"/>
  </bean>

Web Application Configuration

web.xml

<web-app>
...
<listener>
  <listener-class>
      org.springframework.web.context.request.RequestContextListener
  </listener-class>
</listener>
...
</web-app>

Misc

Setting Null value

<property name="v1"><null/></property>

Accessing o1.setP1().setSp1.setSsp1("123)

<property name="p1.sp1.ssp1" value="123" />

Will initialize "thisBean" before all beans defined in "depends-on"

<bean id="thisBean" class="..." depends-on="myBean1,myBean2">
</bean>

Lazy loading a bean when needed instead of Spring Container Startup

<bean id="..." class="..." lazy-init="true"/>