dineshonjava

Difference Between get() and load() Methods in Hibernate

In hibernate session there are two methods for retrieving object from database one is get() and other load() method. These two methods have been used in the different situations but both are from Session interface and we will call them as session.get() & session.load(). This is one of the famous hibernate interview questions. Let's see differences between them with example.
Sample Example Hibernate Application for get() and load() methods:
Consider a Employee class having 3 properties empid, empName, address.
Popular Spring Tutorials
  1. Spring Tutorial
  2. Spring MVC Web Tutorial
  3. Spring Boot Tutorial
  4. Spring JDBC Tutorial
  5. Spring AOP Tutorial
  6. Spring Security Tutorial
Example of session.get():
package com.dineshonjava.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import com.sdnext.hibernate.tutorial.dto.Employee;


public class HibernateTestDemo {
 
 private static SessionFactory sessionFactory = createSessionFactory();
 /**
  * @param args
  */
 public static void main(String[] args) 
 {
  Employee employee = null;
  Session session = sessionFactory.openSession();
  Transaction transaction = session.beginTransaction();
  transaction.begin();
  employee = (Employee) session.get(Employee.class, 1);
                //get employee object with id 1 
  System.out.println(employee);
                //update employee address of retrieved object, it immediately update to DB because of get method retrieved actual persistent object        
  employee.setAddress("Noida");
                //Here we have updated object  
  System.out.println(employee);

  transaction.commit();
  session.close();
  sessionFactory.close();
 }
 
 private static SessionFactory createSessionFactory() {
     if (sessionFactory == null) {
         StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
         Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();
         sessionFactory = metaData.getSessionFactoryBuilder().build();
     }
     return sessionFactory;
 }
}

Output on console:
......................................
.................................
Jan 31, 2017 10:42:58 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Jan 31, 2017 10:42:58 PM org.hibernate.boot.internal.SessionFactoryBuilderImpl$SessionFactoryOptionsStateStandardImpl 
WARN: Unrecognized hbm2ddl_auto value : create | update.  Supported values include create, create-drop, update, and validate.  Ignoring
Hibernate: select employee0_.EMPID as EMPID1_0_0_, employee0_.ADDRESS as ADDRESS2_0_0_, employee0_.EMP_NAME as EMP_NAME3_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?
Employee [empid=1, empname=Dinesh Rajput, address=New Delhi]
Employee [empid=1, empname=Dinesh Rajput, address=Noida]
Hibernate: update EMPLOYEE set ADDRESS=?, EMP_NAME=? where EMPID=?
......................
......................

Explanation:
Here when we call session.get() method hibernate will hit the database and returns the original object [ row ], that’s the reason it was generated a query when we update any value of this object. If suppose object of given id does not exist in data base then it return null instead throwing any exception let's see as below console output.
......................................
.................................
WARN: Unrecognized hbm2ddl_auto value : create | update.  Supported values include create, create-drop, update, and validate.  Ignoring
Hibernate: select employee0_.EMPID as EMPID1_0_0_, employee0_.ADDRESS as ADDRESS2_0_0_, employee0_.EMP_NAME as EMP_NAME3_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?
null
......................
......................

Example of session.load():

package com.dineshonjava.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import com.sdnext.hibernate.tutorial.dto.Employee;


public class HibernateTestDemo {
 
 private static SessionFactory sessionFactory = createSessionFactory();
 /**
  * @param args
  */
 public static void main(String[] args) 
 {
  Employee employee = null;
  Session session = sessionFactory.openSession();
  Transaction transaction = session.beginTransaction();
  transaction.begin();
  employee = (Employee) session.load(Employee.class, 1);
                //get employee object with id 1 
  System.out.println(employee);
                //if object found then update employee address of retrieved the proxy object instead of original object i.e. it does not update to DB         
  employee.setAddress("Noida");
                transaction.commit();
  session.close();
  sessionFactory.close();
 }
 
 private static SessionFactory createSessionFactory() {
     if (sessionFactory == null) {
         StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
         Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();
         sessionFactory = metaData.getSessionFactoryBuilder().build();
     }
     return sessionFactory;
 }
}

Output on console:
......................................
.................................
WARN: Unrecognized hbm2ddl_auto value : create | update.  Supported values include create, create-drop, update, and validate.  Ignoring
Hibernate: select employee0_.EMPID as EMPID1_0_0_, employee0_.ADDRESS as ADDRESS2_0_0_, employee0_.EMP_NAME as EMP_NAME3_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?
Employee [empid=1, empname=Dinesh Rajput, address=Noida]
......................
......................

Explanation:
When you call session.load() method, it will always return a "proxy" object, Proxy means, hibernate will prepare some fake object with given identifier value in the memory without hitting the database, for example if we call session.load(Employee.class, 1) then hibernate will create one fake Employee object [row] in the memory with id 1, when we made change to property of object it didn't update database because of proxy object. So finally we came to know that session.load() will hit the database only when we start retrieving the object (row) values. In case of given id object doesn't exist in DB then this method will throw an Exception as below.
......................................
.................................
WARN: Unrecognized hbm2ddl_auto value : create | update.  Supported values include create, create-drop, update, and validate.  Ignoring
Hibernate: select employee0_.EMPID as EMPID1_0_0_, employee0_.ADDRESS as ADDRESS2_0_0_, employee0_.EMP_NAME as EMP_NAME3_0_0_ from EMPLOYEE employee0_ where employee0_.EMPID=?
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.sdnext.hibernate.tutorial.dto.Employee#4]
 at org.hibernate.boot.internal.StandardEntityNotFoundDelegate.handleEntityNotFound(StandardEntityNotFoundDelegate.java:28)
 at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:242)
 at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:159)
 at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:266)
 at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:68)
 at com.sdnext.hibernate.tutorial.dto.Employee_$$_jvstc72_0.toString(Employee_$$_jvstc72_0.java)
 at java.lang.String.valueOf(Unknown Source)
 at java.io.PrintStream.println(Unknown Source)
 at com.sdnext.hibernate.tutorial.HibernateTestDemo.main(HibernateTestDemo.java:34)
......................
......................

After discussion of this above example there are following difference we found as pointed below.

Session.load():

  • It will always return a “proxy” without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. 
  • load() method doesn't hit the database.
  • If no row found , it will throws an ObjectNotFoundException.


Session.get():

  • It always hit the database and return the real object, an object that represent the database row, not proxy.
  • If no row found , it return null.
  • get() method always hit the database.
  • It returns real object not proxy.



Difference Between Merge And Update Methods In Hibernate

As we know that update() and merge() methods in hibernate are used to convert the object which is in detached state into persistence state. But there are different situation where we should be used update() and where should be used merge() method in hibernate, let us see below snippet of codes.


Employee emp1 = new Employee();
emp1.setEmpId(100);
emp1.setEmpName("Dinesh");
//create session
Session session1 = createNewHibernateSession();
session1.saveOrUpdate(emp1);
session1.close();
//emp1 object in detached state now

