28
Spring Boot! Jakub Kubryński / kubrynski.com [email protected] @jkubrynski

Introduction to Spring Boot!

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction to Spring Boot!

Spring Boot!

Jakub Kubryński / [email protected]

@jkubrynski

Page 2: Introduction to Spring Boot!

whoami

kubrynski.com @[email protected]

Page 3: Introduction to Spring Boot!

History

• 1999 J2EE 1.2

• 2001 xDoclet 1.0

• 2004 Spring Framework 1.0● Injection● POJO oriented● AOP & transactions

• 2006 Java EE 5

• 2013 Spring Boot!

kubrynski.com @[email protected]

Page 4: Introduction to Spring Boot!

Focus

source: spring.io

kubrynski.com @[email protected]

Page 5: Introduction to Spring Boot!

Revolution

@RestController@EnableAutoConfigurationpublic class HelloWorld {

@RequestMapping("/") public String helloWorld() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(HelloWorld.class, args); }

}

kubrynski.com @[email protected]

Page 6: Introduction to Spring Boot!

Key features

• Stand-alone Spring applications

• No code generation / no XML config

• Automatic configuration

• Starter dependencies

• Embedded Tomcat or Jetty

• Production-ready environment

kubrynski.com @[email protected]

Page 7: Introduction to Spring Boot!

Blocks

• SpringApplication

• @EnableAutoConfiguration

• @ConditionalOnClass

• @ConditionalOnBean

• @ConditionalOnExpression

kubrynski.com @[email protected]

@...OnMissing...

Page 8: Introduction to Spring Boot!

Sample auto-configuration

@Configuration

@ConditionalOnClass({ MBeanExporter.class })

@ConditionalOnMissingBean({ MBeanExporter.class })

@ConditionalOnExpression("${spring.jmx.enabled:true}")

public class JmxAutoConfiguration {

...

}

kubrynski.com @[email protected]

Page 9: Introduction to Spring Boot!

Available auto-configurations

● Batch● Spring Data, JPA, Hibernate, Mongo, Redis● JMS, RabbitMQ (AMQP)● Reactor● Security● WebMVC, Thymeleaf, Websocket● Tomcat and Jetty

kubrynski.com @[email protected]

Page 10: Introduction to Spring Boot!

Starters

• spring-boot-starter

• spring-boot-starter-web

• spring-boot-starter-test

• spring-boot-starter-actuator

kubrynski.com @[email protected]

Page 11: Introduction to Spring Boot!

Starters

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

kubrynski.com @[email protected]

Page 12: Introduction to Spring Boot!

Properties

public class MailProperties {

@Value(“serverAddress”)

private InetAddress serverAddress;

}

java – jar app.jar –serverAddress=10.0.0.1

kubrynski.com @[email protected]

Page 13: Introduction to Spring Boot!

Properties

@ConfigurationProperties(prefix=”mail”)

public class MailProperties {

private InetAddress serverAddress;

private Resource template;

}

mail.serverAddress : 84.123.456.32

mail.template : classpath:mail.vm

kubrynski.com @[email protected]

Page 14: Introduction to Spring Boot!

Properties

@ConfigurationProperties(prefix=”mail”)

public class MailProperties {

private InetAddress serverAddress;

private Resource template;

}

mail:

serverAddress : 84.123.456.32

template : classpath:mail.vm

kubrynski.com @[email protected]

Page 15: Introduction to Spring Boot!

Tests

@SpringApplicationConfiguration(classes =

Application.class)

@ContextConfiguration(classes = Application,

loader = SpringApplicationContextLoader)

@IntegrationTest

kubrynski.com @[email protected]

Page 16: Introduction to Spring Boot!

Profiles

• default file is application.properties

• customize by spring.config.name

• spring.profiles.active = production,mysql

• configuration per profile:● application-production.properties● conference-test.properties

kubrynski.com @[email protected]

Page 17: Introduction to Spring Boot!

Logging

• Log4J

• Logback

• Java Util Logging

kubrynski.com @[email protected]

Page 18: Introduction to Spring Boot!

Security

• spring-boot-starter-security

• @SecurityAutoConfiguration

• SecurityProperties● security.requireSsl = true● security.enableCsrf = true

kubrynski.com @[email protected]

Page 19: Introduction to Spring Boot!

User Interface

• Thymeleaf

• Template engine for XML/XHTML/HTML5

• Customizable● spring.thymeleaf.prefix=classpath:/templates/● spring.thymeleaf.suffix=.html● spring.thymeleaf.mode=HTML5● spring.thymeleaf.encoding=UTF-8

kubrynski.com @[email protected]

Page 20: Introduction to Spring Boot!

Production ready

• Monitoring endpoints● /health● /info● /metrics● /trace

• JMX / SSH

• Auditing

kubrynski.com @[email protected]

Page 21: Introduction to Spring Boot!

WAR

@EnableAutoConfiguration

public class WebApp extends SpringBootServletInitializer{

@Override

protected SpringApplicationBuilder

configure(SpringApplicationBuilder application) {

return application.sources(SampleApplication.class);

}

}

kubrynski.com @[email protected]

Page 22: Introduction to Spring Boot!

Monitoring application

● ApplicationPidListener

● SpringApplication.addListeners(...)

● META-INF/spring.factoriesorg.springframework.context.ApplicationListener=\

org.springframework.boot.actuate.system.ApplicationPidListener

kubrynski.com @[email protected]

Page 23: Introduction to Spring Boot!

Rapid prototyping

● Spring Boot CLI

● $ spring run app.groovy

● $ spring test tests.groovy

kubrynski.com @[email protected]

Page 24: Introduction to Spring Boot!

How does it help?

● Dramatically reduces boilerplate code

● Enables polyglot

● Simplifies integration testing

● Simplifies environment maintenance

kubrynski.com @[email protected]

Page 25: Introduction to Spring Boot!

It's Spring

kubrynski.com @[email protected]

Page 26: Introduction to Spring Boot!

Live coding

“Talk is cheap – show me the code”

kubrynski.com @[email protected]

Page 27: Introduction to Spring Boot!

You have questions

I (probably) have answers

kubrynski.com @[email protected]

Page 28: Introduction to Spring Boot!

END! THANK YOU

kubrynski.com @[email protected]