Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views24 pages

Spring

The document provides an overview of the Spring Framework, detailing its core features such as Dependency Injection, Object Lifecycle management, and various modules including Spring MVC, Spring Boot, and Spring Data JPA. It includes practical examples for developing applications using Maven in Eclipse, along with configuration steps and code snippets. Additionally, it outlines the use of Project Lombok to reduce boilerplate code and mentions the creation of REST APIs with Spring REST Module.

Uploaded by

bhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views24 pages

Spring

The document provides an overview of the Spring Framework, detailing its core features such as Dependency Injection, Object Lifecycle management, and various modules including Spring MVC, Spring Boot, and Spring Data JPA. It includes practical examples for developing applications using Maven in Eclipse, along with configuration steps and code snippets. Additionally, it outlines the use of Project Lombok to reduce boilerplate code and mentions the creation of REST APIs with Spring REST Module.

Uploaded by

bhanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Date:25/Jan/2023

----------------

Spring Framework

----------------

Spring Framework is an open source Java platform that provides comprehensive

infrastructure support for developing robust Java applications very easily and rapdily

Dependency Injection

--------------------

Spring is mostly identified with Dependency Injection which is flavor of

Inversion of Control (IoC)

Ex:

class Employee

{}

class Department

Employee emp = new Employee();

Department dept = new Department();

In the above code, emp object and dept object are tightly coupled

Ex:

class Employee

{}
class Department

Employee emp;//property

Department(Employee emp)//constructor injection

this.emp = emp;

void setEmp(Employee emp)//setter injection

this.emp = emp;

Employee emp = new Employee();

Department dept = new Department(emp);

In the above code, emp object and dept object are loosely coupled

Life cycle of an Object

-----------------------

Object creation -> Object Usage -> Object Destruction

In case of Java, Object Destruction is taken care by Java as Java is automatic

garbage collection

In case of Spring, the life of an object is taken care by Spring framework


In spring, the objects are created using Spring bean XML configuration file

Spring Modules

--------------

- Spring Core/Bean Module

- Spring DAO Module

- Spring JDBC

- Spring ORM (Hibernate)

- Spring Data JPA

- Spring MVC Module => used to develop web applications

- Spring Boot Module

- Spring REST Module

Developing a Spring Application in Eclipse using Maven

------------------------------------------------------

- Create a Maven Project

Click on File -> New -> Maven Project

Click Next

In Catalog Select : Internal

In Artifact Id Select : maven-archetype-quickstart

Click Next

Group Id : spring

Artifact Id : SpringProj
Package : com.spring

click Finish

- Add the following dependency in pom.xml file of SpringProj

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.3.10.RELEASE</version>

</dependency>

Refer program pom.xml

- Create a Spring bean class "Book" in com.spring package of src/main/java folder

of SpringProj

Refer program Book.java

- Create a Spring bean class "Library" in com.spring package

Refer program Library.java

- Create Spring bean XML configuration file "library.xml" in src/main/java folder

Refer program library.xml

- Create a test class "LibraryTest" in com.spring package

Refer program LibraryTest.java (Run)


Spring DAO Module

-----------------

DAO stands for Data Access Object which is a class/interface with set of methods

which are used to operate a database

- Spring JDBC Module

- Spring ORM Module (Hibernate)

- Spring Data JPA Module

Spring JDBC Module

------------------

Plain JDBC

----------

- handle the exceptions

- load driver class

- establish connection

- create statement

- execute statement

- close statement

- close connection

Spring JDBC

-----------

- execute statement

JdbcTemplate class of Spring API is used for Spring JDBC


Date:26/Jan/2023

----------------

Spring JDBC Module

------------------

JdbcTemplate class of Spring API is used for Spring JDBC

Example Application

-------------------

- Create a table "Student" in MySQL

mysql>create table Student (id int(3), name varchar(10), age int(2));


- Add the following dependencies in pom.xml file of SpringProj

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>4.3.10.RELEASE</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.31</version>

</dependency>

Refer program pom.xml

- Create an interface "StudentDAO" in com.spring package

