Book Image

Learning Spring 5.0

By : Tejaswini Mandar Jog
Book Image

Learning Spring 5.0

By: Tejaswini Mandar Jog

Overview of this book

<p>Spring is the most widely used framework for Java programming and with its latest update to 5.0, the framework is undergoing massive changes. Built to work with both Java 8 and Java 9, Spring 5.0 promises to simplify the way developers write code, while still being able to create robust, enterprise applications.</p> <p>If you want to learn how to get around the Spring framework and use it to build your own amazing applications, then this book is for you.</p> <p>Beginning with an introduction to Spring and setting up the environment, the book will teach you in detail about the Bean life cycle and help you discover the power of wiring for dependency injection. Gradually, you will learn the core elements of Aspect-Oriented Programming and how to work with Spring MVC and then understand how to link to the database and persist data configuring ORM, using Hibernate.</p> <p>You will then learn how to secure and test your applications using the Spring-test and Spring-Security modules. At the end, you will enhance your development skills by getting to grips with the integration of RESTful APIs, building microservices, and doing reactive programming using Spring, as well as messaging with WebSocket and STOMP.</p>
Table of Contents (18 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
9
Explore the Power of RESTful Web Services

Instance creation


In Java, the following are the two ways to create an instance:

  • Using constructor
  • Using the factory method

I will not go into detailed scenarios on when to use which way, as we all are from Java background, and have read the reasons a number of times. We will directly start with how to use them in the Spring framework one by one. Hope that's fine with you!

Using constructor

Let's take an example of a Car to make it crystal clear how the container creates its object using the following steps:

  1. Create a Java application Ch02_Instance_Creation, and add jars that we added in the previous project.
  2. Create a Car class in the com.ch02.beans package with chassis_number, color, fuel type, price, and average as data members. The code is as follows:
class Car{ 
  private String chassis_number, color, fuel_type; 
  private long price; 
  private double average; 
}
 
  1. Add show() in Car as shown in the following lines of code:
public void show() 
{ 
  System.out.println("showing car "+chassis_number...