Configuration

You need to give instructions to Spring's container about how you want to configure your beans based on your application needs. These instructions should be in the form of configuration metadata, and they should tell the following things to the IoC container:

  • Instantiation: How to create the objects from bean definitions.
  • Lifespan: Till what time these objects are available.
  • Dependencies: Do they need someone else?

Spring provides a great amount of flexibility, even in defining the configuration metadata. You can supply it to the IoC container in the following three ways:

  • XML format: One or more entries with configuration metadata about beans in Spring's Application Context (XML) file.
  • Java annotation: Put the configuration metadata in the form of an annotation in a Java class.
  • pure Java code: From version 3.0, Spring started support of defining configuration with Java code. You can define beans outside of your application classes by using Java rather than XML files.

When the Spring application starts, it will load the application context (XML) file first. This file looks 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- All your bean and its configuration metadata goes here -->
<bean id="..." class="...">
</bean>
</beans>

This file must be present for both XML-based and annotation-based configuration metadata. In the case of XML-based configuration, you need to define your bean with the <bean> element under top-level <beans> elements in this file. One or multiple <bean> entries can be defined. The configuration metadata will go along with the <bean> element.

In the preceding bean definition, the id attribute defines the identity of that bean. The container will use it to point out the specific bean, so it must be unique. While the class attribute defines the type of bean, you need to give its fully qualified class name here.

Each bean is associated with an actual object through the class attribute. You can define beans for any type of class, such as your custom service layer classes, DAO layer classes, presentation classes, and so on. Spring's container will use the class attribute to instantiate the objects, and it applies the configuration metadata associated with the corresponding <bean> element.

In case of annotation-based configuration, your metadata will be defined to actual Java classes and in this (XML) file; you need to just specify the base package name with the <context:component-scan base-package="org.example"/> element. We will see more on this in an upcoming section, Annotation-based DI, in this chapter.

..................Content has been hidden....................

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