Refer program StudentDAO.java

- Create a Spring bean class "StudentDAOImpl" in com.spring package

Refer program StudentDAOImpl.java

- Create Spring bean XML configuration file "jdbc.xml" in src/main/java folder

Refer program jdbc.xml

- Create a test class "JdbcTest" in com.spring package

Refer program JdbcTest.java (Run)


Spring ORM Module (Hibernate)

-----------------------------

- ORM stands for Object Relational Mapping

- Hibernate is an ORM framework

- HibernateTemplate class of Spring API is used for Spring Hibernate

Example Application

-------------------

- Add the following dependencies in pom.xml file

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>4.3.10.RELEASE</version>

</dependency>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>4.3.11.Final</version>

</dependency>

REfer program pom.xml

- Create a POJO class "Employee" in com.spring package

Refer program Employee.java

- Create Hibernate XML mapping file "emp.hbm.xml" in src/main/java folder


Refer program emp.hbm.xml

- Create an interface "EmployeeDAO" in com.spring package

Refer program EmployeeDAO.java

- Create a Spring bean class "EmployeeDAOImpl" in com.spring package

Refer program EmployeeDAOImpl.java

- Create Spring bean XML configuration file "spring-hibernate.xml" in src/main/java folder

Refer program spring-hibernate.xml

- Create a test class "SpringHibernateTest" in com.spring package

Refer program SpringHibernateTest.java (Run)


Date:27/Jan/2023

----------------

Spring MVC Module

-----------------

- Spring MVC Module is used to develop server side dynamic web applications

- MVC stands for Model View and Controller

- MVC Architecture is used to make the Model independent of View

Refer diagram MVC.png

- The front end controller in Spring MVC is "DispatcherServlet" which need

to be configured in web.xml file

- DispatcherServlet loads the spring bean xml configuration file


- In spring bean xml configuration file we configure the controller classes

- Controller class will invoke the Model and Model will return the result

back to the Controller and based on the result value, Controller will invoke

the respective View pages

Spring MVC Annotations

----------------------

@Controller

-----------

used to mark the class as the Controller class

Ex:

@Controller

public class LoginController

{}

@RequestMapping

---------------

used to map the result url

Ex:

<form action="login">

username ...

password ...

<input type="submit"/>

</form>

@Controller

public class LoginController


{

@RequestMapping("login")

public authenticate()

...

Developing a Spring MVC Application in Eclipse using Maven tool

---------------------------------------------------------------

- Configure tomcat server in eclipse workspace

- Create a Maven Project

Click on File -> New -> Maven Project

Click Next

In Catalog Select : Internal

In Artiface Id Select : maven-archetype-webapp

Click Next

Group Id : springmvc

Artifact Id : SpringMVCProj

Package : com.spring.mvc

click Finish
Note

----

If errors in the project,

Right click on SpringMVCProj -> Properties -> Targeted Runtimes ->

Make check mark to "Apache Tomcat Server v9" -> Click Apply and Close

- Add the following dependencies in pom.xml file of SpringMVCProj

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.3.10.RELEASE</version>

</dependency>

Refer program pom.xml

- Create a welcome page "login.jsp" in src/main/webapp folder

Refer program login.jsp

- Create a folder "java" in src/main folder

- Create a package "com.spring.mvc.controller" in src/main/java folder

- Create a Controller class "LoginController" in com.spring.mvc.controller package

Refer program LoginController.java

- Create a package "com.spring.mvc.model" in src/main/java folder

- Create a Model/Action/Service class "LoginAction" in com.spring.mvc.model package


Refer program LoginAction.java

- Create a view page "success.jsp" in src/main/webapp folder

Refer program success.jsp

- Create a view page "failure.jsp" in src/main/webapp folder

Refer program failure.jsp

- Update web.xml file by configuring Spring MVC front end controller "DispatcherServlet"

Refer program web.xml

- Create Spring bean XML configuration file "springmvc-servlet.xml" in WEB-INF folder

Refer program springmvc-servlet.xml

Note

----

The name of the spring bean xml configuration file should be "servlet-name-servlet.xml"

where "servlet-name" is the servlet name given in <servlet-name> element of web.xml file

- Run the application login.jsp


Date:30/Jan/2023

----------------

Spring Boot Module

------------------

- It is a spring module which provides Rapid Application Development (RAD)

feature to spring framework

- It is used to avoid writing spring bean XML configuration file


- Automatically configure spring by adding the project dependencies in pom.xml file

- Embedded tomcat server

Spring Boot IDE (Spring Tool Suite - STS)

-----------------------------------------

Google: sts download

url: https://spring.io/tools

Project Lombok tool

-------------------

Project Lombok tool is a Java Library tool that is used to minimize/remove

the boiler plate code and save the time of developers just by using some

annotations

Ex:

@NoArgsConstructor

@AllArgsConstructor

@Setter

@Getter

@ToString

@Data //@Setter, @Getter and @ToString

public class Employee

private int eno;

private String ename;

private double salary;

}
Download lombok

