XML-based configuration

The XML-based configuration has been the primary configuration technique since Spring started. In this section, we will see the same example as we discussed in the DI pattern, and see how the CustomerService object is injected in the BankingService class through an XML-based configuration.

For an XML-based configuration, we need to create an applicationContext.xml file with a <beans> element. The Spring container must be able to manage one or more beans in the application. Beans are described using the <bean> element inside a top-level <beans> element.

Following is the applicationContext.xml file:

<?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">

<!-- Bean Configuration definition describe here -->
<bean class=""/>

</beans>

The preceding XML file is the basic structure of XML-based configuration metadata where we need to define our bean configuration. As we learned earlier, our bean configuration pattern may be constructor-based or setter-based, depending on the application requirement. Now, we will see how we can configure a bean using both design patterns, one by one.

The following is the example of a constructor-based DI with an XML-based configuration:

<!-- CustomerServiceImpl Bean -->
<bean id="customerService" class="com.packt.springhighperformance.ch2.bankingapp.service.Impl.CustomerServiceImpl" />

<!-- Inject customerService via constructor argument -->
<bean id="bankingService"
class="com.packt.springhighperformance.ch2.bankingapp.model.BankingService">
<constructor-arg ref="customerService" />
</bean>

In the previous example, we have injected the CustomerService object in the BankingServices class with a constructor DI pattern. The ref attribute of the </constructor-arg> element is used to pass the reference of the CustomerServiceImpl object.

The following is the example of a setter-based DI with an XML-based configuration:

<!-- CustomerServiceImpl Bean -->
<bean id="customerService" class="com.packt.springhighperformance.ch2.bankingapp.service.Impl.CustomerServiceImpl" />

<!-- Inject customerService via setter method -->
<bean id="bankingService" class="com.packt.springhighperformance.ch2.bankingapp.model.BankingService">
<property name="customerService" ref="customerService"></property></bean>

The ref attribute of the </property> element is used to pass the reference of the CustomerServiceImpl object to the setter method.

The following is the content of the MainApp.java file:

public class MainApp {

public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
BankingService bankingService =
context.getBean("bankingService",
BankingService.class);
bankingService.showCustomerAccountBalance();
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset