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

Friday, 9 September 2016

Spring Framework DI: Advantages, Types and Examples

Post Brief Table of Content

  • Introduction
  • DI (Dependency Injection)  Basics
  • Advantages of DI
  • Types of DI
  • Setter Injection (Setter DI)
  • Constructor Injection (Constructor DI)
  • Interface Injection (Interface DI)
  • DI Interview Questions

Introduction

Before reading this post, please go through my previous posts: (click on the links)

In this post, I'm going to discuss some more basic principles of Spring Framework. 

DI (Dependency Injection) Basics

DI stands for Dependency Injection. DI is one of the form of IOC. Spring Framework was designed based on this pattern. 

As we discussed in my previous post, DI means “Resolving the bean dependencies”. In Spring Applications, Spring IOC Container resolves these dependencies automatically.


NOTE:- IOC and Dependency Injection follow Push Model.
Advantages of DI
In this section, we will discuss about what are all the major advantages of DI.

Because of DI follows Push Model, it has the following advantages:
  • Developer is not responsible for creating of objects
  • Reduces the application complexity
  • Reduces development time
  • Easy to do the application unit testing
  • Decreases components dependencies
NOTE:- What is the main responsibility of DI?

Object Dependencies Management

Types of DI

DI (Dependency Injection) can be divided into the following three main categories:
  • Setter Injection
  • Constructor Injection
  • Interface Injection



We will discuss these categorizes one by one in detail with suitable examples in the coming sections.

Setter Injection (Setter DI)

As we know, DI means resolving bean dependencies. To resolve bean dependencies, we can use different techniques.

If we use setter method to resolve bean dependencies, then this kind of DI (Dependency Injection) is known as Setter Injection or Setter DI.

Setter DI Example:-
public class CardService
{
    private CreditCard creditCard;
    // Setter 
    public void setCreditCard(CreditCard creditCard)
    {
 this.creditCard= creditCard;
    }
}
How to represent Setter DI in Spring Bean Configuration XML file?
By using <property> element
Syntax:-
For Primitive or String Properties:
<property name="property-name"  value=”some-value”/>

Syntax:-
For Reference Properties:
<property name="property-name"  ref=”reference-name”/>
Example:-
<bean id=”cardService”  class=”com.way2it.bank.CardService”>
 <property  name=” creditCard”   ref=” creditCard”/>
</bean>
Constructor Injection (Constructor DI)

If we use constructor to resolve bean dependencies, then this kind of DI (Dependency Injection) is known as Constructor Injection or Constructor DI.

Constructor DI Example:-
public class CardService
{
   private CreditCard creditCard;
   // Constructor  
   Public CardService (CreditCard creditCard)
   {
     this.creditCard= creditCard;
   }
}

How to represent Constructor DI in Spring Bean Configuration XML file?
By using <constructor-arg> element, but not <property> element
Syntax:-
For Primitive or String Properties:
<constructor-arg  value=”some-value”/>

Syntax:-
For Reference Properties:
<constructor-arg  ref=”refBeanObject”/>
Example:-
<bean id=”cardService”  class=”com.way2it.bank.CardService”>
 <constructor-arg  ref=” creditCard”/>
</bean>
Interface Injection (Interface DI)

If we use interface to resolve bean dependencies, then it is known as Interface Injection or Interface DI.

How to implement Interface Injection?
Spring framework does NOT support Interface Injection.

Why Spring framework does NOT support Interface Injection?
Because we can NOT instantiate an Interface.

That’s it all about “Spring Framework DI” concept. We will discuss some more important Spring Basics in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.

Thursday, 1 September 2016

Spring Framework: IOC, DI and Bean Configuration

Post Brief Table of Content

  • Introduction
  • What is IOC?
  • Forms of IOC
  • What is DI?
  • What is Spring Bean?
  • What is Bean Dependency?
  • Spring XML Bean Configuration
  • What is Injection?
  • Interview Questions

Introduction

Please go through my previous post at Introduction to Spring (click on the link). In this post, I'm going to discuss some basic principles of Spring Framework. 
Spring Framework follows IOC or DI Design Pattern. We will discuss some important concepts or questions in this post: What is IOC? What is DI? Types of DI? Advantages of DI etc. Let us start this tutorial now.

What is IOC?

IOC stands for Inversion of Control. IOC is an architectural Design Pattern. Spring Framework was designed based on this pattern. 