---------------

url: https://projectlombok.org/download

Developing a Spring Boot Application in STS

-------------------------------------------

- Create a table "Product" in MySQL

mysql>create table Product (pid int(3), pname varchar(10), price int(4));

- Create a Spring Starter Project in STS

Click on File -> New -> Spring Starter Project

Name : SpringBootProj

Type : MAven

Java Version : 8

Group : springboot

Artifact : SpringBootProj

Package : com.spring.boot

Click Next

In Available Search for the following dependencies and add

- Spring Web

- JDBC API

- MySQL Driver

- Lombok
Click Finish

- Add the following dependency in pom.xml file

<dependency>

<groupId>org.apache.tomcat</groupId>

<artifactId>tomcat-jasper</artifactId>

<version>10.0.10</version>

</dependency>

Refer program pom.xml

- Create a Javabean class "Product" in com.spring.boot package of src/main/java

folder of SpringBootProj

Refer program Product.java

- Create a folder "webapp" in src/main folder

- Create a welcome page "product.jsp" in src/main/webapp folder

Refer program product.jsp

- Create a Controller class "ProductController" in com.spring.boot package

Refer program ProductController.java

- Create a Service class "ProductService" in com.spring.boot package

Refer program ProductService.java


- Create a view page "successProduct.jsp" in src/main/webapp folder

Refer program successProduct.jsp

- Update application.properties file of src/main/resources folder

Refer program application.properties

- Run the project as Spring Boot App

Right click on SpringBootProj -> Run As -> Spring Boot App

- Open browser and type the following url

http://localhost:8080/product.jsp
Date:31/Jan/2023

----------------

Spring Data JPA Module (Java Persistence API)

----------------------

interface StudentDAO

CRUD operations

public class StudentDAOImpl implements StudentDAO

//implement interface methods

//overridden CRUD operations

interface EmployeeDAO

CRUD operations

public class EmployeDAOImpl implements EmployeeDAO

...

20 DAO interfaces => 20 DAO implementation classes

Spring Data JPA (Java Persistence API)

---------------

JpaRepository interface is used for Spring Data JPA Module


Ex:

interface EmployeeDAO extends JpaRepository<Employee,Integer>{}

interface StudentDAO extends JpaRespository<Student,Integer>{}

Note: Hibernate is an implementation of JPA

Spring DATA JPA methods

------------------------

- save()

- deleteById()

- findById() => primary key

- findAll()

Spring REST Module

------------------

Spring REST Module is used to create REST APIs

REST APIs are used to develop distributed applications which are used to interact

one application with another application

Spring Boot REST CRUD Application with POSTMAN Client

-----------------------------------------------------

Refer document Spring Boot REST CRUD Application with POSTMAN.docx

Refer source codes in SpringBootRESTCRUDProj.ZIP


Note

----

- No class tmw - 01/Feb @7PM (IST)

- Next class will be on 02/Feb @7PM (IST)

02/Feb => Microservices

03/Feb => Microservices

06/Feb => Angular

07/Feb => Angular

08/Feb => Angular

You might also like