emp1.setEmpName("Dinesh Rajput");//Updated Name
//Create session again
Session session2 = createNewHibernateSession();
Employee emp2 =(Employee)session2.get(Employee.class, 100);
//emp2 object in persistent state with id 100

//below we try to make on detached object with id 100 to persistent state by using update method of hibernate
session2.update(emp1);//It occurs the exception NonUniqueObjectException because emp2 object is having employee with same empid as 100. In order //to avoid this exception we are using merge like given below instead of session.update(emp1);

session2.merge(emp1); //it merge the object state with emp2
session2.update(emp1); //Now it will work with exception

In the hibernate session we can maintain only one employee object in persistent state with same primary key, while converting a detached object into persistent, if already that session has a persistent object with the same primary key then hibernate throws an Exception whenever update() method is called to reattach a detached object with a session. In this case we need to call merge() method instead of update() so that hibernate copies the state changes from detached object into persistent object and we can say a detached object is converted into a persistent object.
Difference Between Merge And Update Methods In Hibernate

Hibernate handles persisting any changes to objects in the session when the session is flushed. update can fail if an instance of the object is already in the session. Merge should be used in that case. It merges the changes of the detached object with an object in the session, if it exists.

Update: Suppose we are dealing with any employee object in the same session then we should use update() or saveOrUpdate() method.

Update: if you are sure that the session does not contains an already persistent instance with the same identifier,then use update to save the data in hibernate

Merge: Suppose we are creating a session and load an employee object. Now object in session cache. If we close the session at this point and we edit state of object and tried to save using update() it will throw exception. To make object persistent we need to open another session. Now we load same object again in current session. So if we want to update present object with previous object changes we have to use merge() method. Merge method will merge changes of both states of object and will save in database.

Merge: if you want to save your modifications at any time with out knowing about the state of an session, then use merge() in hibernate.




Difference between ServletConfig and ServletContext in Java Servlet

Difference between ServletConfig and ServletContext in Java Servlet is popular question in java interview. Let see following are differences.

No. of web applications = That many number of ServletContext objects

No. of servlet classes = That many number of ServletConfig objects.

Usage of ServletContext Interface

There can be a lot of usage of ServletContext object. Some of them are as follows:
  • The object of ServletContext provides an interface between the container and servlet.
  • The ServletContext object can be used to get configuration information from the web.xml file.
  • The ServletContext object can be used to set, get or remove attribute from the web.xml file.
  • The ServletContext object can be used to provide inter-application communication.
  • ServletContext object will be available even before giving the first request
  • In web.xml – <context-param> tag will be appear under <web-app> tag


Usage of ServletConfig Interface

  • ServletConfig is implemented by the servlet container to initialize a single servlet using init(). That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class.
  • An object of ServletConfig is created by the web container for each servlet. This object can be used to get configuration information from web.xml file.
  • If the configuration information is modified from the web.xml file, we don't need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time.
  • We should give request explicitly, in order to create ServletConfig object for the first time
  • In web.xml – <init-param> tag will be appear under <servlet-class> tag



Difference between save vs persist methods in Hibernate

Whenever we are using ORM like Hibernate, there are two from many others methods save() and persist() using to save object to database. But these two methods have the differences in functionality. And also difference between hibernate save() and persist() methods is depends on generator class we are using. There are following few differences as below.

1. Suppose we are using generator class to assign the primary key to persisting object, then there is no difference between these two methods because in generate class programmer we need to give the primary key value to save in the database.


2. persist() is supported by JPA, while save() is only supported by Hibernate.
Popular Spring Tutorials
  1. Spring Tutorial
  2. Spring MVC Web Tutorial
  3. Spring Boot Tutorial
  4. Spring JDBC Tutorial
  5. Spring AOP Tutorial
  6. Spring Security Tutorial


3. Main difference between save and persist is there return type. save () method return Serializable object but persist() method return void.

Serializable pk = session.save(object);

4. Another difference between save and persist method in Hibernate is behavior on outside of transaction boundaries. There is no boundary of transaction for save() method because it returns an identifier so that an insert query is executed immediately to get the identifier. But persist() method will not execute an insert query if it is called outside of transaction boundaries.

5. Another difference between persist and save is that both methods make a transient object to persistent state. However, persist() method doesn’t guarantee that the identifier value will be assigned to the persistent state immediately, the assignment might happen at flush time.



Spring Mobile Tutorial

In this Spring Mobile tutorial we will discuss about how to spring work into web environment to detecting device.
Table of Contents
  1. Introduction
  2. Maven Dependency
  3. Device Module
  4. Device Resolution Classes 
    1. DeviceResolver 
    2. Device
    3. DeviceResolverHandlerInterceptor
    4. DeviceResolverRequestFilter
    5. DeviceHandlerMethodArgumentResolver
  5. Site Preference Management 
    1. SitePreferenceHandler
    2. SitePreference
    3. SitePreferenceHandlerInterceptor
    4. SitePreferenceRepository
  6. Site Switching
  7. Device Aware View Management
    1. Enabling Device Aware Views
    2. Fallback View Resolution
  8. Sample Example for Spring Mobile Overview
  9. Summary


1. Introduction
Spring community provide another project Spring Mobile as an extensions to Spring MVC for developing mobile web applications. This module provide the server side device detection and provide site preferences to open website according to device resolution.

2. Maven Dependency
For getting this project following is maven dependency to be added to pom file.
<dependency>
    <groupId>org.springframework.mobile</groupId>
    <artifactId>spring-mobile-device</artifactId>
    <version>${org.springframework.mobile-version}</version>
</dependency>

3. Device Module
Spring Mobile provide extension to Spring MVC for device detection. It is useful when any request by mobile deices need to handled differently from requests made by desktop browsers. This supported provided by Spring Module with the help of Device Resolver in the framework. Let see how device resolver works.
4. Device Resolution Framework
This device resolution module is a framework which has collection of interfaces, filters, handler classes and enum to working as device resolver. Lets have look following classes in this module.
4.1 DeviceResolver interface
In Spring Mobile, the DeviceResolver interface defines the API for device resolution:
public interface DeviceResolver {
    Device resolveDevice(HttpServletRequest request);
}

This interface's method return the Device interface object as below
4.2 Device interface
public interface Device {
    boolean isNormal();
    boolean isMobile();
    boolean isTablet();
    DevicePlatform getDevicePlatform();
}

Device resolution is nothing but it is the simple process of introspecting an HTTP request to determine the device that originated the request by analyzing the User-Agent header and other request headers.

In Spring MVC, web applications perform device resolution at the beginning before any request handler is invoked. Then by the help of Device resolver implementation request handlers can obtain the Device instance and by using of it request served differently as desktop request.

