New Sprinboot
New Sprinboot
Objectives: In this Module, You will learn Data Access with Spring Boot Applications with JDBC, Data
Sources and Various Spring Boot features.
Topics:
Spring Boot - 2
1) Spring offers a powerful way to build web applications, with support for dependency
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
2. Auto Configurations
5. Dev Tools
6. Externalized Configurations
7. YAML Support
8. Embedded Containers
9. Actuators
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.spring</groupId>
<artifactId>Lab1-spring-jdbc</artifactId>
<version>1.0</version>
<name>Lab1-spring-jdbc</name>
<description>This is Spring and JDBC 1) Provide the following
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.spring</groupId>
<artifactId>Lab1-spring-jdbc</artifactId>
<version>1.0</version>
<name>Lab1-spring-jdbc</name>
<description>This is Spring and JDBC example</description>
<url>https://www.jtcindia.org</url>
<properties>
<java.version>1.8</java.version>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
<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></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>8.0.27</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
</build>
</project>
<url>https://www.jtcindia.org</url>
<properties>
<java.version>1.8</java.version>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</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>8.0.27</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>
• Configure DataSource.
• Configure JdbcTemplate
• Customer.java
• CustomerRowMapper.java
• CustomerDAO.java
• CustomerDAOImpl.java
1. MyClient.java
2. CustomerDAO.java
3. CustomerDAOImpl.java
4. Customer.java
5. CustomerRowMapper.java
6. JTCAppConfig.java
7. pom.xml
MyClient.java
package com.jtcindia.spring;
import java.util.List;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyClient {
public static void main(String[] args) {
ApplicationContext ctx = new
AnnotationConfigApplicationContext(JTCAppConfig.class);
CustomerDAO custDAO = (CustomerDAO) ctx.getBean("custDAO");
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository("custDAO")
@Autowired
JdbcTemplate jtemp;
@Override
jtemp.update(SQL,
cust.getCid(),cust.getCname(),cust.getEmail(),cust.getPhone(),cust.getCity());
@Override
return mylist;
}
@Override
public List<Customer> getCustomersByCity(String city) {
String SQL="select * from mycustomers where city=?";
List<Customer> mylist = jtemp.query(SQL, new CustomerRowMapper(),city);
return mylist;
}
}
Customer.java
package com.jtcindia.spring;
public class Customer {
private int cid;
private String cname;
private String email;
private int phone;
private String city;
public Customer() {}
public Customer(String cname, String email, int phone, String city){
this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
}
public Customer(int cid, String cname, String email, int phone, String city) {
super();
this.cid = cid;
this.cname = cname;
this.email = email;
this.phone = phone;
this.city = city;
}
//Setters and Getters
@Override
public String toString() {
return " [" + cid + ", " + cname + ", " + email + ", " + phone + ", " + city + "]";
}
}
CustomerRowMapper.java
package com.jtcindia.spring;
import java.sql.ResultSet;
importjava.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class CustomerRowMapper implements RowMapper<Customer> {
@Override
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
Customer cust=new Customer();
cust.setCid(rs.getInt(1));
cust.setCname(rs.getString(2));
cust.setEmail(rs.getString(3));
cust.setPhone(rs.getInt(4));
cust.setCity(rs.getString(5));
return cust;
}
}
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;
@Configuration
@ComponentScan(basePackages = {"com.jtcindia.spring"})
public class JTCAppConfig {
@Bean(name = "dataSource")
public DataSource getDataSource() {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://localhost:3306/myjtcdb5");
bds.setUsername("root");
bds.setPassword("india");
bds.setMaxActive(5);
return bds;
}
@Bean(name = "jtemp")
public JdbcTemplate getJdbcTemplate(DataSource dataSource){
JdbcTemplate jtemp = new JdbcTemplate(dataSource);
return jtemp;
}
}
Problems:
1 We need to specify many dependecies for getting different Jars belogs to
different technologies. (with pom.xml)
=> What the Jars to download
2 We need to check Version Compatability and need to spacify the Version
Compatability (with pom.xml)
=> What version of Jar to download
3 Complexity in Configuring many Beans in Bean Configuration class
4 Hard-Coding Bean Properties in Bean Configuration class
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-data-mongodb
spring-boot-starter-web
spring-boot-starter-thymeleaf
spring-boot-starter-security
spring-boot-starter-mail
spring-boot-starter-test
2. AutoConfiguration
Most of the Beans will be configured by the Spring Container automatically.
Classpath
YAML Files
use myjtcdb5;
cname char(10),
email char(10),
phone long,
city char(10)
);
3. Check the checkbox for create Simple Project (skip the Archetype
Artifact Id : Lab2-boot-jdbc
Version : 1.0
packaging : jar
Name : Lab2-boot-jdbc
a. src/test/java
b. src/test/resources
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.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>
Select the Project, right click and then select Maven -> Update Project.
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb5
spring.datasource.username=root
spring.datasource.password=india
#Hikari Properties
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
#Log Properties
logging.level.root=DEBUG
No Beans to Configure
@SpringBootApplication
}
www.jtcindia.org Spring Boot
SPRING BOOT PART-1
• Customer.java
• CustomerRowMapper.java
from Lab1)
• CustomerDAO.java
• CustomerDAOImpl.java
Run it.
1) MyBootApplication.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;
@SpringBootApplication
@Autowired
CustomerDAOcustDAO;
@Override
custDAO.addCustomer(cust);
System.out.println("Done!!!");
SpringApplication.run(MyBootApplication.class, args);
6. JTCAppConfig.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
7. application.properties
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb5
spring.datasource.username=root
spring.datasource.password=india
#Hikari Properties
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.hikari.idle-timeout=20000
#Log Properties
logging.level.root=INFO
logging.pattern.console=%-5level %logger{36} - %m %n
8. pom.xml
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab2-boot-jdbc</artifactId>
<version>1.0</version>
<name>Lab2-boot-jdbc</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.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>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
A) Exclude HikariCP
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
spring.datasource.tomcat.initialSize=5
spring.datasource.tomcat.max-active=25
6. Application.properties
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb
spring.datasource.username=root
spring.datasource.password=india
#Tomcat Properties
spring.datasource.tomcat.initialSize=5
spring.datasource.tomcat.max-active=25
#Log Properties
logging.level.root=INFO
7. pom.xml
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab3-boot-jdbc</artifactId>
<version>1.0</version>
<name>Lab3-boot-jdbc</name>
<url>https://www.jtcindia.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</plugins>
</build>
</project>
A) Exclude HikariCP
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
3) Update application.properties
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true
6. application.properties
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb
spring.datasource.username=root
spring.datasource.password=india
#DBCP2 Properties
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true
#Log Properties
logging.level.root=INFO
7. pom.xml
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab 4-boot-jdbc</artifactId>
<version>1.0</version>
<name>Lab4-boot-jdbc</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
A) Include HikariCP
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
3. Update application.properties
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
spring.datasource.tomcat.initialSize=5
spring.datasource.tomcat.max-active=25
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true
6. application.properties
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb
spring.datasource.username=root
spring.datasource.password=india
#Hikari Properties
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
#Tomcat Properties
spring.datasource.tomcat.initialSize=5
spring.datasource.tomcat.max-active=25
#DBCP2 Properties
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true
#Log Properties
logging.level.root=INFO
7) pom.xml
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
A) Include HikariCP
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
3) Update application.properties
spring.datasource.tomcat.initialSize=5
spring.datasource.tomcat.max-active=25
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true
6. application.properties
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb
spring.datasource.username=root
spring.datasource.password=india
#Tomcat Properties
spring.datasource.tomcat.initialSize=5
spring.datasource.tomcat.max-active=25
#DBCP2 Properties
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.max-total=20
spring.datasource.dbcp2.pool-prepared-statements=true
#Log Properties
logging.level.root=INFO
7. pom.xml
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<version>1.0</version>
<url>https://www.jtcindia.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
Using NamedParameterJdbcTemplate
1. MyBootApplication.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;
@SpringBootApplication
@Autowired
CustomerDAO custDAO;
@Override
custDAO.addCustomer(cust);
System.out.println(email);
System.out.println("Done!!!");
SpringApplication.run(MyBootApplication.class, args);
2. CustomerDAO.java
package com.jtcindia. springboot;
import java.util.List;
import java.util.Map;
3. CustomerDAOImpl.java
package com.jtcindia. springboot;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTe
mplate;
import org.springframework.stereotype.Repository;
@Repository
@Autowired
NamedParameterJdbcTemplate namedJTemp;
@Autowired
JdbcTemplate jtemp;
@Override
values(:mycid,:mycname,:myemail,:myphone,:mycity)";
params.put("mycid", cust.getCid());
params.put("mycname", cust.getCname());
params.put("myemail", cust.getEmail());
params.put("myphone", cust.getPhone());
params.put("mycity", cust.getCity());
namedJTemp.update(SQL, params);
@Override
values(:mycid,:mycname,:myemail,:myphone,:mycity)";
namedJTemp.update(SQL, map);
@Override
CustomerRowMapper());
return mylist;
@Override
params.put("mycity", city);
CustomerRowMapper());
return mylist;
params.put("myphone", phone);
return email;
6. application.properties
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb5
spring.datasource.username=root
spring.datasource.password=india
#Hikari Properties
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.hikari.idle-timeout=20000
#Log Properties
logging.level.root=DEBUG
logging.pattern.console=%-5level %logger{36} - %m %n
7. pom.xml
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab7-boot-jdbc</artifactId>
<version>1.0</version>
<name>Lab7-boot-jdbc</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.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>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
• HelloDAO.java
• HaiDAO.java
• HelloService.java
• HaiService.java
• Hello.java
• Hai.java
• HaiDAO
• HaiService
• HelloDAO
• HelloService
• Hello
• Hai
• DataSource
• JdbcTemplate
• DataSourceTransactionManager
• NamedParameterJdbcTemplate
• ApplicationContext
Container or Not.?
• MyBootApp
• JTCAppConfig
1. MyBootApplication.java
2. JTCAppConfig.java
3. HelloDAO.java 4. HaiDAO.java
4. HelloService.java 6. HaiService.java
5. Hello.java
6. Hai.java
7. application.properties
8. pom.xml
1) MyBootApplication.java
package com.jtcindia;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTe
mplate;
import
org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.jtcindia.dao.HaiDAO;
import com.jtcindia.dao.HelloDAO;
import com.jtcindia.service.HaiService;
import com.jtcindia.service.HelloService;
import com.jtcindia.to.Hai;
import com.jtcindia.to.Hello;
@SpringBootApplication
@Autowired
ApplicationContext ctx;
@Autowired
DataSource dataSource;
@Autowired
JdbcTemplate jtemp;
@Autowired
NamedParameterJdbcTemplate npjtemp;
@Autowired
DataSourceTransactionManager txManager;
@Autowired(required = false)
HelloDAO helloDAO;
@Autowired(required = false)
HelloService helloService;
@Autowired
HaiDAO haiDAO;
@Autowired
HaiService haiService;
@Autowired
Hello hello;
@Autowired
Hai hai;
for(String beanName:beanNames) {
System.out.println(beanName);
System.out.println("Done!!!");
System.out.println("Main Begin");
SpringApplication.run(MyBootApplication.class, args);
System.out.println("Main end");
@Bean(name="myhello1")
@Bean(name="myhello2")
@Bean(name="myhai2")
2) JTCAppConfig.java
package com.jtcindia;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.jtcindia.dao.HaiDAO;
import com.jtcindia.service.HaiService;
@SpringBootApplication
@Bean(name="myhaiDAO")
@Bean(name="myhaiService")
publicHaiService getHaiService() {
3) HelloDAO.java
package com.jtcindia.dao;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
@Autowired(required = false)
JdbcTemplate jtemp;
@Autowired(required = false)
DataSource ds;
public HelloDAO() {
System.out.println("HelloDAO-D.C");
System.out.println("A." + ds);
System.out.println("B." + jtemp);
4) HaiDAO.java
package com.jtcindia.dao;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@Autowired(required = false)
JdbcTemplate jtemp;
@Autowired(required = false)
DataSource ds;
public HaiDAO() {
System.out.println("HaiDAO-D.C");
System.out.println("A." + ds);
System.out.println("B." + jtemp);
}
www.jtcindia.org Spring Boot
SPRING BOOT PART-1
5) HelloService.java
package com.jtcindia.service;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service
@Autowired(required = false)
JdbcTemplate jtemp;
@Autowired(required = false)
DataSource ds;
public HelloService(){
System.out.println("HelloService-D.C");
System.out.println("A." + ds);
System.out.println("B." + jtemp);
6) HaiService.java
package com.jtcindia.service;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@Autowired(required = false)
JdbcTemplate jtemp;
@Autowired(required = false)
DataSource ds;
public HaiService(){
System.out.println("HaiService-D.C");
System.out.println("A." + ds);
System.out.println("B." + jtemp);
7) Hello.java
package com.jtcindia.to;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public classHello {
@Autowired(required = false)
JdbcTemplate jtemp;
@Autowired(required = false)
DataSource ds;
public Hello() {
System.out.println("Hello-D.C");
System.out.println("A." + ds);
System.out.println("B." + jtemp);
8) Hai.java
package com.jtcindia.to;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
@Autowired(required = false)
JdbcTemplate jtemp;
@Autowired(required = false)
DataSource ds;
public Hai() {
System.out.println("Hai-D.C");
System.out.println("A." + ds);
System.out.println("B." + jtemp);
9) application.properties
#Database Properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myjtcdb5
spring.datasource.username=root
spring.datasource.password=india
#Log Properties
logging.level.root=INFO
logging.pattern.console=%-5level %logger{36} - %m %n
10) pom.xml
<project …>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia </groupId>
<artifactId>Lab8-boot-startup-tasks</artifactId>
<version>1.0</version>
<name>Lab8-boot-startup-tasks</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.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>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Interview Questions
Q1) What is Spring Boot?
Spring Framework.
Q2) What are the Spring Boot features discussed till now?
2) Auto Configuration
Ans: Each Starter brings some set of Jars and it’s Dependent and
Compatiable jars.
Spring Container Readily. You have to inject those beans where ever
you want.
Ans: No
Configuration class?
Ans: No
100 Beans)
path or not.
applications?
@Controller,@Service,@Repository .
NamedParameterJdbcTemplate,
DataSourceTranactionManager
EntityManagerFactory, EntityManager,
JpaTranactionManager, JpaTemplate
If above two cases are not possible then you have to configure the
Bean explicitly.
Ans: Using Maven Dependency , You can not get the Oracle Jar from
▪ Push Your jar into Maven Local repo by Specifing Group Id, Artifact
Id,Version
Id,Version
Party DataSources
▪ Hikari DataSource
▪ Tomcat DataSource
▪ DBCP2
▪ DBCP
▪ C3P0 DataSources
Application?
application.properties
Application?
Configured
Q15) How Can I exclude the Jars which are downloaded by Spring
Boot Starters?
Ans: You can not change the Built-In Spring Boot Starters related Jars.
Ans: Yes
Ans: TomcatCP
Ans: HikariCP
DataSource?
DBCP2 TomcatCP
excluding Hikari?
Queries?
1) Postional Parameters.
2) Named Parameters.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Parent Version.
Ans: Yes, You can specify the MySQL Version required explicitly.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
▪ @Configuration
▪ @ComponentScan(basePackages = "com.jtcindia.springboot")
▪ @EnableAutoConfiguration
• Check DB Connections
Ans: @Service
@Controller
@Repository
@Component
Ans: Yes
Q33) What Happens Internally When you run the Boot Application?
Ans: When you launch the Spring Boot Application then following Tasks
will happen
two parameters called Boot Main class and command line args
JTCAppConfig,MyBootApplication) *
Prepares the
Matches and
Negative Matches.
Positive matches:
a) DataSource
b) JdbcTemplate
c) NamedParameterJdbcTemplate
d) DataSourceTransactionManager
or not.