KEMBAR78
Spring Boot | PDF
SPRING BOOT
HONGSEONG JEON
HSJEON70@GMAIL.COM
MAY 24, 2016
Easy to create, run spring applications
INTRODUCING SPRING BOOT
 Easy to create stand-alone, production-grade Spring based
applications that you can “just run”.
 It needs very little spring configuration.
 Create Java applications that can be started using java –jar or
more traditional war deployments.
 Primary goals for the Spring Boot.
 Provide a radically faster and widely accessible getting started
experience for all Spring development.
 Be opinionated out of the box, but get out of the way quickly as
requirements start to diverge from the defaults.
 Provide a range of non-functional features that are common to
large classes of projects (e.g.embedded servers, security,
metrics, health checks, externalized configuration).
 Absolutely no code generation and no requirement for XML
configuration.
SYSTEM REQUIREMENTS
 Spring Boot 1.4.0 requires Java 7 and Spring Framework 4.1.5
or above.
 Servlet containers
 Servlet 3.0+ compatible container
DEVELOPMENT ENVIRONMENT
 Build tool that supports dependency management
 Maven – maven.apache.org
 Gradle – www.gradle.org
 Development IDE
 Eclipse or Any Editor
 Eclipse plugin
- m2ecipse
- gradle
SIMPLE APPLICATION
 New Gradle Project
 Create HelloApplication
SIMPLE APPLICATION
 Create HelloController
 Create application.properties in the resources
SIMPLE APPLICATION
 Run HelloApplication at the eclipse
 Run http://localhost:8010/hello
SPRING BOOT ADMIN
 Spring Boot Admin is a simple application to manage and
monitor your Spring Boot Applications.
 The applications register with our Spring Boot Admin Client
(via http) or are discovered using Spring Cloud (e.g. Eureka).
SPRING BOOT ADMIN APPLICATION
 Create New Gradle Project
 Add spring boot server libraries to project dependencies.
 Create SpringBootAdminApplication
dependencies {
compile 'de.codecentric:spring-boot-admin-server:1.3.2'
compile 'de.codecentric:spring-boot-admin-server-ui:1.3.2'
compile 'org.springframework.boot:spring-boot-starter-web:1.3.1.RELEASE'
}
SPRING BOOT ADMIN APPLICATION
 Application properties
# =======================
# Tomcat Configuration
# =======================
server.tomcat.max-threads=10
server.address=127.0.0.1
server.port=9090
# =======================
# Security Configuration
# =======================
security.user.name=admin
security.user.password=admin
management.security.role=SUPERUSER
management.security.enabled=false
SPRING BOOT ADMIN CLIENT
 Add spring-boot-admin-starter-client to project dependencies.
 Add run task in the build.gradle
dependencies {
compile 'de.codecentric:spring-boot-admin-starter-client:1.3.2‘
compile 'org.springframework.boot:spring-boot-starter-web:1.3.1.RELEASE'
}
task run (dependsOn: classes, type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'com.example.myproject.HelloApplication'
}
SPRING BOOT ADMIN CLIENT
 Add spring boot admin properties in the application.properties
# Tomcat Configuration
server.address=127.0.0.1
server.port=8010
# =======================
# JMX Configuration
#management.port=8011
#management.address=127.0.0.1
management.security.role=SUPERUSER
management.security.enabled=false
# ============================================
# Client Configuration for Spring Boot Admin
info.version=1.0
info.info=spring boot hello application
spring.boot.admin.client.name=hello
spring.boot.admin.url=http://127.0.0.1:9090
spring.boot.admin.username=admin
spring.boot.admin.password=admin
spring.boot.admin.client.health-url=http://localhost:8010/health
spring.boot.admin.client.service-url=http://localhost:8010
spring.boot.admin.client.management-url=http://localhost:8010
SPRING BOOT ADMIN TESTING
 Run HelloApplication
SPRING BOOT ADMIN TESTING
 Run SpringBootAdminApplication
 http://localhost:9090/
 Application Dashboard
SPRING BOOT ADMIN
 Applications Details
SPRING BOOT ADMIN
 Application Details
SPRING BOOT ADMIN
 Application Environment
SPRING BOOT ADMIN
 Application Logging
SPRING BOOT ADMIN
 Application JMX
SPRING BOOT ADMIN
 Application Threads