Spring Mobile provides by default implementation of DeviceResolver is LiteDeviceResolver. It is used for device resolution. You may plug-in another DeviceResolver implementation by injecting a constructor argument. For activating device resolver Spring Mobile provides an interceptor so initially we have register this interceptor as below.

4.3 DeviceResolverHandlerInterceptor interceptor

XML Configuration
<interceptors>
  <!-- On pre-handle, resolve the device that originated the web request -->
  <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
</interceptors>
Java Configuration
@Bean
public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
return new DeviceResolverHandlerInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(deviceResolverHandlerInterceptor());
}
Alternative to the DeviceResolverHandlerInterceptor, Spring Mobile also provides one servlet filter for support to the device resolver. Configured as below

4.4 DeviceResolverRequestFilter filter

To enable, add the DeviceResolverRequestFilter to your web.xml:
<filter>
  <filter-name>deviceResolverRequestFilter</filter-name>
  <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>

4.5 DeviceHandlerMethodArgumentResolver 
Find the Current Device
There is an Untility class in the Spring Mobile to fetching current device to generate the request.

Device currentDevice = DeviceUtils.getCurrentDevice(servletRequest);

If you'd like to pass the current Device as an argument to one of your @Controller methods, configure a DeviceWebArgumentResolver:

<annotation-driven>
  <argument-resolvers>
    <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
  </argument-resolvers>
</annotation-driven>

You can alternatively configure a DeviceHandlerMethodArgumentResolver using Java-based configuration:

@Bean
public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
    return new DeviceHandlerMethodArgumentResolver();
}

@Override
public void addArgumentResolvers(List argumentResolvers) {
    argumentResolvers.add(deviceHandlerMethodArgumentResolver());
}

You can then inject the Device into your @Controllers as shown below:

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping("/")
    public void home(Device device) {
        if (device.isMobile()) {
            logger.info("Hello mobile user!");
        } else if (device.isTablet()) {
            logger.info("Hello tablet user!");
        } else {
            logger.info("Hello desktop user!");         
        }
    }

}

5. Site Preference Management
This feature called site preference management allows user to set preference (SitePreference) to view a particular site in either "normal", "mobile" or "tablet" mode from a particular device. After device resolution we have to decide site preference either open mobile site or desktop or tablet site.

5.1 SitePreferenceHandler
Following interface provided by the Spring Mobile.

public interface SitePreferenceHandler {

    final String CURRENT_SITE_PREFERENCE_ATTRIBUTE = "currentSitePreference";
    
    SitePreference handleSitePreference(HttpServletRequest request, HttpServletResponse response);

}
Above interface has one method handleSitePreference with request and response parameters to decide SitePreference, it is actually an enum.

5.2 SitePreference enum
public enum SitePreference {
//...
}

To selecting site preference as below

Site: <a href="${currentUrl}?site_preference=normal">Normal</a> |
<a href="${currentUrl}?site_preference=mobile">Mobile</a>

Spring Mobile provides handler for SitePreference is SitePreferenceHandler, it's implementation is StandardSitePreferenceHandler.

5.3 SitePreferenceHandlerInterceptor
To enable SitePreference management before requests are processed, add the SitePreferenceHandlerInterceptor to your DispatcherServlet configuration:
<interceptors>
  <!-- On pre-handle, manage the user's site preference (declare after DeviceResolverHandlerInterceptor) -->
  <bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
</interceptors>
 
Java-based configuration is also available:
@Bean
public SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor() {
    return new SitePreferenceHandlerInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(sitePreferenceHandlerInterceptor());
}

5.4 Site Preference Storage
'CookieSitePreferenceRepository' (default implementation of SitePreferenceRepository) stores the user's preference in client side cookie so that it can be used for the future requests made by that user.

For getting current site preference Spring Mobile provides utility class SitePreferenceUtils, it give current site preference instance as below.

SitePreference sitePreference = SitePreferenceUtils.getCurrentSitePreference(servletRequest);

You could pass this site preference as argument with following configuration.

To configure a SitePreferenceWebArgumentResolver:
<annotation-driven>
  <argument-resolvers>
    <bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
  </argument-resolvers>
</annotation-driven>
  
Java-based configuration is also available:
@Bean
public SitePreferenceHandlerMethodArgumentResolver sitePreferenceHandlerMethodArgumentResolver() {
    return new SitePreferenceHandlerMethodArgumentResolver();
}

@Override
public void addArgumentResolvers(List argumentResolvers) { 
    argumentResolvers.add(sitePreferenceHandlerMethodArgumentResolver());
}

You can then inject the indicated SitePreference into your @Controller as shown below:

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home(SitePreference sitePreference, Model model) {
        if (sitePreference == SitePreference.NORMAL) {
            logger.info("Site preference is normal");
            return "home";
        } else if (sitePreference == SitePreference.MOBILE) {
            logger.info("Site preference is mobile");
            return "home-mobile";
        } else if (sitePreference == SitePreference.TABLET) {
            logger.info("Site preference is tablet");
            return "home-tablet";
        } else {
            logger.info("no site preference");
            return "home";
        }
    }

}
6. Site Switching
Spring mobile also provides different types of site switchers (like mDot, dotMobi and urlPath SiteSwitcher) which automatically redirect users to the device specific site based on the device generating the request and site preference set by the user.

SiteSwitcherHandlerInterceptor redirect users to device specific site. There are different types of site switchers available like mDot, dotMobi and urlPath SiteSwitcher. Bute here we have used urlPath SiteSwitcher which redirects users to different paths within the application based on the device and site preference.

<interceptors>
  <!-- On pre-handle, resolve the device that originated the web request -->
  <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
  <!-- On pre-handle, redirects mobile users to "m.myapp.com" (declare after DeviceResolverHandlerInterceptor) -->
  <bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" 
      factory-method="mDot">
    <constructor-arg index="0" type="java.lang.String" value="myapp.com"/>
  </bean>
</interceptors>

Java-based configuration is also available:


@Bean
public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
    return new DeviceResolverHandlerInterceptor();
}

@Bean
public SiteSwitcherHandlerInterceptor siteSwitcherHandlerInterceptor() {
    return SiteSwitcherHandlerInterceptor.mDot("myapp.com", true);
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(deviceResolverHandlerInterceptor());
    registry.addInterceptor(siteSwitcherHandlerInterceptor());
}

7. Device Aware View Management
ViewResolver provides a mapping between logical view name and actual view. Here we have used device aware view resolver ('LiteDeviceDelegatingViewResolver') that delegates to InternalResourceViewResolver allowing for resolution of device specific view names by adjusting view name by adding prefix (say 'm/' and 't/' for mobile and tablet) or suffix without the need for defining separate mapping for each device specific view.

7.1 Enabling Device Aware Views

XML configuration:
<bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
  <constructor-arg>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/" />
      <property name="suffix" value=".jsp" />
    </bean>
  </constructor-arg>
  <property name="mobilePrefix" value="mobile/" />
  <property name="tabletPrefix" value="tablet/" />
