Top 5 Spring Boot Features Java Developers Should Know
Check out these top five Spring Boot features that every Java dev should know about, including auto-configuration, starter dependencies, Spring Boot CLI, and more.
Join the DZone community and get the full member experience.
Join For FreeYou might have heard about Spring Boot and it's magical powers that can create a Spring Web application in just under 140 characters, but what does that really mean? What are these features that provide Spring Boot with so much power and can make Spring application development so easy? Well, that's what you will learn in this article. We are going to talk about Spring Boot's auto-configuration, starter dependencies, Spring Boot CLI, and Actuator and Spring Initializer features in greater detail. These are the features that take away most of the pain and friction associated with writing Spring-based Java web applications.
But before going into the details, let's revisit problems associated with Spring-based Java development. I personally like to see the problem first and get a feel for it before I can enjoy the solution. Remember: comfort only feels better after hard work. It is the same for meals; you enjoy more when you are hungry.
Spring is — no doubt — a great framework and it does a lot of things for you, e.g. it creates an object, provides them with their dependency, and takes away a lot of code you would have written if Spring didn't exist, but in return, it also asks a lot from you in terms of configuration and learning.
If you have ever worked in a greenfield project where you had to start a fresh, Spring-based Java application from scratch, you know that it's not a piece of cake. You first need to find all the dependencies you need and then their compatible version. You also need to configure a lot of beans to enable some Spring magic.
For example, if you want to create a Spring MVC-based REST application, which supports the JSON format in embedded Tomcat, then you can create at least 8 to 10 dependencies in your Maven pom.xml file, e.g. spring-core.jar, spring-mvc.jar, jackson.jar, embedded-tomcat.jar, etc., and mind it, this is a very simple setup.
Spring Boot takes away all these pains and lets you write the code that matters, i.e. application code. All of the Spring Boot features that I mentioned previously, e.g. auto-configuration, Starter POMs or Starter dependencies, and Spring Boot CLI, aims to simplify Java development with Spring.
Now, let's go into a little bit more detail on each of these features.
1. AutoConfiguration
You might have worked with Spring-based Java web applications before, which connects to a relational database, e.g. an in-memory database like H2, and if yes, then you might know that you need to declare the JdbcTemplate as a bean and also need to configure a DataSource
, which is a dependency for the JDBC template.
In a modern Spring application that uses Java-based configuration, you need to add the following two methods into your Configuration class:
@Bean
public JdbcTemplate jdbcTempalte(DateSource ds){
return new JdbcTempalte(ds);
}
@Bean
public DataSource dataSource(){
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScripts('ddl.sql', 'data.sql')
.build();
}
This is not really complex for someone who has conducted development in Spring before, but if you are starting fresh, then it can take hours and days to figure this out.
But, more importantly, this is a piece of code that many of us have written irrespective of our application. I mean, this code is not unique and every single Spring application that works with JDBC will need it.
That's where the Spring Boot AutoConfiguration
method comes into the picture. It detects the presence of certain a Class in the Classpath and then automatically configures it for you.
For example, if you have added the JDBC Template into your classpath and the H2.jar, then Spring Boot can automatically configure an in-memory database for you and a JDBC Template, which is ready to use. You don't need to write the above code to use the JDBC Template in your DAO layer.
This is just an example. Spring Boot auto-configuration makes more than 200+ such decisions and automatically configures many functionalities by examining JAR dependencies. For example, if spring-mvc.jar is present, then it can automatically configure DispatcherServlet, InternalViewResolver, etc.
If JPA and Hibernate are present, then it can configure that as well, and if you have spring-security.jar, then it can even configure basic security to protect your application.
Keep in mind: when it comes to relying on auto-configuration, as in-depth knowledge is required to properly protect your application, if you are interested in Spring Security, check out Spring Security MasterClass by Eugen Paraschiv.
The Auto-Configuration feature is, by default, disabled, and you need to enable it by using @EnableAutoConfiguration
or the @SpringBootApplication
annotation on your Configuration class. I normally annotated the Main class, which I am going to run with an embedded Tomcat server.
It's recommended using the @SpringBootApplication
annotation from Spring Boot 1.2 onwards as it combines a couple of other annotations to make your code more readable. See Learn Spring Boot - Rapid Spring Application Development to learn more about Spring Boot annotations.
In short, the auto-configuration feature of Spring Boot saves a lot of work and reduce the development time and I strongly recommend to use auto-configuration whenever you use Spring Boot.
2. Starter POMs
While AutoConfiguration
takes away the pain of configuring common functionalities, the Starter POMs take away pain by finding and adding common dependencies in your project.
In order to build a simple Spring MVC-based REST application that supports Jackson and to run it an embedded container, you would at least need the following dependencies:
spring-core.jar
spring-web.jar
spring-webmvc.jar
jackson-databind.jar
tomcat-embed-core.jar
tomcat-embed-el.jar
tomcat-embed-logging-juil.jar
By using Spring Boot Starter POMs or starter dependency feature, you can get all of these by just adding spring-boot-starter-web dependency in your pom.xml.
So instead of adding all these dependencies and worrying about their compatible version, you just need to add one. You will also be more confident that tried and tested versions of libraries are used, and there won't be any incompatibility issue in future.
Another subtle benefit of the starter POMs feature is that you don't need to remember or search dependencies. If you are building a web application, you can add a 'web' starter. If you are building a JPA application, you could add the 'JPA' starter by aggregating common dependencies and functionalities that Spring Boot has made easy to remember and use.
In short, Starter POMs or starter dependencies are another awesome feature of Spring Boot that really helps to simplify Spring application development. It's like a close cousin of auto-configuration and you will frequently use them together.
3. Spring Boot CLI
In the first paragraph of this article, I said that it's now possible to create a Java web application that can fit in a tweet. This happens because of Groovy and Spring Boot CLI.
The Spring Boot CLI is a command line interface provided by the Spring Boot framework, which allows you to create Spring-based web applications using the Groovy programming language. Actually, Groovy and Spring Boot nicely complement each other. Groovy aims to make Java development simpler, while Spring Boot aims to make Spring application development simpler and both benefit from each other's simplicity.
While auto-configuration and starter dependencies are an integral feature of Spring Boot, Spring CLI is an optional one. You also need to install Spring CLI in order to use it.
Here is a simple HelloWorld RESTful Web Service in Groovy and Spring Boot CLI, and it works so that you can run it without compiling, as shown below:
@RestController
class HelloSpringBootController{
@RequestMapping("/")
def hello() {
return "Hello Spring Boot CLI"
}
}
That's it! You can run it on an embedded container, which comes with Spring Boot CLI, no web.xml, no configuration, and no server setup.
If you are wondering how these whole things work, for example, how Groovy knows about @RestController and @RequestMapping
annotations, then let me tell you that Spring Boot CLI leverages auto-configuration and starter POMs features to let you focus on only writing application code.
Spring Boot CLI detect that @RestController
and @RequestMapping
are in use, and it knows which starter dependencies are required to add into classpath to make it work.
Once it downloads those series of dependencies, auto-configuration automatically kicks-in and configures it for use, e.g. once spring-boot-web-starter comes into the picture, it downloads spring-mvc.jar and then auto-configuration automatically configures the DispatcherServlet and enables Spring MVC.
This whole thing looks like magic, but it's actually very easy to do. If you like this model of development, I suggest you go through Spring Boot in Action by Craig Walls to learn Spring CLI in depth. Craig has covered this topic really nicely.
4. Actuator
The actuator
is another awesome feature of Spring Boot that allows seeing what's going on inside a running Spring Boot application. With all its goodness of auto-configuration, there comes a risk of not knowing what is inside your application and that risk is addressed by the Spring Actuator.
It provides a lot of insight and metrics about running applications in production. For example, by using Actuator
, you can find out exactly which beans are configured in the Application context, what are auto-configuration decisions made, what environment variables, system properties, command line arguments are available to an application, and many more.
You can even get a trace of HTTP requests handled by the application, along with various useful application metrics, e.g. CPU and Memory usage, garbage collection details, web requests, and data source usage.
Spring Boot Actuator
also provides several endpoints to retrieve this data, e.g. you can get all this using RESTful APIs, or you can use its remote shell feature to securely go inside the application and get all this information by issuing commands.
It also exposes all this functionality using JMX MBeans, which means you can control them at runtime using a JMX client, like JConsole.
At the same time, you also need to secure access to Actuator
endpoints because it not only exposes confidential information but also it's dangerous. For example, anyone can stop your application by using /shutdown
endpoints.
Though, you don't need to worry. Like any other Spring application, you can use Spring Security to protect Actuator
endpoints.
5. Spring Boot Initializer
Spring Initializer is another feature of Spring Boot that solves the problem with respect to project structure. It's a web application that allows you to generate a Maven or Gradle projects with Java, Kotlin, Groovy, or Spring Boot.
All you need to provide is the Project MetaData in GUI, e.g. name of your project, Group, Artifact, etc. It also allows you to choose a starter dependency from a big list, e.g. web, JPA, or security starter.
The Spring Initializer project can be accessed at https://start.spring.io/. Once you create a project, you can download the Zip file and then open into an IDE like Eclipse or IntelliJ IDEA. You can then edit this sample project to use with your code.
As per my experience, one of the common problems many Java and Spring developers face is how to start a project. Many of them are clueless about whether to put use your Java file, resource file, etc.
Though Maven, Gradle, IntelliJ IDEA, and Eclipse help to give you a basic structure, you still need to be proficient on those two skills to get a head start, and if you are not familiar with Maven or your IDE, it can be a nightmare.
Spring Boot Initaizer solves this problem and makes it easy to create a Spring-based Java application without really knowing about a lot of internal details of Spring framework.
That's all for now about some of the features of Spring Boot that Java developers should know. These features really make working with Java and Spring fun and productive, and that's why more and more companies are adopting Spring Boot for Java development. Java developers with Spring Boot experience is also in good demand and if you are looking for your next job as Java web developer then Spring Boot skills can really make a difference. If you are interested in learning more about Spring Boot, the following resources can help you get started:
- Spring Boot in Action
- Learn Spring Boot - Rapid Spring Application Development
- Master Java Web Services and REST API with Spring Boot
- Spring Boot: Efficient Development, Configuration, and Deployment
- Master Hibernate and JPA with Spring Boot in 100 Steps
Thanks for reading! If you like these Spring Boot features, then please share this post with your friends and colleagues. If you have any questions or feedback, then please let us know in the comments below.
Opinions expressed by DZone contributors are their own.
Comments