Showing posts with label Spring Tutorial. Show all posts
Showing posts with label Spring Tutorial. Show all posts

Friday, March 30, 2018

How Spring MVC supports REST Resources?

Spring has long had some of the ingredients needed for exposing REST resources.
Starting with version 3.0, however, Spring began adding enhancements to Spring MVC
to provide first-class REST support.

Spring supports the creation of REST resources in the following ways:
  • Controllers can handle requests for all HTTP methods, including the four primary REST methods: GET, PUT, DELETE, and POST. Spring 3.2 and higher also supports the PATCH method.
  • The @PathVariable annotation enables controllers to handle requests for parameterized URLs (URLs that have variable input as part of their path).
  • Resources can be represented in a variety of ways using Spring views and view resolvers, including View implementations for rendering model data as XML,JSON, Atom, and RSS.
  • The representation best suited for the client can be chosen using Content Negotiating ViewResolver
  • View-based rendering can be bypassed altogether using the @ResponseBody annotation and various HttpMethodConverter implementations.
  • Similarly, the @RequestBody annotation, along with HttpMethodConverter
    implementations, can convert inbound HTTP data into Java objects passed in to
    a controller’s handler methods.
  • Spring applications can consume REST resources using RestTemplate.
     

Thursday, January 25, 2018

Spring Boot features

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features:

  1. Create stand-alone Spring applications
  2. Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  3. Provide opinionated 'starter' POMs to simplify your Maven configuration
  4. Automatically configure Spring whenever possible
  5. Provide production-ready features such as metrics, health checks and externalized configuration
  6. Absolutely no code generation and no requirement for XML configuration
 The following are Prerequisite of Spring Boot:

Java 1.8
Gradle 2.3+ or Maven 3.0+
Spring Framework 5.0.0.BUILD-SNAPSHOT
An IDE (Spring Tool Suit) is recommended.

Installing Spring Boot:

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Regardless, you will need Java SDK v1.6 or higher. You should check your current Java installation before you begin:

$ java -version

You can use Spring Boot in the same way as any standard Java library. Simply include the appropriate spring-boot-*.jar files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor; and there is nothing special about a Spring Boot application, so you can run and debug as you would any other Java program.

Although you could just copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).


Wednesday, March 15, 2017

Spring IoC Container

In this post we will discuss and learn the overview of Spring IoC Container.The Spring Container will create the objects,write them together,configure them and manage their complete life cycle from creation till destruction.The IoC container gets information from the XML and works accordingly. The IoC container is the main component of the Spring framework. The core container is responsible for providing essential functionality to the Spring Framework.Spring's container used Dependency Injection to manage the components that make up an application.

The Spring IoC Contains mainly Two types they are:


  • BeanFactory
  • ApplicationContext
 The org.springframework.beans and org.springframework.context packages provide the basis for the spring framework's IoC container. 

BeanFactory:

BeanFactories are the simplest of containers,providing basic support for Dependency Injection.The org.springframework.beans.factory.BeanFactory is the actual representation of the Spring IoC container that is responsible for containing and otherwise managing the beans.

ApplicationContext:

The ApplicationContext interface builds on top of the BeanFactory(is is a sub-interface) and adds other functionality such as easier integration with spring's AOP features,message resources handling(i18N),event propagation,and application-layer specific contexts such as webApplicationContext for use in the web applications.

The ApplicationContext container includes all functionality of the BeanFactory container,so it is generally recommended over BeanFactory. BeanFactory is used when we develop applications like mobile devices or applet based applications.That means we used BeanFactory for light weight applications.

IoC is also known as dependency injection (DI). There are 3 forms of DI: They are


  1. Constructor Injection
  2. Setter Injection
  3. Interface Injection
Constructor Injection:
In this approach , an IoC container uses the constructor to inject the dependency. All the dependencies (simple or references) are declared in the constructor. One of the advantages of Constructor Injection is that all the dependencies are declared in one step. This also helps in understanding whether the class depends on too many services.

Through the Spring beans XML file you can configure your bean to initialize with an argument for the constructor, and then assign the arguments. Spring essentially “injects” the argument into your bean. This is referred to as constructor injection.

The following example passes in the String message using a constructor. The class is the same as the one in Basic Bean Creation except the default message on the message variable has been cleared and is now set to null. A single parameter constructor has been added to set a message.



Example:
public class ConstructorMessage {
private String message = null;
/**
* Constructor
*/
public ConstructorMessage(String message) {
this.message = message;
}
/**
* Gets message.
*/
public String getMessage() {
return message;
}

/**
* Sets message.
*/
public void setMessage(String message) {
this.message = message;
}

}

ConstructorMessageTest-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="message"
class="org.learnprogramingbyluckysir.di.xml.ConstructorMessage">
<constructor-arg value="Spring is fun." />
</bean>

</beans>


Setter Injection:
In this approach Dependency Injection uses Setters to inject the required resources or dependencies. Each of the objects that the class depends upon will have a setter and the IoC container will use the setters to provide the resource at run-time.

Spring uses the set method names to infer the property name, the naming of your set methods should follow the JavaBeans Spec, or at least be consistent within the confines of your application. See the example below for basic setter injection on our Message class

Example:
public class SetterMessage {
private String message = null;
/**
* Gets message.
*/
public String getMessage() {
return message;
}
/**
* Sets message.
*/
public void setMessage(String message) {
this.message = message;
}

}

The property element is used to define the setter injection:
SetterMessageTest-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="message"
class="org.learnprogramingbyluckysir.di.xml.SetterMessage">
<property name="message" value="Spring is fun." />
</bean>

</beans>


Interface Injection:

So far we have only injected constructor and property values with static values, which is useful if you want to eliminate configuration files. Values can also be injected by reference -- one bean definition can be injected into another. To do this, you use the constructor-arg or property's ref attribute instead of the value attribute. The ref attribute then refers to another bean definition's id.

In the following example, the first bean definition is a java.lang.String with the id springMessage. It is injected into the second bean definition by reference using the property element's ref attribute.

ReferenceSetterMessageTest-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="springMessage"
class="java.lang.String">
<constructor-arg value="Spring is fun." />
</bean>
<bean id="message"
class="org.learnprogramingbyluckysir.di.xml.SetterMessage">
<property name="message" ref="springMessage" />
</bean>
</beans>

Some Interview Questions:

1) What is difference between BeanFactory and ApplicationContext?

Ans: BeanFactory is the basic container whereas ApplicationContext is the advanced Container. ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory such as integration with AOP,message resources handling for i18N etc..

2) What is difference between Constructor Injection and setter injection?

Ans: 

3) What is the role of IoC container in spring?

Ans:
  1. Create the instance
  2. configure the instance,and
  3. assemble the dependencies


See also:

Spring MVC Architecture
Spring Interview Questions


High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...