Introduction to Spring Boot
- providing ability to create simple web apps very quickly
- minimizing amount of XML codebloat which is usually necessary to configure every Spring application
- most of app configuration is automatical
- simplify running and deployment process by using embedded Tomcat or Jetty servers that can run our applications without special effort and deploy process
- there are lot of so called spring boot starters which are packages containing default configuration for various fields of Spring like database access by JPA, aspect oriented programming or security
First steps
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.1.RELEASE")
}
}
allprojects {
apply plugin: "java"
version = '1.0-SNAPSHOT'
group = "info.rnowak.springBootFun"
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-test:1.0.1.RELEASE"
compile "com.google.guava:guava:16.0.1"
compile "com.h2database:h2:1.3.175"
testCompile "junit:junit:4.11"
testCompile "org.mockito:mockito-all:1.9.5"
testCompile "org.assertj:assertj-core:1.5.0"
}
}
Now when we have common configuration, we can declare basic modules of application:
project(":persistence") {
dependencies {
compile "org.springframework.boot:spring-boot-starter-data-jpa:1.0.1.RELEASE"
testCompile project(":webapp")
}
}
project(":webapp") {
apply plugin: "spring-boot"
dependencies {
compile project(":persistence")
compile "org.springframework.boot:spring-boot-starter-web:1.0.1.RELEASE"
}
}
Let’s start fun with Spring!
package info.rnowak.springFun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class SpringFun {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(SpringFun.class);
app.setShowBanner(false);
app.run(args);
}
}
Add some AngularJS
<!DOCTYPE html>
<html ng-app="springFun">
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.4/angular-route.min.js"></script>
<script src="js/application.js"></script>
<script src="js/controllers.js"></script>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#/index">Spring Boot Fun</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="#/list">Messages list</a></li>
<li><a href="#/about">About</a></li>
</ul>
</div>
</div>
</nav>
<div ng-view></div>
<footer class="text-center">
Spring Boot Fun
</footer>
</body>
</html>