Springs - SPLessons

Spring XML Configuration

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring XML Configuration

Spring XML Configuration

shape Description

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. [java]<beans> <description> This configuration file will have all beans which may be used for controlling transactions. </description> ... </beans>[/java] No need to specify version of schema:  general configuration file as explained below [java]<?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> [/java] This can be written as follows [java]<?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>[/java] 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. [java]<!-- 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>[/java] 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. [java]<!-- 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>[/java] 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. [java]<!-- 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> [/java] Reuse bean definitions [java]<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}"/> [/java] 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. [java]<!-- Always use classpath: prefix--> <import resource="classpath:/META-INF/spring/applicationContext-security.xml"/> [/java] Use Dependency check Bean definition should have the attribute dependency check then the container can do dependency validation inside. [java]<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" />[/java]

Summary

shape Key Points

  • 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.