Springs - SPLessons

Chapter 11

Spring XML Configuration

Spring XML Configuration

Spring XML Configuration, XML was the first available method and it is a better way to configure easily in Spring framework. Spring setup records contain bean definitions and other data. They are utilized to give context data to the Spring structure. XML file by default consists a sequence of code and syntax as follows. It is helpful to include configuration file header, which describes the properties indicated in the configuration files. Here description tag is helpful to add comments and it will be identified by tools to pick up the description easily.

<beans>
    <description>
        This configuration file will have all beans 
        which may be used for controlling transactions.
    </description>
    ...
</beans>

No need to specify version of schema: 
general configuration file as explained below

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

This can be written as follows

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

Use Setter injection over Constructor injection
It’s better to use setter injection because individual dependency of injection is possible here. For instance, take three variables like int, float, char. then no need to mention values of those variables. By default it takes the values.
But above process is not available in constructor injection.Constructor injection can not override the setter properties but setter can do this.

<!-- Constructor injection -->
<bean id="employeeDAO" class="splessons.dao.EmployeeDAO">
    <constructor-arg ref="datasource"/>
</bean>
 
<!-- Setter injection -->
<bean id="employeeDAO" class="splessons.dao.EmployeeDAO">
    <property name="datasource" ref="datasource">
</bean>

Use type over index for constructor argument matching in Constructor injection
Better to avoid constructor injection and prefer to use setter injection for dependency injection. But, if a developer wants to use constructor injection, then use arguments matching based on type rather than index. Example as follows.

<!-- Index based constructor injection -->
<bean id="employeeDAO" class="splessons.EmployeeDAO">
    <constructor-arg index="0" value="rest"/>
    <constructor-arg index="1" value="8080"/>
</bean>
 
<!-- Type based constructor injection -->
<bean id="employeeDAO" class="splessons.EmployeeDAO">
    <constructor-arg type="java.lang.String" value="rest"/>
    <constructor-arg type="int" value="8080"/>
</bean>

Use shortcut forms over than expanded forms

Spring XML Configuration allows two types of forms to indicate property values are as shortcut and expanded forms as follows.

<!-- Expanded version -->
<bean id="employeeDAO" class="splessons.dao.EmployeeDAO">
    <property name="datasource">
        <ref bean="datasource"></ref>
        <value>datasource</value>
     </property>
</bean>
 
<!-- Shorter/shortcut version -->
<bean id="employeeDAO" class="splessons.dao.EmployeeDAO">
    <property name="datasource" ref="datasource" value="datasource">
</bean>

Reuse bean definitions

<bean id="abstractDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
 
<bean id="concreteDataSourceOne" parent="abstractDataSource" p:url="${jdbc.databaseurlOne}"/>
  
<bean id="concreteDataSourceTwo" parent="abstractDataSource" p:url="${jdbc.databaseurlTwo}"/>

Use id’s as bean identifiers
Spring permits two sorts of identifiers for a bean. Utilizing trait “id” or by “name”.Developer should pick id over the name.
Use class path prefix
While importing elements, XML config, properties. Then prefer the classpath: or classpath*:
prefix. It gives consistency and clarity to the location of the element. This class path is
determined by the IDE.

<!-- Always use classpath: prefix-->
<import resource="classpath:/META-INF/spring/applicationContext-security.xml"/>

Use Dependency check
Bean definition should have the attribute dependency check then the container can do dependency validation inside.

<bean id="abstractDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:username="${jdbc.username}" p:password="${jdbc.password}" dependency-check="all" />

Summary

  • Spring XML Configuration – XML became a W3C Recommendation in 1998.
  • Spring XML Configuration – XML is easy to parse. There are a few well known, lightweight, feature full, and/or free XML parsing libraries available in many languages.