IOC means “Developer is not responsible for creating any single object. Container creates all the required objects and given them to the developer”
What are the responsibilities of Developer in IOC?
Developer is responsible for informing the following three things to the Container:
  • What
  • Where
  • When
That means 
What type of objects they want
Where they want those objects
When they want those objects – either Deployment time or Runtime.


Forms of IOC

In this section we will discuss about what are the different forms of IOC available.

In general, IOC has two main forms
  • DI (Dependency Injection)
  • Dependency Bijection
Spring Framework supports only DI, does not support Dependency Bijection. JBoss SEAM framework supports both DI and  Dependency Bijection.
What is DI?

DI stands for “Dependency Injection”. It is one of the forms of IOC Pattern. Spring Framework uses this design pattern extensively in all it's modules.

DI means “Resolving bean dependencies by using Injection”. Resolving bean dependencies done by Spring Container, not by us (Developers).

Like Servlet API uses Servlet Container to run servlets and JSP has JSP Container,  EJB uses EJB Container etc., Spring Framework uses Spring Container to run Spring applications. As Spring Framework follows this IOC or DI Design pattern extensively, we can call it as "Spring IOC Container".

What is Spring Bean?

Spring Framework treats every Java class as a Bean. We need to configure them by using of the following techniques:
  • Spring XML Configuration
  • Spring Annotations
  • Java Config
Spring Bean Example:-
public class CreditCard
{
  private int cardid;
  private int cardnumber;
  private String custname;
  // Setters and Getters
}

public class CardService
{
  private CreditCard card;
  // Setter and Getter
}

What is Bean Dependency?

Please observe above example carefully. 

Here we have two beans: Two classes – CreditCard and CardService. CardService class is using CreditCard class object (OOP: Composition) That means CardService is depending on CreditCard object to perform card operations.

This is called Bean Dependency or Bean Collaboration.

Spring XML Configuration

In this section, we will see some examples on how to configure Spring Beans using XML Configuration. We will discuss rest of two concepts like Spring Annotations and Java Config in my coming posts.

Simple Bean Declaration Syntax:-
<bean id=”beanObjectName”  class=”FullyQualifiedBeanName”/>
Here id attribute needs a name that is beanObjectName.  it is our instance name. We can give any name we want.

class attribute needs fully qualified class name.

Example:-
<bean id=”creditCard”  class=”com.way2it.bank.CreditCard”/>
Bean With Properties Declaration:
If Bean contains primitive data type properties or  String properties, we need to use the following syntax. 

Syntax:
<bean id=”beanObjectName”  class=”FullyQualifiedBeanName”>
<property  name=”property-name”   value=”property-value”/>
</bean>
Example:-

<bean id=”creditCard”  class=”com.way2it.bank.CreditCard”>
 <property name=”cardid” value=”1001”/>
 <property name=”cardnumber”   value=”2222222”/>
        <property  name=”custname”  value=”Lokesh”/>
</bean>

If Bean contains reference data type properties, we need to use this syntax 
Syntax:
<bean id=”beanObjectName”  class=”FullyQualifiedBeanName”>
 <property  name=”property-name”   ref=”property-value”/>
</bean>
Here attribute  “ref”  stands for reference data type.

Example:-
<bean id=”creditCard”  class=”com.way2it.bank.CreditCard”/>
<bean id=”cardService”  class=”com.way2it.bank.CardService”>
 <property  name=”card”   ref=” creditCard”/>
</bean>
NOTE :-  <property> element attributes
  • name: Bean property name
  • ref: Bean reference name

What is Injection?

In this section, apart from answering What is Injection? we will also answer this important question: How to resolve bean dependencies using Injection?

Please observe above examples. CardService bean has a dependency on CreditCard bean.
Here CardService is referring or using CreditCard bean using ref=””  attribute. This is called Injection.

We are resolving bean dependencies by using Injection.

And we have answered many interview questions in this post right. Let me remind you some important questions here:
  • What is IOC?
  • What is DI?
  • What is Spring Bean Configuration XML file?
  • We use XML file to provide all bean configurations.
  • Why we call Spring Container as “Spring IOC Container”?
That’s it all about “Spring Framework: IOC, DI and Bean Configuration” concepts. We will discuss some more important Spring Basics in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.

Monday, 29 August 2016

Introduction To Spring Framework