</bean>
   
Java-based configuration:
@Bean
public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() {
    InternalResourceViewResolver delegate = new InternalResourceViewResolver();
    delegate.setPrefix("/WEB-INF/views/");
    delegate.setSuffix(".jsp");
    LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate);
    resolver.setMobilePrefix("mobile/");
    resolver.setTabletPrefix("tablet/");
    return resolver;
}

7.2 Fallback View Resolution
There may be cases where device specific views for a particular page may not be available. There Fallback resolution ('enableFallback') comes into picture. If adjusted view name can not be resolved then original view is used by the ViewResolver.

XML configuration:
<bean class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
    ...
    <property name="enableFallback" value="true" />
    ...
</bean>
       
Java-based configuration:
@Bean
public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() {
    ...
    resolver.setEnableFallback(true);
    ...
    return resolver;
}

8. Sample Example for Spring Mobile Overview
In this section we discuss a sample web application with using Spring Mobile to detect device and accordingly rendering the views.

Source Code of Sample Application
You could find source code this application from GitHub.

sample-application-spring-mobile


9. Summary
In this article we have seen two different module to detecting compatible site to the device one is Device Resolution and another is SitePreference by these we could resolve device aware views. And also add one sample application for Spring Mobile.



Method injection with Spring using Lookup method property

Spring Method Injection

In this article we will discuss how spring lookup-method annotation used for the method injection. Spring could inject dependencies between the beans in the application by following way of injection types:
  1. Setter Injection
  2. Constructor Injection
  3. Field Injection (@Autowired at field)
  4. Method Injection
As discussed in earlier article Spring Beans have mainly two types of scope i.e 1. Singleton (Instantiate only one object) and 2. Prototype (Instantiate a new object every time). To find more about Spring Bean Scope before reading this article. In Spring application there are many beans injected to each other for a goal. There is no problem when injected beans have same scope of beans like singleton bean injected with other singleton beans. Sometimes in Spring, Problems arise when you need to inject a prototype-scoped bean in a singleton-scoped bean. Since singletons are created (and then injected) during context creation it's the only time the Spring context is accessed and thus prototype-scoped beans are injected only once, thus defeating their purpose.


But Spring provides another way for injection of beans, It is called method injection. It is solution of above problem in injecting different scoped beans. It works as that since singleton beans are instantiated at context creation, but it changes the way prototype-scoped are handled, from injection to created by an abstract method. It is actually a another kind of injection used for dynamically overriding a class and its abstract methods to create instances every time the bean is injected.

Method injection is different from Constructor Injection and Setter Injection. While in Constructor and Setter Injection, Spring creates the beans and injects them using the constructor or setter method, in Method Injection Spring overrides a given abstract method of an abstract class and provides an implementation of the overridden method.

Note there is an alternative to method injection would be to explicitly access the Spring context to get the bean yourself. It's a bad thing to do since it completely defeats the whole Inversion of Control pattern, but it works.

This is an advanced form of dependency injection and should be used in very special cases as it involves byte-code manipulation by Spring.

Sample of Use Case of Spring Method Injection
You have a generic business object/singleton (TokenMachine) that need to create an implementation of an interface with state to perform some task.

Method injection with Spring using Lookup method property

There is usually one instance of Token Machine usually available in the Banks. However, a Token Machine generates a new instance of Token every time. In this case, Token Machine bean has scope Singleton while Token bean has scope Prototype.

In this sample application we define a Token class and an abstract TokenMachine class. The TokenMachine class defines an abstract method called generateToken() which returns a new Token. We use Spring’s Method Injection to create a new instance of Token from TokenMachine. We will configure Spring to override the generateToken() method and return a new instance of Token class.

Finally, we will test this sample by using Main class which will load the TokenMachine bean via Spring and invoke the generateToken() method.

Required Dependencies in Pom.xml

<properties>
  <spring.version>4.3.5.RELEASE</spring.version>
 </properties>
 
   <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
 </dependencies>

Creating TokenMachine class for generating Token.
TokenMachine.java
/**
 * 
 */
package com.doj.app;

/**
 * @author Dinesh.Rajput
 *
 */
public abstract class TokenMachine {
 
 public void findToken(){
  System.out.println("Token has been generated "+generateToken());
 }
 public abstract Token generateToken();
 
}

TokenMachine contains the lookup-method tag with the name generateToken and bean id token that tells Spring to perform Method Injection on generateToken() method and return the Token type

Creating Token class for generating Token.
Token.java

/**
 * 
 */
package com.doj.app;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author Dinesh.Rajput
 *
 */
@Component
@Scope("prototype")
public class Token {
 
}

Token is declared with scope Prototype so that a new instance is created for every call to generateToken() method.

Spring Configuration file
spring.xml

<?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">
 
 <bean id="token" class="com.doj.app.Token" scope="prototype"/>
 
 <bean id="tokenMachine" class="com.doj.app.TokenMachine" scope="singleton">
  <lookup-method bean="token" name="generateToken"/>
 </bean>

</beans>


Finally, we need a java program to test the Method Injection setup. This is done by Main.java as below.

/**
 * 
 */
package com.doj.app.main;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.doj.app.TokenMachine;
import com.doj.app.config.AppConfig;

/**
 * @author Dinesh.Rajput
 *
 */
public class Main {
 public static void main(String[] args) {
  ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  TokenMachine machine = context.getBean(TokenMachine.class);
  machine.findToken();
  machine = context.getBean(TokenMachine.class);
  machine.findToken();
  context.close();
 }
}


Now run this application main class has generation two times Token it always return different tokens.

Find Source Code from GitHub.

Spring JavaConfig's approach

/**
 * 
 */
package com.doj.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.doj.app.Token;
import com.doj.app.TokenMachine;

/**
 * @author Dinesh.Rajput
 *
 */
@Configuration
@ComponentScan("com.doj.app")
public class AppConfig {
 @Bean 
 public TokenMachine tokenMachine(){
  return new TokenMachine(){

   @Override
   public Token generateToken() {
    return new Token();
   }
   
  };
 }
}




Container Managed Transaction (CMT) on the EJB and Spring Declarative Transaction

This article is to compare the transaction management of EJB and Spring Declarative Transaction is very important on J2EE environment and usually we don’t notice them because J2EE container or POJOs lightweight framework do help our dirty work.

You could find more about Spring Transaction




