An overview of Spring Boot
I have come up with this blog that outlines:
- What is Spring Boot?
- Why do we use it?
- How to get started?
1. What is Spring Boot?
Spring boot from Spring is just another project enabling developers to create stand-alone, production-grade Spring based applications.
2. Why do we use it?
- Create stand-alone Spring applications
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files). You can create a war file and deploy if need be.
- Provide opinionated ‘starter’ POMs to simplify your Maven configuration
- Absolutely no code generation and no requirement for XML configuration (Automatically configure spring wherever possible)
3. How to get started?
There are multiple ways you can get started with spring boot application eg – Spring Boot CLI, Maven or gradle build tool.
I am going to use Eclipse + Maven. However, if you prefer using gradle or boot client there are many examples that you can find – Here is one with CLI .
Let’s get started –
- Create a maven project with following archetype
<groupId>am.ik.archetype</groupId> <artifactId>spring-boot-blank-archetype</artifactId> <version>1.0.6</version
2. Now you will get default pom.xml file with many dependencies ( these dependencies are not required as it’s just an example, so I am removing all dependencies except the one outlined below) –
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId
Here is how your maven may look like –
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ttn.aem</groupId> <artifactId>springboot</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>Spring Boot Blank Project (from ; <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- <start-class>com.ttn.aem.springboot.main.App</start-class> --> <java.version>1.8</java.version> <lombok.version>1.14.8</lombok.version> <log4jdbc.log4j2.version>1.16</log4jdbc.log4j2.version> <rest.assured.version>2.3.3</rest.assured.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>spring-boot-${project.version}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>${spring-loaded.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
3. You will get 3 generated classes. Out of these, we need only App.java class which we will use as a starter class for our application. Here is how your App.java will look –
package com.ttn.aem.springboot.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages="com.ttn.aem.springboot.controllers") public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
@SpringBootApplication works as entry point for the application and bootstrap spring. When applicaiton starts spring boot search for the configuration annotation.
We can also use @EnableAutoConfiguration and @ComponentScan instead of @SpringBootApplication, depending on the preferences.
We have also given scanBasePackages along with @SpringBootApplication so that it is easy to look for the configuration or the classes for which beans needs to be created.
4. Here is our controller code –
package com.ttn.aem.springboot.controllers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") public String hello() { return "Hello World!"; } @RequestMapping("calc") public int calc(@RequestParam int left, @RequestParam int right) { return left+right; } }
RestController is pretty straight forward isn’t it?
And we already have instructed spring to scan com.ttn.aem.springboot.controllers package
What’s next?
That’s all – Our application is ready to build and run.
run mvn clean instal
Then
java -jar target/<jarname>.jar
Now go to the below link:
http://localhost:8000/calc?left=10&right=5 and http:localhost:8080
You see it works. Was it ever so easy to create a spring rest application?
- No Web.xml
- No dispatcher-servlet.xml
- No applicationContext.xml
- No Annotation configuration classes
- and the list goes on.
Please share your thoughts in the comments below.