0% found this document useful (0 votes)
86 views

1 - Spring-Boot 2

Uploaded by

Rahul Gupta
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)
86 views

1 - Spring-Boot 2

Uploaded by

Rahul Gupta
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/ 15

www.jtcindia.

org 1 Spring Boot 2


1) Spring offers a powerful way to build web applications, with support for
dependency injection, transaction management, polyglot persistence, application
security, first-hand REST API support, an MVC framework and a lot more.

2) Spring applications have always required significant configuration and, which


sometimes build up a lot of complexity during development. That’s where Spring
Boot comes in.

3) The Spring Boot project aims to make building web application with Spring much
faster and easier. The guiding principle of Boot is convention over configuration.

Important features in Spring Boot:

1. Spring Boot Starters


2. Auto Configurations

3. Spring Boot Initializer

4. Spring Boot CLI


5. Dev Tools

6. Externalized Configurations

7. YAML Support

8. Embedded Containers
9. Actuators

10.Spring Boot Admin

www.jtcindia.org 2 Spring Boot 2


Lab1 : Working Steps:
A) Setup Database
create database myjtcdb;
use myjtcdb;
create table
mycustomers( cid int
primary key,
cname char(10),
email char(10),
phone long,
city char(10)
);

B) Create the maven Project.


1) Open the Eclipse required Workspace
2) Select New -> Maven Project.
3) Check the checkbox for create Simple Project(skip the Archetype selection) and Click
on Next Button.
4) Provide the following
Group Id: com.jtcindia.spring Artifact Id :
Lab1
Version : 1.0
packaging : jar
Name : MyLab1
and Click On finish button.

5) Add the maven-compiler-plugin


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
6) You can see the Error in Maven Project.
Fix the Error by updating the Maven Project.
Select the Project , right click and then select Maven -> Update Project.
www.jtcindia.org 3 Spring Boot 2
7) Update pom.xml by adding the following dependencies.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version5.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version5.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

8) Write JTCAppConfig.java ( Bean Configuration class)


 Configure DataSource.
 Configure JdbcTemplate

9) Write the following Files


 Customer.java
 CustomerRowMapper.java

10) Write the DAO and its Implementation class


 CustomerDAO.java
 CustomerDAOImpl.java

11) Write Client Program - Lab1 and Run it.

www.jtcindia.org 4 Spring Boot 2


Lab1: Files required

1. Lab1.java
2. CustomerDAO.java
3. CustomerDAOImpl.java
4. Customer.java
5. CustomerRowMapper.java
6. JTCAppConfig.java
7. pom.xml

1) Lab1.java
package com.jtcindia.spring;

import java.util.List;
import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */
public class Lab1 {

public static void main(String[] args) {

AnnotationConfigApplicationContext ctx=new
AnnotationConfigApplicationContext(JTCConfig.class);
CustomerDAO cdao=(CustomerDAO) ctx.getBean("custDAO");

// 1. addCustomer
Customer cust1 = new Customer(104, "SP", "SP@jtc", 9999, "Noida");
cdao.addCustomer(cust1);

// 2. getAllCustomers
System.out.println("getAllCustomers");
List<Customer> list=cdao.getAllCustomers();
list.forEach( cust -> System.out.println(cust) );
}
}

www.jtcindia.org 5 Spring Boot 2


2) CustomerDAO.java
package com.jtcindia.spring;

import java.util.List;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */
public interface CustomerDAO {
public void addCustomer(Customer cust);
public List<Customer> getAllCustomers();
}

3) CustomerDAOImpl.java
package com.jtcindia.spring;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */
@Repository("custDAO")
public class CustomerDAOImpl implements CustomerDAO {

@Autowired
JdbcTemplate jdbcTemp;

public void addCustomer(Customer cust) {


String sql = "insert into customers values(?,?,?,?,?)";
jdbcTemp.update(sql, cust.getCid(), cust.getCname(), cust.getEmail(), cust.getPhone(), cust.getCity());
}

public List<Customer> getAllCustomers()


{ String sql = "select * from customers";
List<Customer> list = jdbcTemp.query(sql, new CustomerRowMapper());
return list;
}

www.jtcindia.org 6 Spring Boot 2


4) Customer.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */
public class Customer
{ private int cid;
private String cname;
private String email;
private long phone;
private String city;

//Constructors
//Setters and Getters

public String toString() {


return "" + cid + "\t" + cname + "\t" + email + "\t" + phone + "\t"+ city;
}
}

5) CustomerRowMapper.java
package com.jtcindia.spring;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */
public class CustomerRowMapper implements RowMapper<Customer>
{ @Override
public Customer mapRow(ResultSet rs, int rn) throws SQLException
{ Customer cust=new Customer();
cust.setCid(rs.getInt(1));
cust.setCname(rs.getString(2));
cust.setEmail(rs.getString(3));
cust.setPhone(rs.getLong(4));
cust.setCity(rs.getString(5));
return cust;
}
}

www.jtcindia.org 7 Spring Boot 2


6) JTCAppConfig.java
package com.jtcindia.spring;