SPRING BOOT ADMIN
 Application Trace
DATABASE CONFIGURATION
 H2 Database
 H2 is written in Java and is easily runs as an embedded in-
memory database.
 Because it is an embedded in memory database, it makes your
build portable.
 H2 Database and Spring Boot
 By adding this dependency to your gradle, Sring Boot will
automatically configure the H2 database.
 JDBC DataSource Configuration in the application.properties.
dependencies {
compile 'com.h2database:h2'
}
spring.datasource.jndi-name=java:jboss/datasources/customers
DATABASE CONFIGURATION
 JDBC DataSource Configuration in the application.properties
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url = jdbc:h2:d:/applications/springBoot/data/boot.db
# JDBC Driver class
spring.datasource.driver-class-name=org.h2.Driver
# Username and password
spring.datasource.username = sa
spring.datasource.password = sa
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same
time.
spring.datasource.max-active=50
# Validate the connection before borrowing it from the pool.
spring.datasource.test-on-borrow=true
DATABASE CONFIGURATION
 H2 Database Web Console
 Enable H2 Database Web Console in the application.properties
 H2 Database Web Console : http://${serviceUrl}/h2-console/
# Enable H2 Database Web Console
spring.h2.console.enabled=true
DATABASE CONFIGURATION
 Spring Boot Admin – Database Monitoring
SPRING DATA JPA
 Using Spring Data JPA can save you a lot of time when
interacting with the database.
 Spring Data JPA implements the Repository Pattern.
 Domain Driven Design
 Create a Persistence Repository
 Defining a repository for our City domain class is as simple as
defining a interface and extending the CrudRepository
interface.
SPRING DATA JPA
 Create a City Loader component
SPRING DATA JPA
 Run Application
SPRING DATA JPA
 Confirm loaded data
SPRING REST CONTROLLER
 Create a CityService interface
 Create a CityServiceImpl class
SPRING REST CONTROLLER
 Create a CityController
 Testing
SPRING BOOT EHCACHE
 Gradle Dependency for ehcache
 Ehcache configuration – config/ehcache.xml
dependencies {
compile 'net.sf.ehcache:ehcache-core:2.6.10'
}
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"monitoring="autodetect"dynamicConfig="true" statistics="true">
<diskStore path="java.io.tmpdir" />
<cache name="myCache"
maxEntriesLocalHeap="5000“ maxEntriesLocalDisk="1000" eternal="false"
diskSpoolBufferSizeMB="20“ timeToIdleSeconds="300"
timeToLiveSeconds="600“ memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
SPRING BOOT EHCACHE
 Enable Cache and Define a CacheManager bean in the
Application class.
SPRING BOOT EHCACHE
 Apply Cacheable annotation
 Run and Testing
SPRING BOOT EHCACHE
 Ehcache Statistics on the Spring Boot Admin
SPRING BOOT WEB APPLICATION
 Thymeleaf is a modern server-side Java template engine for
both web and standalone environments.
 Thymeleaf is ideal for modern-day HTML5 JVM web
development
 http://www.thymeleaf.org/
 Gradle Dependency for Thymeleaf
 Create a HelloController