Post Brief Table of Content

  • Introduction
  • What is Spring Framework?
  • Drawbacks of Non-Spring Applications
  • Why We need Spring Framework?
  • Advantages of Spring Framework
  • Drawbacks of Spring Framework
  • Spring Framework follows Invasive Approach
  • Spring Configuration
  • Pull Model
  • Push Model

Introduction

I'm going to deliver a serious of posts on Spring and Hibernate Frameworks. If you are new to XML, please go through my previous post on XML Basics (click on this link) because both frameworks uses XML extensively. It is my first post in this series.
We need Java and XML Basics to start this tutorial series.

What is Spring Framework

Spring Framework is an open source, light weight and loosely coupled Java Framework for developing enterprise applications very easily.
Founder of Spring Framework: Pivotal Software
Spring Framework Website: https://spring.io/

First version was release by Rod Johnson with his book 
Expert One-on-One J2EE Design and Development in October 2002.

First Spring 1.0 Milestone version was release in March 2004.
Latest Stable Spring Framework version is 4.3.2, released on June 2016

Drawbacks of Non-Spring Applications

Non-Spring Java EE web applications have the following drawbacks:
  • Code duplication or boilerplate code
  • Resource Leakage
  • Need to take care about exception handling
  • Tightly coupled applications
  • Tough to do unit testing
  • Need to write code for Singletons
  • Need to write code for Factory classes
Why we need Spring Framework?
As we discussed in the previous section Non-Spring Java EE Web Applications have lots of drawbacks. That's why most of the Java-based projects are running using Spring Framework.
Spring Framework solves all the above mentioned problems and also provides many benefits (Will discuss in next section).

Advantages of Spring Framework

Spring framework is one of the most popular web applications development framework because of it's benefits:
  • No code duplication
  • No Resource Leakage 
         Framework will take care about resource management 
        (Resource Closing)
  • No need to worry about Exception Handling
         Spring Framework takes care about converting Checked Exceptions   
         (SqlException) into Unchecked Exceptions(DataAccessException)
  • Loosely coupled applications
  • Easy to do unit testing (Spring Unit Testing Framework)
  • No need to write code for creating singletons because it provides declarative approach to create singletons.
  • No need to write code for factory classes. Use POJO style model    
NOTE:- Main Feature of Spring Framework is it follows Invasive Approach
Spring Framework follows Invasive Approach

Spring framework does NOT force the developer to define the bean classes by extending or implementing any Spring Framework related API classes or interfaces.

Spring Configuration

Other MVC Frameworks like Struts, JSF, Wicket etc forces the developer to use only XML file for Framework configuration file.

However, Spring framework never forces the developer to use only XML file
for Spring Bean Configuration file.

Spring supports the following options for Spring Bean Configuration file:
  • XML configuration file
  • Properties file
  • Programmatic Approach
  • Annotations Approach
Pull Model


Non-Spring J2EE Web Applications follow Pull model that means 
Developer is responsible for creating objects and pulls into the container

Example:-
Emp empObj = new Emp();
// Set employee values using Setter methods
httpSession.setAttribute("emp",empObj);



Drawbacks of Pull Model Approach:

Developer is responsible for creating the objects
Increases application complexity
Increases Development time
Tough to do the application unit testing 
Increases components dependency

Push Model

Spring Framework follows Push model approach that means Developer is not responsible for creating any objects. Container creates all required objects and give them to the developer.




Advantages of Push Model Approach:
Developer is not responsible for creating of objects
Reduces the application complexity
Reduces development time
Easy to do the application unit testing
Decreases components dependencies

Why Spring is more popular than other MVC frameworks like Struts, JSF, Wicket?
See "Advantages of Spring Framework" Section 

How Spring resolves all Non-Spring Java EE application problems?
Because of Spring Framework supports the following two concepts:
  • IOC(Inversion of Control)
  • DI(Dependency Injection)
We will discuss about IOC and DI in my coming posts with some useful examples soon.

That’s it all about “Introduction to Spring Framework”. We will discuss some more important Spring Framework concepts in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.
Thank you.

Tuesday, 26 July 2016

Spring Boot: Books To Learn

Introduction

In this post, I'm going to list all available best books to learn and become expert in Spring Boot Framework easily.

NOTE:- I work as a Pre-release book reviewer for Manning Publications. 


Spring Boot Books:

