Your first Spring application

Let's start with a very simple Spring application now. This application simply greets the user with a welcome message. Technically, it demonstrates how you configure a Spring ApplicationContext (IoC container) with just a single bean in it and invoke that bean method in your application. The application has four artifacts in it (besides the project build file, of course):

  • GreetingService.java: A Java interface—just a single method
  • GreetingServiceImpl.java: A simple implementation of GreetingService
  • Application.java: Your application with a main method
  • application-context.xml: The Spring configuration file of your application

The following are the service components of your application. The service implementation just prints a greeting message to the logger:

interface GreetingService {
   void greet(String message);
}

public class GreetingServiceImpl implements GreetingService {
   Logger logger = LoggerFactory.getLogger(GreetingService.class);

   public void greet(String message) {
      logger.info("Greetings! " + message);
   }
}

Now let's take a look at the application-context.xml file, which is your Spring configuration file, where you register GreetingService as a Spring bean in the following listing:

<?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">
   <bean id="Greeter"
      class="com.springessentialsbook.chapter1.GreetingServiceImpl">
   </bean>
</beans>

Finally, you invoke the GreetingService.greet() method from your Spring application, as given in the following code:

public class Application {

   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application-context.xml"});
      GreetingService greeter = (GreetingService) context.getBean("Greeter");
     greeter.greet("I am your first Spring bean instance, configured purely with XML metadata. I am resolved by name.");
   }
}

We will explore and conquer the mighty Spring Framework right from this very simple and pretty much self-explanatory application. We will discuss and elaborate the concepts behind this application, and more, in the following sections.

Inversion of Control explained

IoC is a design principle that decouples objects of an object-oriented program from their dependencies (collaborators), that is, the objects they work with. Usually, this decoupling is achieved by externalizing the responsibility of object creation and Dependency Injection to an external component, such as an IoC container.

This concept is often compared to the Hollywood principle, "Don't call us, we will call you." In the programming world, it recommends the main program (or a component) not to instantiate its dependencies by itself but let an assembler do that job.

This immediately decouples the program from the many problems caused by tightly coupled dependencies and relieves the programmer to let them quickly develop their code using abstract dependencies (program to interfaces). Later, at runtime, an external entity, such as an IoC container, resolves their concentrate implementations specified somewhere and injects them at runtime.

You can see this concept implemented in the example we just saw. Your main program (Application.java) is not instantiating the GreetingService dependency; it just asks the ApplicationContext (IoC container) to return an instance. While writing Application.java, the developer doesn't need to think about how the GreetingService interface is actually implemented. The Spring ApplicationContext takes care of object creation and injects any other functionality transparently, keeping the application code clean.

Objects managed by an IoC container do not control the creation and resolution of their dependencies by themselves; rather, that control is inverted by moving it away to the container itself; hence the term "Inversion of Control".

The IoC container assembles the components of the application as specified in the configuration. It handles the life cycles of the managed objects.

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

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