EJB-CMT(Container Managed Transaction) Spring Declarative Transaction
EJB-CMT is tied with JTA. Spring Transaction is flexible, not tied any implementation, it is consistent abstraction of transaction either we could use Global Transaction or Local Transaction.
For EJB-CMT application server is required. No Application server required for Spring Managed Transaction
Container Managed Transaction apply only on the EJB specific classes. Container Managed Transaction apply only on the EJB specific classes.
EJB-CMT does not have flexible rule for rollback, First, if a system exception is thrown, the container will automatically roll back the transaction. Second, by invoking the setRollbackOnly method of the EJBContext interface, the bean method instructs the container to roll back the transaction. If the bean throws an application exception, the rollback is not automatic but can be initiated by a call to setRollbackOnly. Spring Transaction provides comprehensive support for customized rolling back. It also provide automatically rolling back in case any run time exceptions thrown not on checked exceptions.
You can not make influence to EJB CMT. You could customize the Spring Transaction with help of Spring AOP. You could make own advice for Spring Transaction.
EJB CMT provide the transaction across the remote calls. If your requirement such type then It is recommended you could EJB CMT. Spring transaction manager can not support remote calls across the servers. For this support required high end application server.





Implement Queue using Stack

How to implement queue functionality into stack?
As Queue has nature First In First Out (FIFO) i.e. any item insert into Queue arrange such way whenever we will fetch the item from queue we will get oldest inserted element fist.
Implement Queue using Stack

The Stack class represents a Last-In-First-Out (LIFO) stack of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.
Solution 1: Using a Temporary Stack

import java.util.Stack;

/**
 * 
 */

/**
 * @author Dinesh.Rajput
 *
 */
public class StackAsQueue {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Stack<Integer> stack = new Stack<Integer>();
  for(int i = 1;i<=10;i++ ){
   //stack.push(i);
   push(i, stack);
  }
  while(!stack.isEmpty()){
   System.out.println(stack.pop());
  }
 }

 private static void push(int i, Stack<Integer> stack) {
  Stack<Integer> temp = new Stack<Integer>();
  if(stack.isEmpty()){
   stack.push(i);
  }else{
   while(!stack.isEmpty()){
    int iii=stack.pop();
    temp.push(iii);
   }
   stack.push(i);
   while(!temp.isEmpty()){
    int iii=temp.pop();
    stack.push(iii);
   }
  }
 }

}


Solution 2: Without Using a Temporary Stack

For performing enqueue we require only one stack as we can directly push data into stack, but to perform dequeue we will require two Stacks, because we need to follow queue's FIFO property

Using recursive approach, the queue can be implemented with single Stack instance.
import java.util.Stack;


public class MyQueue<Integer> {

 Stack<Integer> stack = new Stack<Integer>();
 
 public boolean isEmpty(){
  return stack.isEmpty();
 }
 
 public void enqueue(Integer item){
  stack.push(item);
 }
 
 public Integer dequeue(){
  return pop(stack);
 }
 
 private Integer pop(Stack<Integer> stack){
  Integer top = stack.pop();
  Integer last;
  if(stack.isEmpty()){
   return top;
  }else{
   last = pop(stack);
  }
  stack.push(top);
  return last;
 }
}


How to find all Pairs in Array of Integers whose Sum is equal to a given Number?

It many times asked question in the programming interview. We have an array of integers and a given number so we have to find all pair in the array whose sum is equal to a given number. Suppose we have an array {4, 2, 5, 7, -1} and given number 6 so these pair will be (4,2) and (7,-1).

  • Array may contains positive or negative numbers.
  • Same pair could be repeated twice, we should print it every time.
  • Reverse of pair is acceptable e.g. can we print both (4,2) and (2,4) if given sum is 6.
  • Array may be big in size.
Solutions
There are following three solutions available for this problem. Lets see below the three solutions.

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 
 */

/**
 * @author Dinesh.Rajput
 *
 */
public class PairSumInArray {

 /**
  * @param args
  */
 public static void main(String[] args) {
  int [] array = {3,4,1,6,-1,7,5,2};
  int number = 6;
  firstSolutionON2(array, number);//This solution has O(n2) time complexity
  secondSolutionON1(array, number);//This solution has O(n) time complexity but consume extra space
  thirdSolutionONlogN(array, number);//This solution has O(NlogN)time complexity extra logN for Arrays.sort() method it using quick sort behind
 }

 private static void firstSolutionON2(int[] array, int number) {
  System.out.println("=====First Solution====");
  for(int i = 0; i < array.length ; i++){
   int first = array[i];
   for(int j = i +1 ; j < array.length; j++){
    int second = array[j];
    if(first+second == number){
     System.out.println("("+first+","+second+")");
    }
   }
  }
 }
 private static void secondSolutionON1(int[] array, int number) {
  Set<Integer> set = new HashSet<Integer>();
  System.out.println("=====Second Solution====");
  for(int a : array){
   int diff = number - a;
   if(set.contains(diff)){
    System.out.println("("+diff+","+a+")");
   }else{
    set.add(a);
   }
  }
 }
 private static void thirdSolutionONlogN(int[] array, int number) {
  Arrays.sort(array);
  int left = 0;
  int right = array.length -1 ;
  System.out.println("=====Third Solution====");
  while(left < right){
   int leftItem = array[left];
   int rightItem = array[right];
   if(leftItem+rightItem == number){
    System.out.println("("+leftItem+","+rightItem+")");
    left++;
    right--;
   }else if(leftItem+rightItem > number){
    right--;
   }else if(leftItem+rightItem < number){
    left++;
   }
  }
  
 }
}


Discussion for First Solutions
You take one number from array and then loop through array and output pairs which is equal to given sum. This solution is correct but it's time complexity is very high, O(n^2).

Discussion for Second Solutions
What we can do here is to store all numbers in a hashtable and just check if it contains second value in a pair. How is this solution better than previous one? It would require less comparisons. Only N to iterate through array and insert values in a Set because add() and contains() both O(1) operation in hash table. So total complexity of solution would be O(N). But this solution has few constraints, first it would need additional space of order O(n) to store numbers in Hashtable or Set, so you need additional space which could be problem if array is very large (remember the question we asked before writing solution). For a large array, you need a solution which doesn't require additional space, also known as in-place solution.

Discussion for Third Solutions
A more efficient in-place solution would be to sort the array and use two pointers to scan through array from both direction i.e. beginning and end. If sum of both the values are equal to given number then we output the pair and advance them. If the sum of two numbers is less than k then we increase the left pointer, else if the sum is greater than k we decrements the right pointer, until both pointers meet at some part of the array. The complexity of this solution would be O(NlogN) due to sorting.



Spring HATEOAS- a Hypermedia-Driven RESTful Web Service

In this Spring HATEOAS tutorial we will discuss about an another way of use of Restfull API. HATEOAS (Hypermedia as the Engine of Application State)- a Hypermedia-Driven RESTful Web Service. It is a constraint of the REST application architecture.

Spring HATEOAS

Table of Contents
  1. Introduction
  2. What is HATEOAS?
  3. What is need of HATEOAS?
  4. Features of HATEOAS
  5. Build a a Hypermedia-Driven RESTful Web Service
    1. Maven Dependency for Spring HATEOAS
    2. Implementation With LinkBuilder API
    3. ResourceSupport Implementation
  6. Summary