import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */
@Configuration
@ComponentScan(basePackages = {"com.jtcindia.spring"}) public
class JTCAppConfig {
@Bean
public DataSource dataSource() {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://localhost:3306/myjtcdb");
bds.setUsername("root"); bds.setPassword("jtcsom");
bds.setInitialSize(10);
bds.setMaxActive(15);
return bds;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource ds)
{ return new JdbcTemplate(ds);
}
}
7) pom.xml
<project ……>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.spring</groupId>
<artifactId>Lab1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyLab1</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<spring.version>5.1.9.RELEASE</spring.version>

www.jtcindia.org 8 Spring Boot 2


</properties>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

www.jtcindia.org 9 Spring Boot 2


Problems:
1) We need to update the pom.xml when ever I need some extra jars / replacing
Existing jars. (pom.xml)

2) Configuring Beans in Configuration class.(JTCAppConfig.java)

3) Hard-coding Configuration Data/Parameters (What happens when I change


the Configuration Data).

How SpringBoot Solves the Above Problems.


1) Starters.
 Gets All the Dependent Compatible Jars with given starter name.
 spring-boot-starter-jdbc
 spring-boot-starter-data-jpa
 spring-boot-starter-web

2) AutoConfiguration
 Most of the Beans will be configured by the Spring Container automatically.
 Beans will be configured conditionally based availability of Classes in the
Classpath.

3) Centralizing the Configuration Data


 You can centralize the Configuration Data/Parameters in Property Files or
YAML Files

Lab2 : Working Steps:


A) Setup Database
Same as Lab1

B) Create the maven Project.


1) Open the Eclipse required Workspace
2) Select New -> Maven Project.
3) Check the checkbox for create Simple Project(skip the Archetype selection) and Click
on Next Button.
4) Provide the following
Group Id: com.jtcindia.springboot
Artifact Id : Lab2
Version : 1.0
packaging : jar

www.jtcindia.org 10 Spring Boot 2


Name :MyLab2and
Click On finish button.

C) Start the Example Code.


1) Remove the following two Source Folders.
a. src/test/java
b. src/test/resources

2) Add the spring-boot-maven-plugin in pom.xml

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

3) Update pom.xml by adding the following dependencies.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

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

</dependencies>

4) You can see the Error in Maven Project.


Fix the Error by updating the Maven Project.
Select the Project , right click and then select Maven -> Update Project.

www.jtcindia.org 11 Spring Boot 2


5) Create application.properties under src/main/resources
6) Add the following properties in application.properties

#Database Properties
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb
spring.datasource.username=root spring.datasource.password=jtcsom

#Hikari Properties
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5

#Log Properties
logging.level.root=DEBUG
logging.pattern.console=%-5level %logger{36} - %msg%n

7) Write JTCAppConfig.java ( Bean Configuration class)


 No Beans to Configure

@Configuration
@ComponentScan(basePackages = {"com.jtcindia.springboot"})
public class JTCAppConfig {

8) Write the following Files (Copy from Lab1)


 Customer.java
 CustomerRowMapper.java

9) Write the DAO and its Implementation class (Copy from Lab1)
 CustomerDAO.java
 CustomerDAOImpl.java

10) Write Boot Main Class - MyBootApp and Run it.

Run As => Spring Boot Application

www.jtcindia.org 12 Spring Boot 2


Lab2: Files required

1. MyBootApp.java
2. CustomerDAO.java Same As Lab1
3. CustomerDAOImpl.java Same As Lab1
4. Customer.java Same As Lab1
5. CustomerRowMapper.java Same As Lab1
6. JTCAppConfig.java
7. application.properties
8. pom.xml

1) MyBootApp.java
package com.jtcindia.springboot;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */
@SpringBootApplication
public class MyBootApp implements CommandLineRunner {

@Autowired
CustomerDAO cdao;

public static void main(String[] args)


{ SpringApplication.run(MyBootApp.class, args);
}

@Override
public void run(String... args)
{ System.out.println("MyBootApp..run() method-- Starts");
System.out.println ("------------------------------------ ");

// 1. addCustomer
Customer cust1 = new Customer(98, "SP", "SP@jtc", 9999, "Noida");
cdao.addCustomer(cust1);

www.jtcindia.org 13 Spring Boot 2


// 2. getAllCustomers
System.out.println("getAllCustomers");
List<Customer> list=cdao.getAllCustomers();
list.forEach(cust -> System.out.println(cust) );

System.out.println ("------------------------------------ ");


System.out.println("MyBootApp..run() method-------Ends");
}
}

6) JTCAppConfig.java
package com.jtcindia.springboot;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash Rai
* @company : JTCINDIA
* */

@Configuration
@ComponentScan(basePackages = {"com.jtcindia.springboot"})
public class JTCAppConfig {

7) application.properties

#Database Properties
spring.datasource.url=jdbc:mysql://localhost:3306/jtcdb
spring.datasource.username=root spring.datasource.password=jtcsom

#Hikari Properties
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5

#Log Properties
logging.level.root=DEBUG
logging.pattern.console=%-5level %logger{36} - %msg%n

www.jtcindia.org 14 Spring Boot 2


8) pom.xml
<project …..>
<modelVersion>4.0.0</modelVersion>

<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab2</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>MyLab2</name>
<url>http://www.jtcindia.com</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

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

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

www.jtcindia.org 15 Spring Boot 2

You might also like