Book Image

Spring Essentials

Book Image

Spring Essentials

Overview of this book

Spring is an open source Java application development framework to build and deploy systems and applications that run on the JVM. It is the industry standard and the most popular framework among Java developers with over two-thirds of developers using it. Spring Essentials makes learning Spring so much quicker and easier with the help of illustrations and practical examples. Starting from the core concepts of features such as inversion of Control Container and BeanFactory, we move on to a detailed look at aspect-oriented programming. We cover the breadth and depth of Spring MVC, the WebSocket technology, Spring Data, and Spring Security with various authentication and authorization mechanisms. Packed with real-world examples, you’ll get an insight into utilizing the power of Spring Expression Language in your applications for higher maintainability. You’ll also develop full-duplex real-time communication channels using WebSocket and integrate Spring with web technologies such as JSF, Struts 2, and Tapestry. At the tail end, you will build a modern SPA using EmberJS at the front end and a Spring MVC-based API at the back end.By the end of the book, you will be able to develop your own dull-fledged applications with Spring.
Table of Contents (14 chapters)
Spring Essentials
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

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.