1. Introduction
Here we using Spring HATEOAS for creating very simple example of restful web service. Before that lets take understanding about HATEOAS (Hypermedia as the Engine of Application State) is a constraint of the REST application architecture.

2. What is HATEOAS?
HATEOAS is an another way to creating RESTful web services such that the client can dynamically discover the next or previous actions available to it at run-time from the server. These actions forward to the client as the Response body or Response Header in the for the links. HATEOAS is considered the final level of REST. This means that each link is presumed to implement the standard REST verbs of GET, POST, PUT, and DELETE.

3. What is need of HATEOAS?
Restful web service is very popular due to light weight and its six contraints. But there is drawback of Restful web service for large system because of each services would have a seperate URL to be defined. It is not easy to remember and maintain the complete list of services and state transition by the clients.

HATEOAS approach comes into picture with Restful API to overcome this problem. For All the client should require to get started is an initial URI, and set of standardized media types. Once it has loaded the initial URI, all future application state transitions will be driven by the client selecting from choices provided by the server. A hypermedia-driven site provides information to navigate the site's REST interfaces dynamically by including hypermedia links with the responses.

4. Features

  • Model classes for link, resource representation models
  • Link builder API to create links pointing to Spring MVC controller methods
  • Support for hypermedia formats like HAL


5. Build a a Hypermedia-Driven RESTful Web Service
We will create a simple example of hypermedia-driven REST service with Spring HATEOAS.

5.1 Maven Dependency for Spring HATEOAS
If you are using Maven, please add these dependencies to your pom.xml file.
<dependencies>
    <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
        <version>0.22.0.RELEASE</version>
    </dependency>
</dependencies>

5.2 Implementation With LinkBuilder API
A Simple HATEOAS Application

A simple example of a Spring HATEOAS project is freely available on Github

https://github.com/DOJ-SoftwareConsultant/spring-hateoas-rest-ws

This simple example consists of 2 media types: 'Account' and 'AccountHolder'

spring-hateoas-app

AccountController.java
/**
 * 
 */
package com.doj.hateoas.ws.accounts;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Dinesh.Rajput
 *
 */
@RestController
public class AccountController {

 protected Logger logger = Logger
   .getLogger(AccountController.class.getName());
 
 @Autowired
 AccountRepository accountRepository;
 
 @RequestMapping("/accounts")
 public List<Resource<Account>> all() {
  logger.info("accounts all() invoked");
  List<Account> accounts = accountRepository.getAllAccounts();
  List<Resource<Account>> resources = new ArrayList<Resource<Account>>();
  for (Account account : accounts) {
          resources.add(getAccountResource(account));
       }
  logger.info("accounts all() found: " + accounts.size());
  return resources;
 }
 