dependencies {
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
SPRING BOOT WEB APPLICATION
 Create a greeting.html
 src/main/resources/templates/greeting.html
 Run and Testing
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
SPRING BOOT WEB APPLICATION
 Spring Boot Admin – HTTP API Monitoring
SPRING BOOT APPLICATION
 Project Package Layout
Spring Boot main application class
 Resources Layout
REFERENCES
 https://projects.spring.io/spring-framework/
 http://projects.spring.io/spring-boot/
 http://codecentric.github.io/spring-boot-admin/1.3.0/
 http://www.thymeleaf.org/
 http://gradle.org/
 http://www.h2database.com/html/main.html
 Example projects repository
- https://bitbucket.org/jlookexamples/examples-spring-boot-admin
- https://bitbucket.org/jlookexamples/examples-spring-boot
- https://bitbucket.org/jlookexamples/examples-hello
Q & A

Spring Boot

  • 1.
    SPRING BOOT HONGSEONG JEON HSJEON70@GMAIL.COM MAY24, 2016 Easy to create, run spring applications
  • 2.
    INTRODUCING SPRING BOOT Easy to create stand-alone, production-grade Spring based applications that you can “just run”.  It needs very little spring configuration.  Create Java applications that can be started using java –jar or more traditional war deployments.  Primary goals for the Spring Boot.  Provide a radically faster and widely accessible getting started experience for all Spring development.  Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.  Provide a range of non-functional features that are common to large classes of projects (e.g.embedded servers, security, metrics, health checks, externalized configuration).  Absolutely no code generation and no requirement for XML configuration.
  • 3.
    SYSTEM REQUIREMENTS  SpringBoot 1.4.0 requires Java 7 and Spring Framework 4.1.5 or above.  Servlet containers  Servlet 3.0+ compatible container
  • 4.
    DEVELOPMENT ENVIRONMENT  Buildtool that supports dependency management  Maven – maven.apache.org  Gradle – www.gradle.org  Development IDE  Eclipse or Any Editor  Eclipse plugin - m2ecipse - gradle
  • 5.
    SIMPLE APPLICATION  NewGradle Project  Create HelloApplication
  • 6.
    SIMPLE APPLICATION  CreateHelloController  Create application.properties in the resources
  • 7.
    SIMPLE APPLICATION  RunHelloApplication at the eclipse  Run http://localhost:8010/hello
  • 8.
    SPRING BOOT ADMIN Spring Boot Admin is a simple application to manage and monitor your Spring Boot Applications.  The applications register with our Spring Boot Admin Client (via http) or are discovered using Spring Cloud (e.g. Eureka).
  • 9.
    SPRING BOOT ADMINAPPLICATION  Create New Gradle Project  Add spring boot server libraries to project dependencies.  Create SpringBootAdminApplication dependencies { compile 'de.codecentric:spring-boot-admin-server:1.3.2' compile 'de.codecentric:spring-boot-admin-server-ui:1.3.2' compile 'org.springframework.boot:spring-boot-starter-web:1.3.1.RELEASE' }
  • 10.
    SPRING BOOT ADMINAPPLICATION  Application properties # ======================= # Tomcat Configuration # ======================= server.tomcat.max-threads=10 server.address=127.0.0.1 server.port=9090 # ======================= # Security Configuration # ======================= security.user.name=admin security.user.password=admin management.security.role=SUPERUSER management.security.enabled=false
  • 11.
    SPRING BOOT ADMINCLIENT  Add spring-boot-admin-starter-client to project dependencies.  Add run task in the build.gradle dependencies { compile 'de.codecentric:spring-boot-admin-starter-client:1.3.2‘ compile 'org.springframework.boot:spring-boot-starter-web:1.3.1.RELEASE' } task run (dependsOn: classes, type: JavaExec) { classpath = sourceSets.main.runtimeClasspath main = 'com.example.myproject.HelloApplication' }
  • 12.
    SPRING BOOT ADMINCLIENT  Add spring boot admin properties in the application.properties # Tomcat Configuration server.address=127.0.0.1 server.port=8010 # ======================= # JMX Configuration #management.port=8011 #management.address=127.0.0.1 management.security.role=SUPERUSER management.security.enabled=false # ============================================ # Client Configuration for Spring Boot Admin info.version=1.0 info.info=spring boot hello application spring.boot.admin.client.name=hello spring.boot.admin.url=http://127.0.0.1:9090 spring.boot.admin.username=admin spring.boot.admin.password=admin spring.boot.admin.client.health-url=http://localhost:8010/health spring.boot.admin.client.service-url=http://localhost:8010 spring.boot.admin.client.management-url=http://localhost:8010
  • 13.
    SPRING BOOT ADMINTESTING  Run HelloApplication
  • 14.
    SPRING BOOT ADMINTESTING  Run SpringBootAdminApplication  http://localhost:9090/  Application Dashboard
  • 15.
    SPRING BOOT ADMIN Applications Details
  • 16.
    SPRING BOOT ADMIN Application Details
  • 17.
    SPRING BOOT ADMIN Application Environment
  • 18.
    SPRING BOOT ADMIN Application Logging
  • 19.
    SPRING BOOT ADMIN Application JMX
  • 20.
    SPRING BOOT ADMIN Application Threads
  • 21.
    SPRING BOOT ADMIN Application Trace
  • 22.
    DATABASE CONFIGURATION  H2Database  H2 is written in Java and is easily runs as an embedded in- memory database.  Because it is an embedded in memory database, it makes your build portable.  H2 Database and Spring Boot  By adding this dependency to your gradle, Sring Boot will automatically configure the H2 database.  JDBC DataSource Configuration in the application.properties. dependencies { compile 'com.h2database:h2' } spring.datasource.jndi-name=java:jboss/datasources/customers
  • 23.
    DATABASE CONFIGURATION  JDBCDataSource Configuration in the application.properties # =============================== # = DATA SOURCE # =============================== # Set here configurations for the database connection spring.datasource.url = jdbc:h2:d:/applications/springBoot/data/boot.db # JDBC Driver class spring.datasource.driver-class-name=org.h2.Driver # Username and password spring.datasource.username = sa spring.datasource.password = sa # Keep the connection alive if idle for a long time (needed in production) spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 # Number of ms to wait before throwing an exception if no connection is available. spring.datasource.max-wait=10000 # Maximum number of active connections that can be allocated from this pool at the same time. spring.datasource.max-active=50 # Validate the connection before borrowing it from the pool. spring.datasource.test-on-borrow=true
  • 24.
    DATABASE CONFIGURATION  H2Database Web Console  Enable H2 Database Web Console in the application.properties  H2 Database Web Console : http://${serviceUrl}/h2-console/ # Enable H2 Database Web Console spring.h2.console.enabled=true
  • 25.
    DATABASE CONFIGURATION  SpringBoot Admin – Database Monitoring
  • 26.
    SPRING DATA JPA Using Spring Data JPA can save you a lot of time when interacting with the database.  Spring Data JPA implements the Repository Pattern.  Domain Driven Design  Create a Persistence Repository  Defining a repository for our City domain class is as simple as defining a interface and extending the CrudRepository interface.
  • 27.
    SPRING DATA JPA Create a City Loader component
  • 28.
    SPRING DATA JPA Run Application
  • 29.
    SPRING DATA JPA Confirm loaded data
  • 30.
    SPRING REST CONTROLLER Create a CityService interface  Create a CityServiceImpl class
  • 31.
    SPRING REST CONTROLLER Create a CityController  Testing
  • 32.
    SPRING BOOT EHCACHE Gradle Dependency for ehcache  Ehcache configuration – config/ehcache.xml dependencies { compile 'net.sf.ehcache:ehcache-core:2.6.10' } <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance"xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"monitoring="autodetect"dynamicConfig="true" statistics="true"> <diskStore path="java.io.tmpdir" /> <cache name="myCache" maxEntriesLocalHeap="5000“ maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20“ timeToIdleSeconds="300" timeToLiveSeconds="600“ memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localTempSwap" /> </cache> </ehcache>
  • 33.
    SPRING BOOT EHCACHE Enable Cache and Define a CacheManager bean in the Application class.
  • 34.
    SPRING BOOT EHCACHE Apply Cacheable annotation  Run and Testing
  • 35.
    SPRING BOOT EHCACHE Ehcache Statistics on the Spring Boot Admin
  • 36.
    SPRING BOOT WEBAPPLICATION  Thymeleaf is a modern server-side Java template engine for both web and standalone environments.  Thymeleaf is ideal for modern-day HTML5 JVM web development  http://www.thymeleaf.org/  Gradle Dependency for Thymeleaf  Create a HelloController dependencies { compile 'org.springframework.boot:spring-boot-starter-thymeleaf' }
  • 37.
    SPRING BOOT WEBAPPLICATION  Create a greeting.html  src/main/resources/templates/greeting.html  Run and Testing <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html>
  • 38.
    SPRING BOOT WEBAPPLICATION  Spring Boot Admin – HTTP API Monitoring
  • 39.
    SPRING BOOT APPLICATION Project Package Layout Spring Boot main application class  Resources Layout
  • 40.
    REFERENCES  https://projects.spring.io/spring-framework/  http://projects.spring.io/spring-boot/ http://codecentric.github.io/spring-boot-admin/1.3.0/  http://www.thymeleaf.org/  http://gradle.org/  http://www.h2database.com/html/main.html  Example projects repository - https://bitbucket.org/jlookexamples/examples-spring-boot-admin - https://bitbucket.org/jlookexamples/examples-spring-boot - https://bitbucket.org/jlookexamples/examples-hello
  • 41.