Spring boot introduction
-
Upload
rasheed-waraich -
Category
Technology
-
view
559 -
download
1
Transcript of Spring boot introduction
www.aurorasolutions.io www.aurorasolutions.io
Spring Boot Introduction
Presenter: Rasheed Amir
www.aurorasolutions.io www.aurorasolutions.io
Who is Rasheed?
❏ Programmer (Java, Groovy, C#, JavaScript). Architect. Agile Coach.❏ Co-founder Aurora Solutions, FixTelligent❏ Serial Entrepreneur❏ Certified Instructor for Spring Courses (Core, Web & Integration)❏ You can find me on LinkedIn
www.aurorasolutions.io www.aurorasolutions.io
Overview
www.aurorasolutions.io www.aurorasolutions.io
www.aurorasolutions.io www.aurorasolutions.io
www.aurorasolutions.io www.aurorasolutions.io
Spring Boot Goals
➔ Introduce developers to Spring Boot, an opinionated way to rapidly build production grade Spring applications quickly and with minimal fuss.
➔ 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!
www.aurorasolutions.io www.aurorasolutions.io
Spring Boot Goals...
➔ Single point of focus (as opposed to large collection of spring-* projects)
➔ A tool for getting started very quickly with Spring
➔ Common non-functional requirements for a "real" application
➔ Exposes a lot of useful features by default
➔ Gets out of the way quickly if you want to change defaults
www.aurorasolutions.io www.aurorasolutions.io
Installation
Spring Boot CLI
www.aurorasolutions.io www.aurorasolutions.io
Installation - Spring CLI
Spring CLI Installer -- Installer for the spring CLI command on Un*x-like system (should work on Linux, Mac or Cygwin).
You can curl http://start.spring.io/install.sh | sh, or download the script and run it.
www.aurorasolutions.io www.aurorasolutions.io
Getting Started Quickly!
in Groovy!
www.aurorasolutions.io www.aurorasolutions.io
Getting Started Quickly in Groovy!
Here’s a really simple web application that you can use to test your installation. Create a file called Welcome.groovy:
@RestController
class WelcomeToSpringBootMeetup {
@RequestMapping("/")
String home() {
"Welcome Everyone!"
}
}
$ spring run --watch Welcome.groovy
... application is running at http://localhost:8080
www.aurorasolutions.io www.aurorasolutions.io
How did it work?
// import org.springframework.web.bind.annotation.RestController// other imports …
@RestController
class WelcomeToSpringBootMeetup {
@RequestMapping("/")
String home() {
"Welcome Everyone!"
}
}
www.aurorasolutions.io www.aurorasolutions.io
How did it work?
// import org.springframework.web.bind.annotation.RestController// other imports …
// @Grab("org.springframework.boot:spring-boot-web-starter:0.5.0")
@RestController
class WelcomeToSpringBootMeetup {
@RequestMapping("/")
String home() {
"Welcome Everyone!"
}
}
www.aurorasolutions.io www.aurorasolutions.io
How did it work?
// import org.springframework.web.bind.annotation.RestController// other imports …
// @Grab("org.springframework.boot:spring-boot-web-starter:0.5.0")
// @EnableAutoConfiguration
@RestController
class WelcomeToSpringBootMeetup {
@RequestMapping("/")
String home() {
"Welcome Everyone!"
}
}
www.aurorasolutions.io www.aurorasolutions.io
How did it work?
// import org.springframework.web.bind.annotation.RestController// other imports …
// @Grab("org.springframework.boot:spring-boot-web-starter:0.5.0")
// @EnableAutoConfiguration
@RestController
class WelcomeToSpringBootMeetup {
@RequestMapping("/")
String home() {
"Welcome Everyone!"
}
// public static void main(String[] args) {
// SpringApplication.run(Example.class, args);
// }
}
www.aurorasolutions.io www.aurorasolutions.io
Getting Started Quickly!
in Java!
www.aurorasolutions.io www.aurorasolutions.io
Getting Started Quickly in Java!
Step 1: Create a folder; name it “helloworld”. Create an empty file called “pom.xml”. Copy the content given below...
Step 2: Run mvn package
Step 3: Run mvn dependency:tree
Step 4: Add spring-boot-starter-web dependency in pom
Step 5: Run mvn dependency:tree
Step 6: Create directory structure (src/main/java/com/helloworld) and file named “HelloWorld.java”. Copy the code give below...
Step 7: Running the HelloWorld: mvn spring-boot:run
Step 8: Open the browser: If you open a web browser to http://localhost:8080 and you will see something…Step 9: Create executable jar....
www.aurorasolutions.io www.aurorasolutions.io
Starter POM’s
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
➔ Standard Maven POMs➔ Define dependencies that we recommend➔ Parent optional➔ Available for web, batch, integration, data, amqp, aop, jdbc, ...➔ e.g. data = hibernate + spring-data + JSR 303
www.aurorasolutions.io www.aurorasolutions.io
SpringApplication
SpringApplication app = new SpringApplication(MyApplication.class);app.setShowBanner(false);app.run(args);
➔ Gets a running Spring ApplicationContext➔ Uses EmbeddedWebApplicationContext for web apps➔ Can be a single line: SpringApplication.run(MyApplication.class, args)
www.aurorasolutions.io www.aurorasolutions.io
SpringApplication
@Configuration@EnableAutoConfigurationpublic class Welcome {}
➔ Attempts to auto-configure your application➔ Backs off as you define your own beans➔ Regular @Configuration classes➔ Usually with @ConditionalOnClass and @ConditionalOnMissingBean
www.aurorasolutions.io www.aurorasolutions.io
Production Packaging
Maven plugin (using spring-boot-starter-parent):
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId></plugin>
$ mvn package or mvn clean install
$ java -jar <file-name>.jar
www.aurorasolutions.io www.aurorasolutions.io
Production Packaging
$ java -jar <file-name.jar>
➔ Easy to understand structure➔ No unpacking or start scripts required➔ Typical REST app ~10Mb
www.aurorasolutions.io www.aurorasolutions.io
Command Line Arguments
@Value("${name}")private String name;
SpringApplication adds command line arguments to the Spring Environment so you can refer inject them into beans:
$ java -jar <file-name>.jar --name=Rasheed
You can also configure many aspects of Spring Boot itself:
$ java -jar <file-name>.jar --server.port=9999
www.aurorasolutions.io www.aurorasolutions.io
Externalizing Configuration to Properties
server.port: 9000
Just put application.properties in your classpath or next to you jar, e.g. application.properties
Properties can be overridden (command line arg > file > classpath)
www.aurorasolutions.io www.aurorasolutions.io
Using YAML
server: port: 9000
Just include snake-yaml.jar and put application.yml in your classpath e.g. application.yml
Both properties and YAML add entries with period-separated paths to the Spring Environment.
www.aurorasolutions.io www.aurorasolutions.io
Binding Configuration To Beans
@ConfigurationProperties(prefix="mine")public class MyPoperties { private Resource location; private boolean skip = true; // ... getters and setters}
MyProperties.java
application.properties
mine.location: classpath:mine.xmlmine.skip: false
Explore ServerProperties.java
www.aurorasolutions.io www.aurorasolutions.io
Customizing Configuration Location
Set
➔ spring.config.name - default application, can be comma-separated list➔ spring.config.location - a Resource path, overrides name
$ java -jar <file-name>.jar --spring.config.name=production
www.aurorasolutions.io www.aurorasolutions.io
Spring Profiles
Activate external configuration with a Spring profile file name convention e.g. application-development.properties
or nested documents in YAML:
defaults: etc… --- spring: profiles: development, qa other: stuff: more stuff...
application.yml
Set the default spring profile in external configuration, e.g: application.properties
spring.profiles.active: default, qa
www.aurorasolutions.io www.aurorasolutions.io
Logging➔ Spring Boot provides default configuration files for 3 common logging frameworks:
logback, log4j and java.util.logging➔ Starters (and Samples) use logback with colour output➔ External configuration and classpath influence runtime behavior➔ LoggingApplicationContextInitializer sets it all up
www.aurorasolutions.io www.aurorasolutions.io
Autoconfigured Behavior<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId></dependency><dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId></dependency>
Let’s extend the demo and see what we can get by just modifying the classpath, e.g.
● Add an in memory database
www.aurorasolutions.io www.aurorasolutions.io
Available Autoconfigured Behaviour...
● Embedded servlet container (Tomcat or Jetty)● JDBC: DataSource and JdbcTemplate● JPA, JMS, AMQP (Rabbit), AOP● Websocket● Spring Data JPA (scan for repositories) and Mongodb● Thymeleaf● Mobile● Batch processing● Reactor for events and async processing● Actuator features (Security, Audit, Metrics, Trace)
www.aurorasolutions.io www.aurorasolutions.io
The Actuator
Monitoring Endpoints
www.aurorasolutions.io www.aurorasolutions.io
The Actuator
Adds common non-functional features to your application and exposes MVC endpoints to interact with them.
Provides following endpoints: /metrics, /health, /trace, /dump, /shutdown, /beans, /env, /info
www.aurorasolutions.io www.aurorasolutions.io
Spring Boot Modules
www.aurorasolutions.io www.aurorasolutions.io
● Spring Boot - main library supporting the other parts of Spring Boot● Spring Boot Autoconfigure - single @EnableAutoConfiguration annotation creates a
whole Spring context● Spring Boot Starters - a set of convenient dependency descriptors that you can include in
your application.● Spring Boot CLI - compiles and runs Groovy source as a Spring application● Spring Boot Actuator - common non-functional features that make an app instantly
deployable and supportable in production● Spring Boot Tools - for building and executing self-contained JAR and WAR archives● Spring Boot Samples - a wide range of sample apps
www.aurorasolutions.io www.aurorasolutions.io
Build WAR file!