 @RequestMapping(value= "/accounts/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
 public Resource<Account> byId(@PathVariable("id") Long id) {
  logger.info("accounts byId() invoked: " + id);
  Account account = accountRepository.getAccount(id.toString());
  Resource<Account> resource = new Resource<Account>(account);
  resource.add(linkTo(methodOn(AccountController.class).all()).withRel("accounts"));
  resource.add(linkTo(methodOn(AccountController.class).findAccountHolderById(account.getAccountId(), account.getAccountHolder().getUserId())).withRel("accountHolder"));
  logger.info("accounts byId() found: " + account);
  return resource;
 }
 
 @RequestMapping(value= "/accounts/{id}/{userId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
 public Resource<AccountHolder> findAccountHolderById(@PathVariable("id") Long id, @PathVariable("userId") Long userId) {
  logger.info("accounts findAccountHolderById() invoked: " + id);
  Account account = accountRepository.getAccount(id.toString());
  AccountHolder accountHolder = account.getAccountHolder();
  Resource<AccountHolder> resource = new Resource<AccountHolder>(accountHolder);
  resource.add(linkTo(methodOn(AccountController.class).byId(account.getAccountId())).withRel("account"));
  logger.info("accounts findAccountHolderById() found: " + account);
  return resource;
 }
 
 private Resource<Account> getAccountResource(Account account) {
  Resource<Account> resource = new Resource<Account>(account);
  resource.add(linkTo(methodOn(AccountController.class).byId(account.getAccountId())).withSelfRel());
  resource.add(linkTo(methodOn(AccountController.class).findAccountHolderById(account.getAccountId(), account.getAccountHolder().getUserId())).withRel("accountHolder"));
  return resource;
 }
 
}


Look at the first two import statements, those are static imports as part of the spring hateoas api. linkTo and methodOn are used for adding the link to the response.

The entry point to our API is :
http://localhost:1111/accounts
[
{
accountId: 1000,
accountHolder: {
userId: 5115,
name: "Arnav",
address: "Noida"
},
amount: 1039.13,
ifscCode: "AA992QA",
links: [
{
rel: "self",
href: "http://localhost:1111/accounts/1000"
},
{
rel: "accountHolder",
href: "http://localhost:1111/accounts/1000/5115"
}
]
},
{
accountId: 2000,
accountHolder: {
userId: 2089,
name: "Anamika",
address: "Noida"
},
amount: 1239.43,
ifscCode: "AB966QJ",
links: [
{
rel: "self",
href: "http://localhost:1111/accounts/2000"
},
{
rel: "accountHolder",
href: "http://localhost:1111/accounts/2000/2089"
}
]
},
{
accountId: 3000,
accountHolder: {
userId: 1286,
name: "Dinesh",
address: "Noida"
},
amount: 3339.61,
ifscCode: "AD912SA",
links: [
{
rel: "self",
href: "http://localhost:1111/accounts/3000"
},
{
rel: "accountHolder",
href: "http://localhost:1111/accounts/3000/1286"
}
]
}
]
This will basically list all the accounts available at our repository.
This response not only has the account's detail, but includes the self-linking URL where that account is located.

  • rel means relationship. In this case, it's a self-referencing hyperlink. More complex systems might include other relationships. For example, an account might have a "rel":"accountHolder" relationship, linking the order to its customer.
  • href is a complete URL that uniquely defines the resource.


It provides links to any account detail of our api telling it the URLs it can use to:

http://localhost:1111/accounts/{id}
{
accountId: 1000,
accountHolder: {
userId: 5115,
name: "Arnav",
address: "Noida"
},
amount: 1039.13,
ifscCode: "AA992QA",
_links: {
accounts: {
href: "http://localhost:1111/accounts"
},
accountHolder: {
href: "http://localhost:1111/accounts/1000/5115"
}
}
}
It also provides links to any account holder of any account detail of our api telling it the URLs it can use to:

http://localhost:1111/accounts/{id}/{accountHolderId}
{
userId: 5115,
name: "Arnav",
address: "Noida",
_links: {
account: {
href: "http://localhost:1111/accounts/1000"
}
}
}
This account holder detail api has link for associated account detail for this user

And account detail API has two links one for all account list and other for account holder details.

5.3 ResourceSupport Implementation
ResourceSupport is the representation of resources. This class allows us to add the links and manage it.
/**
 * 
 */
package com.doj.hateoas.ws.accounts;

import java.io.Serializable;

import org.springframework.hateoas.ResourceSupport;

/**
 * @author Dinesh.Rajput
 *
 */
public class Account extends ResourceSupport implements Serializable {
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private Long accountId;
 private AccountHolder accountHolder;
 private Double amount;
 private String ifscCode;
 public Long getAccountId() {
  return accountId;
 }
 public void setAccountId(Long accountId) {
  this.accountId = accountId;
 }
 public AccountHolder getAccountHolder() {
  return accountHolder;
 }
 public void setAccountHolder(AccountHolder accountHolder) {
  this.accountHolder = accountHolder;
 }
 public Double getAmount() {
  return amount;
 }
 public void setAmount(Double amount) {
  this.amount = amount;
 }
 public String getIfscCode() {
  return ifscCode;
 }
 public void setIfscCode(String ifscCode) {
  this.ifscCode = ifscCode;
 }
 public Account(Long accountId, AccountHolder accountHolder, Double amount, String ifscCode) {
  super();
  this.accountId = accountId;
  this.accountHolder = accountHolder;
  this.amount = amount;
  this.ifscCode = ifscCode;
 }
 @Override
 public String toString() {
  return "Account [accountId=" + accountId + ", accountHolder=" + accountHolder + ", amount=" + amount
    + ", ifscCode=" + ifscCode + "]";
 }
}


6. Summary
Here we have been learned about Spring HATEOAS one of different approach to creating Restful web service. And also a build an application to describe HATEOAS concept.


Difference between Microservices Architecture and SOA

Service-oriented architecture (SOA): It is an architectural pattern in software design and development according to SOA application components provide services to other components via a communications protocol, typically over a network.

Microservices: It is a software architecture style in which large and complex software system are divided into independent collaborating processes communicating with each other using language-agnostic APIs.

Microservices has a special approach of breaking a monolithic application into many smaller services that talk to each other, SOA has a broader scope.

Microservices Architecture and SOA
@ImageSource-StackOverflow

Microservices are the kind of SOA we have been talking about for the last decade. Microservices must be independently deployable, whereas SOA services are often implemented in deployment monoliths. Classic SOA is more platform driven, so microservices offer more choices in all dimensions.

SOA-vs-Microservices



How to Create Microservices Architecture with Spring Boot

In this Microservices with Spring Boot tutorial we will discuss about to creating a microservices with spring and will see microservices architecture. Microservices allow large systems to be built up from a number of collaborating components. Microservices allows to do loose coupling between application processes in stead of loose coupling between application components as Spring does.

Table of Contents
  1. Introduction
    1. What is Microservices Architecture?
    2. Without Microservices
    3. With Microservices
  2. Microservices Benefits
  3. Microservices Challenges
  4. Microservices Infrastructure
  5. Microservices Tooling Supports
    1. Spring
      1. Spring Boot
    2. Spring Cloud
      1. Eureka by Netflix
  6. Developing Simple Microservices Example
    1. Creating Discovery Service
    2. Creating MicroService (the Producer)
    3. Create Microservice Consumers
  7. Summary

1. Introduction
Microservices is not a new term. It coined in 2005 by Dr Peter Rodgers then called micro web services based on SOAP. It became more popular since 2010. Micoservices allows us to break our large system into number of independent collaborating processes. Lets see below microservices architecture.

1.1 What is Microservices Architecture?
Microservices architecture allows to avoid monolith application for large system. It provide loose coupling between collaborating processes which running independently in different environments with tight cohesion. So lets discuss it by an example as below.

For example imagine an online shop with separate microservices for user-accounts, product-catalog order-processing and shopping carts. So these components are inevitably important for such a large online shopping portal. For online shopping system we could use following architectures.

1.2 Shopping system without Microservices (Monolith architecture)
In this architecture we are using Monolith architecture i.e. all collaborating components combine all in one application.
monolith application architecture

1.3 Shopping system with Microservices
In this architecture style the main application divided in a set of sub applications called microservices. One large Application divided into multiple collaborating processes as below.
Microservices Architecture

Spring enables separation-of-concerns

  • Loose Coupling- Effect of changes isolated
  • Tight Cohesion- Code perform a single well defined task


Microservices provide the same strength as Spring provide

  • Loose Coupling- Application build from collaboration services or processes, so any process change without effecting another processes.
  • Tight Cohesion-An individual service or process that deals with a single view of data.
spring certification

There are a number of moving parts that you have to setup and configure to build such a system. For implementing this system is not too obvious you have to knowledge about spring boot, spring cloud and Netflix. In this post I will discuss one example for this architecture before the example lets first discuss about pros and cons of microservices architecture.

2. Microservices Benefits
  • Smaller code base is easy to maintain.
  • Easy to scale as individual component.
  • Technology diversity i.e. we can mix libraries, databases, frameworks etc.
  • Fault isolation i.e. a process failure should not bring whole system down.
  • Better support for smaller and parallel team.
  • Independent deployment
  • Deployment time reduce
3. Microservices Challenges
  • Difficult to achieve strong consistency across services
  • ACID transactions do not span multiple processes.
  • Distributed System so hard to debug and trace the issues
  • Greater need for end to end testing
  • Required cultural changes in across teams like Dev and Ops working together even in same team.
4. Microservices Infrastructure

  • Platform as a Service like Pivotal Cloud Foundry help to deployment, easily run, scale, monitor etc.
  • It support for continuous deployment, rolling upgrades fo new versions of code, running multiple versions of same service at same time.
5. Microservices Tooling Supports
5.1 Using Spring for creating Microservices
  • Setup new service by using Spring Boot
  • Expose resources via a RestController
  • Consume remote services using RestTemplate

5.2 Adding Spring Cloud and Discovery server
What is Spring Cloud?
  • It is building blocks for Cloud and Microservices
  • It provides microservices infrastructure like provide use services such as Service Discovery, Configuration server and Monitoring.
  • It provides several other open source projects like Netflix OSS.
  • It provides PaaS like Cloud Foundry, AWS and Heroku.
  • It uses Spring Boot style starters
There are many use-cases supported by Spring Cloud like Cloud Integration, Dynamic Reconfiguration, Service Discovery, Security,Client side Load Balancing etc. But in this post we concentrate on following microservices support
  • Service Discovery (How do services find each other?)
  • Client-side Load Balancing (How do we decide which service instance to use?)
Service Discovery
Problem without discovery
  • How do services find each other?
  • What happens if we run multiple instances for a service

microservices without discovery server


Resolution with service discovery

microservices with discovery server

Implementing Service Discovery
Spring Cloud support several ways to implement service discovery but for this I am going to use Eureka created by Netflix. Spring Cloud provide several annotation to make it use easy and hiding lots of complexity.

Client-side Load Balancing
Each service typically deployed as multiple instances for fault tolerance and load sharing. But there is problem how to decide which instance to use?

Implementing Client-Side Load Balancing
We will use Netflix Ribbon, it provide several algorithm for Client-Side Load Balancing. Spring provide smart RestTemplate for service discovery and load balancing by using @LoadBalanced annotation with RestTemplate instance.

eureka discovery server

@ImageSource-Spring.io

6. Developing Simple Microservices Example

For build a simple microservices system following steps required
  1. Creating Discovery Service (Creating Eureka Discovery Service)
  2. Creating MicroService (the Producer)
    1. Register itself with Discovery Service with logical service.
  3. Create Microservice Consumers find Service registered with Discovery Service
    1. Discovery client using a smart RestTemplate to find microservice.

Maven Dependencies
<dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.hsqldb</groupId>
   <artifactId>hsqldb</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

Step 1: Creating Discovery Service (Creating Eureka Discovery Service)

  • Eureka Server using Spring Cloud
  • We need to implement our own registry service as below.


discovery-service-application

application.yml
# Configure this Discovery Server
eureka:
  instance:
    hostname: localhost
  client: #Not a client
    registerWithEureka: false
    fetchRegistry: false

# HTTP (Tomcat) port
server:
  port: 1111   


DiscoveryMicroserviceServerApplication.java
package com.doj.discovery;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryMicroserviceServerApplication {

 public static void main(String[] args) {
  SpringApplication.run(DiscoveryMicroserviceServerApplication.class, args);
 }
}


pom.xml
<!-- Eureka registration server -->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>

For Whole Source Code for the Discover Server Application you could download from github as below link.

discovery-microservice-server

Run this Eureka Server application with right click and run as Spring Boot Application and open in browser http://localhost:1111/

discovery-service-home-page

Step 2: Creating Account Producer MicroService
Microservice declares itself as an available service and register to Discovery Server created in Step 1.

  • Using @EnableDiscoveryClient
  • Registers using its application name

Lets see the service producer application structure as below.
account microservice application

application.yml
### Spring properties
# Service registers under this name
spring:
  application:
    name: accounts-microservice

# Discovery Server Access
eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:1111/eureka/

# HTTP Server (Tomcat) Port
server: 
  port: 2222

# Disable Spring Boot's "Whitelabel" default error page, so we can use our own
error:
  whitelabel:
    enabled: false

AccountsMicroserviceServerApplication.java
package com.doj.ms.accounts;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class AccountsMicroserviceServerApplication {

 public static void main(String[] args) {
  SpringApplication.run(AccountsMicroserviceServerApplication.class, args);
 }
 
}


pom.xml
<dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

 </dependencies>

Other required source files related to this application you could download from github link as given below

accounts-microservice-server

Now run this account service application as Spring Boot application and after few seconds refresh browser to the home page of Eureka Discovery Server at http://localhost:1111/ in previous Step 1. Now one Service registered to the Eureka registered instances with Service Name "ACCOUNT-MICROSERVICE" as below

account microservice

Step 3: Consumer Service

  • Create Consumers to find the Producer Service registered with Discovery Service at Step 1.
  • @EnableDiscoveryClient annotation also allows us to query Discovery server to find miroservices.


Lets see the consumer application structure as below.


application.yml
# Service registers under this name
# Control the InternalResourceViewResolver:
spring:
  application:
    name: accounts-web
  mvc:
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp

# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/

# Disable Spring Boot's "Whitelabel" default error page, so we can use our own
error:
  whitelabel:
    enabled:  false
      

WebclientMicroserviceServerApplication.java
package com.doj.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class WebclientMicroserviceServerApplication {
 
 public static final String ACCOUNTS_SERVICE_URL = "http://ACCOUNTS-MICROSERVICE";
 
 public static void main(String[] args) {
  SpringApplication.run(WebclientMicroserviceServerApplication.class, args);
 }
 
 @Bean
 @LoadBalanced
 public RestTemplate restTemplate() {
  return new RestTemplate();
 }
 @Bean
 public AccountRepository accountRepository(){
  return new RemoteAccountRepository(ACCOUNTS_SERVICE_URL);
 }
}


pom.xml
<dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <!-- These dependencies enable JSP usage -->
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>
 </dependencies>


Other required source files related to this application you could download from github link as given below

webclient-microservice-server

Now run this consumer service application as Spring Boot application and after few seconds refresh browser to the home page of Eureka Discovery Server at http://localhost:1111/ in previous Step 1. Now one more Service registered to the Eureka registered instances with Service Name "ACCOUNTS-WEB" as below


Lets our consumer consume the service of producer registered at discovery server.

package com.doj.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

/**
 * @author Dinesh.Rajput
 *
 */
public class RemoteAccountRepository implements AccountRepository {
 
 @Autowired
 protected RestTemplate restTemplate;
 
 protected String serviceUrl;
 
 public RemoteAccountRepository(String serviceUrl) {
  this.serviceUrl = serviceUrl.startsWith("http") ? serviceUrl
    : "http://" + serviceUrl;
 }
 
 @Override
 public List<Account> getAllAccounts() {
  Account[] accounts = restTemplate.getForObject(serviceUrl+"/accounts", Account[].class);
  return Arrays.asList(accounts);
 }

 @Override
 public Account getAccount(String number) {
  return restTemplate.getForObject(serviceUrl + "/accounts/{id}",
    Account.class, number);
 }

}


Lets open web application which is a consumer of the account microservice registered at Eureka Discovery Server.

http://localhost:8080/ as below

ms-webclient-service-app

Now click on View Account List then fetch all accounts from account microservice.

http://localhost:8080/accountList

ms-webclient-service-app-2

Now click on any account from the list of accounts to fetch the details of account for account number from account microservice.

http://localhost:8080/accountDetails?number=5115

ms-webclient-service-app-3


Load Balanced RestTemplate
Create using @LoadBalanced- Spring enhances it to service lookup & load balancing
@Bean
 @LoadBalanced
 public RestTemplate restTemplate() {
  return new RestTemplate();
 }

Must inject using same qualifier-

  • If there are multiple RestTemplate you get the right one.
  • It can used to access multiple microservices

@Autowired
        @LoadBalanced
 protected RestTemplate restTemplate;

Load Balancing with Ribbon
Our smart RestTemplate automatically integrates two Netflix utilities

  • Eureka Service Discovery
  • Ribbon Client Side Load Balancer

Eureka return the URL of all available instances
Ribbon determine the best available service too use

Just inject the load balanced RestTemplate automatic lookup by logical service-name

7. Summary
After completion of this article you should have learned:

  • What is the MicroServices Architecture
  • Advantages and Challenges of MicroServices
  • And some information about Spring Cloud such as Eureka Discover Server by Netflix and Ribbon.