Spring Boot for first time Java developer

Spring Boot is highly opinionated rapid development framework to create stand-alone, production-grade Spring based Applications.

It’s super easy to create projects using Spring Boot and as promised it addresses lot of Non Functional Requirements or Cross cutting concerns which is pretty much needed in every project.

Some of the most common questions I had as Java developer while moving to Spring Boot were as follows.

Why should I use Spring boot?

Primary Goals of the Spring Boot framework as per Spring website are:

  • 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.

I heard Spring Boot creates a Jar file for a web application?

If you build a Spring Boot initializer project, it generates a Jar file for a web application. As a java developer you might have been habitual of creating War file for Web Applications. How does this Jar work then? How it will be deployed to Web Servers?

This jar known as fat jar includes all the other dependencies and things like your web server(tomcat or jetty) inside the archive. You can give anybody this .jar and they can run your entire Spring application with no issues: no build tool required, no setup, no web server configuration, etc:

Just run using below command

java -jar target/myproject-0.0.1-SNAPSHOT.jar

What is fat Jar?

Executable jars also known as Fat Jars contain project compiled classes along with all of the jar dependencies that your code needs to run.

As Java does not provide any standard way to load nested jar files. Many developers use “uber” jars. An uber jar simply packages all classes, from all jars, into a single archive.

How web app present in Jar file run behind the scenes?

Via embedded web server. Out of the box, Spring Boot uses a public static void main entry-point that launches an embedded web server for you.

When you run your application, Spring Boot will detect that you have a Spring MVC controller and start up an embedded Apache Tomcat 7 instance, by default.

okay, Embedded Server, but how can I change web server configuration?

Answer is programmatically. There are lots of configuration options for the embedded Tomcat or jetty. You can enable HTTPS (SSL/TLS termination) for your webservice fairly easily by providing an EmbeddedServletContainerCustomizer.

By specifying properties either through the command line (as –D-style arguments) or through a loaded property file, you can tweak smaller things like port numbers.

Most of the developer’s are habitual to War convention. Can I generate war files and deploy it to Java EE application servers?

Easy! It’s still just Spring. You’ll need to make three changes:

  • Move from a jar build to a war build in Maven
  • Comment out the declaration of the spring-boot-maven-plugin plugin in your pom.xml file, and change the Maven or gradle packaging type to war.
  • Add a web entry point into your application.

See link for detailed info.

How to deploy to the cloud?

You can deploy is as Jar file or war file! Flexibility is always yours.

Can I use Maven or Gradle?

Yes!

How dependency management is handled in Spring?

Each release of Spring Boot provides a list of dependencies it supports. In practice, you do not need to provide a version for any of these dependencies in your build configuration as Spring Boot is managing that for you.

When you upgrade Spring Boot itself, these dependencies will be upgraded as well in a consistent way. So less things to worry about!

You can still specify a version and override Spring Boot’s recommendations if you feel that’s necessary.

How to run application from IDE?

You can run a Spring Boot application from your IDE as a simple Java application. If you can’t directly import your project into your IDE, you may be able to generate IDE metadata using a gradle or Maven build plugin.

How to run application from commandline? is remote debugging possible?

If you use the Spring Boot Maven or Gradle plugins to create an executable jar you can run your application using java -jar.

java -jar target/myproject-0.0.1-SNAPSHOT.jar

You can also attach a debugger to your application via

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myproject-0.0.1-SNAPSHOT.jar

will hot swapping work out of the box? do i need to start application again if I do some changes?

Spring Boot applications are just plain Java applications, JVM hot-swapping should work out of the box.

Applications that use spring-boot-devtools module will automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder will be monitored for changes. See http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-restart for more details.

What all kind of starter projects are offered by Spring Boot

A lot. See link for detailed info.

Where should we place Application.java

Spring Boot favors Java-based configuration. Spring generally recommend that you place your main application class in a root package above other classes. The @EnableAutoConfiguration annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @EnableAutoConfiguration annotated class will be used to search for @Entity items.

Where I can refer for additional Spring Boot features?

See link for detailed info.

Quick steps to create the project

To create a default demo.zip: $ curl https://start.spring.io/starter.zip -o demo.zip

To create a web project using Java 7:

$ curl https://start.spring.io/starter.zip -d dependencies=web \
                        -d javaVersion=1.7 -o demo.zip

To create a web/data-jpa gradle project unpacked:

$ curl https://start.spring.io/starter.tgz -d dependencies=web,data-jpa -d type=gradle-project -d baseDir=my-dir | tar -xzvf -

To generate a Maven POM with war packaging:

$ curl https://start.spring.io/pom.xml -d packaging=war -o pom.xml

## References —————- http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-documentation-about

https://spring.io/blog/2014/03/07/deploying-spring-boot-applications

Version History


Date Description
2015-07-01    Initial Version
2015-08-01    Added “Create Branch commands”