Spring Booted
22 October 2015
Spring Boot is an exciting new way to develop Spring applications with minimal friction from the framework itself. Auto-configuration eliminates much of the boilerplate configuration that infests traditional Spring applications. Spring Boot starters enable you to specify build dependencies by what they offer rather than explicit library names and version. The Spring Boot CLI takes Spring Boot’s frictionless development model to a whole new level by enabling quick and easy development with Groovy from the command line. Finally, the Actuator lets you look inside your running application to see what and how Spring Boot has done.
Above is an excerpt from Craig Walls' Spring Boot in Action which perfectly describe my feelings with the traditional Spring configuration and how Spring Boot modernise it.
My favourite part of Spring Boot is its CLI so I'll try creating a new project using CLI.
Installing Spring Boot CLI
$ sdk install springboot
Downloading: springboot 1.3.5.RELEASE
Hang tight, this could take a while...
Installing: springboot 1.3.5.RELEASE
Done installing!
Do you want springboot 1.3.5.RELEASE to be set as default? (Y/n):
Setting springboot 1.3.5.RELEASE as default.
Installing gradle
This is optional. You will need gradle
if you don't want to use the default project type maven
.
$ sdk install gradle
Downloading: gradle 2.13
Hang tight, this could take a while...
Installing: gradle 2.13
Done installing!
Do you want gradle 2.13 to be set as default? (Y/n):
Setting gradle 2.13 as default.
Spring shell
$ spring shell
Spring Boot (v1.3.5.RELEASE)
Hit TAB to complete. Type 'help' and hit RETURN for help, and 'exit' to quit.
$ help
usage:
<command> [<args>]
Available commands are:
help command
Get help on commands
version
Show the version
run [options] <files> [--] [args]
Run a spring groovy script
test [options] <files> [--] [args]
Run a spring groovy script test
grab
Download a spring groovy script's dependencies to ./repository
jar [options] <jar-name> <files>
Create a self-contained executable jar file from a Spring Groovy script
war [options] <war-name> <files>
Create a self-contained executable war file from a Spring Groovy script
install [options] <coordinates>
Install dependencies to the lib directory
uninstall [options] <coordinates>
Uninstall dependencies from the lib directory
init [options] [location]
Initialize a new project using Spring Initializr (start.spring.io)
prompt
Change the prompt used with the current 'shell' command. Execute with no arguments to return to the previous value.
clear
Clear the screen
exit
Quit the embedded shell
Common options:
-d, --debug Verbose mode
Print additional status information for the command you are running
See 'help <command>' for more information on a specific command.
spring init at a glance
$ spring init -l
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Service capabilities :: https://start.spring.io
Supported dependencies
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------+
| Id | Description | Required version |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------+
| activiti-basic | Activiti BPMN workflow engine | |
| actuator | Production ready features to help you monitor and manage your application | |
| actuator-docs | API documentation for the Actuator endpoints | >= 1.3.0.M4 |
| amqp | Advanced Message Queuing Protocol via spring-rabbit | |
| aop | Aspect-oriented programming including spring-aop and AspectJ | |
| artemis | Java Message Service API via Apache Artemis | >= 1.3.0.M2 |
| batch | Spring Batch including HSQLDB database | |
| cache | Spring's Cache abstraction | >= 1.3.0.M1 |
| cloud-aws | AWS native services from spring-cloud-aws | >= 1.2.3.RELEASE |
| cloud-aws-jdbc | Relational databases on AWS with RDS and spring-cloud-aws-jdbc | >= 1.2.3.RELEASE |
| cloud-aws-messaging | Messaging on AWS with SQS and spring-cloud-aws-messaging | >= 1.2.3.RELEASE |
| cloud-bus-amqp | A simple control bus with AMQP and spring-cloud-bus-amqp | [0.0.0.RELEASE,1.3.0.M5] |
| cloud-bus-kafka | A simple control bus with Kafka and spring-cloud-bus | (1.3.0.M5,9999.9.9.RELEASE] |
| cloud-bus-rabbit | A simple control bus with Rabbit and spring-cloud-bus | (1.3.0.M5,9999.9.9.RELEASE] |
| cloud-bus-redis | A simple control bus with Redis and spring-cloud-bus | (1.3.0.M5,9999.9.9.RELEASE] |
| cloud-cloudfoundry-discovery | Service discovery with Cloud Foundry | >= 1.3.0.M4 |
| cloud-cluster-hazelcast | Leadership election and global state with Hazelcast and spring-cloud-cluster-hazelcast | >= 1.3.0.M4 |
| cloud-cluster-redis | Leadership election and global state with Redis and spring-cloud-cluster-redis | >= 1.3.0.M4 |
| cloud-cluster-zookeeper | Leadership election and global state with Zookeeper and spring-cloud-cluster-zookeeper | >= 1.3.0.M4 |
| cloud-config-client | spring-cloud-config Client | >= 1.2.3.RELEASE |
| cloud-config-server | Central management for configuration via a git or svn backend | >= 1.2.3.RELEASE |
| cloud-connectors | Simplifies connecting to services in cloud platforms, including spring-cloud-connector and spring-cloud-cloudfoundry-connector | >= 1.2.0.RELEASE |
| cloud-eureka | Service discovery using spring-cloud-netflix and Eureka | >= 1.2.3.RELEASE |
| cloud-eureka-server | spring-cloud-netflix Eureka Server | >= 1.2.3.RELEASE |
| cloud-feign | Declarative REST clients with spring-cloud-netflix Feign | >= 1.2.3.RELEASE |
| cloud-hystrix | Circuit breaker with spring-cloud-netflix Hystrix | >= 1.2.3.RELEASE |
| cloud-hystrix-dashboard | Circuit breaker dashboard with spring-cloud-netflix Hystrix | >= 1.2.3.RELEASE |
| cloud-oauth2 | OAuth2 and distributed application patterns with spring-cloud-security | >= 1.2.3.RELEASE |
| cloud-ribbon | Client side load balancing with spring-cloud-netflix and Ribbon | >= 1.2.3.RELEASE |
| cloud-security | Secure load balancing and routing with spring-cloud-security | >= 1.2.3.RELEASE |
| cloud-starter | spring-cloud-context (e.g. Bootstrap context and @RefreshScope) | >= 1.2.3.RELEASE |
| cloud-starter-consul-bus | Inter instance control events with Hashicorp Consul | >= 1.3.0.M4 |
| cloud-starter-consul-config | Configuration management with Hashicorp Consul | >= 1.3.0.M4 |
| cloud-starter-consul-discovery | Service discovery with Hashicorp Consul | >= 1.3.0.M4 |
| cloud-starter-sleuth | Distributed tracing via logs with spring-cloud-sleuth | >= 1.3.0.M4 |
| cloud-starter-zipkin | Distributed tracing via Zipkin and spring-cloud-sleuth-zipkin | >= 1.3.0.M4 |
| cloud-starter-zookeeper-config | Configuration management with Zookeeper and spring-cloud-zookeeper-config | >= 1.3.0.M4 |
| cloud-starter-zookeeper-discovery | Service discovery with Zookeeper and spring-cloud-zookeeper-discovery | >= 1.3.0.M4 |
| cloud-stream-binder-kafka | Messaging microservices with Kafka | >= 1.3.0.M4 |
| cloud-stream-binder-rabbit | Messaging microservices with RabbitMQ | >= 1.3.0.M4 |
| cloud-stream-binder-redis | Messaging microservices with Redis | >= 1.3.0.M4 |
| cloud-turbine | Circuit breaker metric aggregation using spring-cloud-netflix with Turbine and server-sent events | >= 1.2.3.RELEASE |
| cloud-turbine-amqp | Circuit breaker metric aggregation using spring-cloud-netflix with Turbine and AMQP | [0.0.0.RELEASE,1.3.0.M5] |
| cloud-turbine-stream | Circuit breaker metric aggregation using spring-cloud-netflix with Turbine and Spring Cloud Stream (choose a specific Stream binder implementation to complement this) | (1.3.0.M5,999.9.9.RELEASE] |
| cloud-zuul | Intelligent and programmable routing with spring-cloud-netflix Zuul | >= 1.2.3.RELEASE |
| data-cassandra | Cassandra NoSQL Database, including spring-data-cassandra | >= 1.3.0.RC1 |
| data-elasticsearch | Elasticsearch search and analytics engine including spring-data-elasticsearch | |
| data-gemfire | GemFire distributed data store including spring-data-gemfire | |
| data-jpa | Java Persistence API including spring-data-jpa, spring-orm and Hibernate | |
| data-jpa | Java Persistence API including spring-data-jpa, spring-orm and Hibernate | |
| data-mongodb | MongoDB NoSQL Database, including spring-data-mongodb | |
| data-rest | Exposing Spring Data repositories over REST via spring-data-rest-webmvc | |
| data-rest-hal | Browsing Spring Data REST repositories with an HTML UI | >= 1.3.0.M1 |
| data-solr | Apache Solr search platform, including spring-data-solr | |
| derby | Apache Derby database (with embedded support) | >= 1.2.2.RELEASE |
| devtools | Spring Boot Development Tools | >= 1.3.0.M1 |
| freemarker | FreeMarker templating engine | |
| groovy-templates | Groovy templating engine | |
| h2 | H2 database (with embedded support) | |
| hateoas | HATEOAS-based RESTful services | >= 1.2.2.RELEASE |
| hornetq | Java Message Service API via HornetQ | |
| hsql | HSQLDB database (with embedded support) | |
| integration | Common spring-integration modules | |
| jdbc | JDBC databases | |
| jersey | the Jersey RESTful Web Services framework | >= 1.2.0.RELEASE |
| jooq | Persistence support using Java Object Oriented Querying | >= 1.3.0.M2 |
| jta-atomikos | JTA distributed transactions via Atomikos | >= 1.2.0.M1 |
| jta-bitronix | JTA distributed transactions via Bitronix | >= 1.2.0.M1 |
| lombok | Lombok | >= 1.3.0.M4 |
| mail | javax.mail | >= 1.2.0.RC1 |
| mobile | Simplify the development of mobile web applications with spring-mobile | |
| mustache | Mustache templating engine | >= 1.2.2.RELEASE |
| mysql | MySQL jdbc driver | |
| postgresql | PostgreSQL jdbc driver | |
| ratpack | Spring Boot integration for the Ratpack framework | >= 1.2.0.RELEASE |
| redis | REDIS key-value data store, including spring-redis | |
| remote-shell | CRaSH shell integration | |
| restdocs | Document RESTful services by combining hand-written and auto-generated documentation | |
| security | Secure your application via spring-security | |
| social-facebook | spring-social-facebook | |
| social-linkedin | spring-social-linkedin | |
| social-twitter | spring-social-twitter | |
| thymeleaf | Thymeleaf templating engine, including integration with Spring | |
| vaadin | Vaadin | |
| validation | JSR-303 validation infrastructure (already included with web) | >= 1.3.0.M1 |
| velocity | Velocity templating engine | |
| web | Full-stack web development with Tomcat and Spring MVC | |
| websocket | Websocket development with SockJS and STOMP | |
| ws | Contract-first SOAP service development with Spring Web Services | |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------+
Project types (* denotes the default)
+-----------------+-----------------------------------------+-----------------------------+
| Id | Description | Tags |
+-----------------+-----------------------------------------+-----------------------------+
| gradle-build | Generate a Gradle build file | build:gradle,format:build |
| gradle-project | Generate a Gradle based project archive | build:gradle,format:project |
| maven-build | Generate a Maven pom.xml | build:maven,format:build |
| maven-project * | Generate a Maven based project archive | build:maven,format:project |
+-----------------+-----------------------------------------+-----------------------------+
Parameters
+-------------+------------------------------------------+------------------------------+
| Id | Description | Default value |
+-------------+------------------------------------------+------------------------------+
| type | project type | maven-project |
| bootVersion | spring boot version | 1.2.7.RELEASE |
| packaging | project packaging | jar |
| javaVersion | language level | 1.8 |
| language | programming language | java |
| groupId | project coordinates | com.example |
| artifactId | project coordinates (infer archive name) | demo |
| version | project version | 0.0.1-SNAPSHOT |
| name | project name (infer application name) | demo |
| description | project description | Demo project for Spring Boot |
| packageName | root package | com.example |
+-------------+------------------------------------------+------------------------------+
Creating my first project
$ spring init --dependencies web,h2,data-jpa,jdbc,groovy-templates,security --build gradle --packaging jar --extract
Using service at https://start.spring.io
Project extracted to '/home/drmanalo/workspace/paluwagan'
$ tree
.
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── DemoApplication.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── com
└── example
└── DemoApplicationTests.java
Running the application
$ gradle bootRun
Started DemoApplication in 4.605 seconds (JVM running for 4.954)
> Building 80% > :bootRun