The following are the most popular and available books to learn Spring Boot Framework:

  • Pro Spring Boot
          From Apress Publications. 
          It's really very nice book for all from novice to experts.


  • Spring Boot in Action
          From Manning Publications. 
          I did review this book in pre-release stage and love to recommend this book to all. 

          It's really very nice book for all from novice to experts.

  • Spring Microservices
         It is from Packt Publishing.
          Best book to develop Microservices with Spring Boot Framework.
  
  • Spring Boot Cookbook
         It's also from Packt Publishing. 
         If you want to learn Spring Boot with examples, please have a read this book.

  • Learning Spring Boot
         It's also form Packt Publishing.

         It has been release a while ago 
         so it has covered some old version of Spring Boot Framework.

As now-a-days, most of projects are using Microservices to develop their projects, "Spring Microservices" is best book to learn Spring Boot Framework an also Microservices.

Thank you for reading my posts.

If you have any questions or issues, please drop me a comment.

Happy learning Spring Boot!

Thursday, 27 August 2015

Complete Spring 4.0/4.1/4.2 New Features In-Depth Tutorial

In this posts, I'm going to Complete Spring 4.0/4.1/4.2 New Features In-Depth discussion with Real-time examples through Javapapers.com website.
  1. Spring 4.0 Features
  2. Spring 4.1 Features
  3. Spring 4.2 Features
Sl NoSpring 4.x Feature Post URL
1Spring 4.x Features http://javapapers.com/spring/spring-4-x-features/
2Autowiring Java Generic Types in Spring Frameworkhttp://javapapers.com/spring/autowiring-java-generic-types-in-spring-framework/
3@Profile Annotation Improvements in Spring 4http://javapapers.com/spring/profile-annotation-improvements-in-spring-4/
4Spring @Conditional Annotationhttp://javapapers.com/spring/spring-conditional-annotation/
5Spring Properties with @PropertySource Annotationhttp://javapapers.com/spring/spring-properties-with-propertysource-annotation/
6
7
8
9


I will update this post as and when I deliver new posts.
Please drop me a comment if you like my posts or have any issues/suggestions.
Happy Spring 4.x Framework Learning!


Wednesday, 29 July 2015

Spring Boot Tutorial

Spring Boot Framework Tutorial

I'm going to deliver this Spring Boot Tutorial through www.journaldev.com website. Few posts are already live. Please use those post URLs to read this tutorial. 

My Screen name for all other tutorials: http://www.journaldev.com/author/rambabu

Sl NoPost TitlePost URL
1Introduction to Spring Boothttp://www.journaldev.com/7969/introduction-to-spring-boot
2Introduction to Gradlehttp://www.journaldev.com/7971/introduction-to-gradle
3Key Components and Internals of Spring Boot Frameworkhttp://www.journaldev.com/7989/key-components-and-internals-of-spring-boot-framework
4Spring Boot CLI Setup and HelloWorld Examplehttp://www.journaldev.com/8195/spring-boot-cli-setup-and-helloworld-example
5Java Simple Example with Gradle Eclipse Pluginhttp://www.journaldev.com/8118/java-simple-example-with-gradle-eclipse-plugin
6Gradle Build Script Basics, Difference between Maven and Gradle build scriptshttp://www.journaldev.com/8179/gradle-build-script-basics-difference-between-maven-and-gradle-build-scripts
7Gradle Spring MVC Simple Applicationhttp://www.journaldev.com/8398/gradle-spring-mvc-simple-application
8Compare Gradle and Maven Commands, Gradle Commands from CMD Prompthttp://www.journaldev.com/8396/compare-gradle-maven-comamnds
9Spring Boot Initilizr Web Interfacehttp://www.journaldev.com/8225/spring-boot-initilizr-web-interface
10Spring Boot Initilizr With IDEs or IDE Pluginshttp://www.journaldev.com/8596/spring-boot-initilizr-with-ides-or-ide-plugins
          11Spring Boot Initilizr With Spring Boot CLI                                                     http://www.journaldev.com/8609/spring-boot-initilizr-with-spring-boot-cli                                                                
         12Spring Boot Initilizr With ThirdParty Tools                                                     http://www.journaldev.com/8650/spring-boot-initilizr-with-thirdparty-tools                                                               
         13Spring Boot Framework In A Nutshell                                                           http://www.journaldev.com/8611/spring-boot-framework-ina-nutshell                                                                     


Please drop me your comments or suggestions to improve my Tutorial content useful to all.

Thanks. Happy Spring Boot Learning.