10 Things to Know in Spring Boot

10 Things to Know in Spring Boot

10 Things to Know in Spring Boot

1. Spring boot is not a brand new framework like mvc/web/rest/jpa add-ons, it’s just a additional kit from spring to reduce the development headaches with spring framework.

2. Spring boot provides single dependency entry for pom.xml configuration,

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Here this downloads all the dependencies required for web spring applications.

 

3. Version 1.5.10 represents the version of the spring boot(not any spring core/context jars) and equivalent version mappings for spring-context, spring-core can be found here,
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot/1.5.10.RELEASE

4. SpringApplication.run method should be called out in your main method to run as embedded spring boot project,

public static void main(String[] args) throws Exception {
SpringApplication.run(YOUR_CLASS_NAME.class, args);
}

and it launches an embedded web server for you.

Ensure the main class annotated with @Controller & @EnableAutoConfiguration atleast.

 

 

Related spring boot article:

Restful Web services with Spring boot Simple Example

 

 

5. Spring boot does not require web.xml and everything can be configured through annotations only.

 

6. @Configuration annotation should be used for classes where you want to add the filters and listeners and @Bean annotation can be used to add the filter mapping in spring boot Main class. (earlier the same has been done in web.xml)

 

 

Related spring boot article:

Scheduler in Spring Boot with 3 Simple Steps Example

 

 

7. Spring Boot will detect that you have a Spring MVC controller If you have @ComponentScan in main java class and @RestController anywhere and start up an embedded Apache Tomcat 7 instance, by default.

You should be able to test the REST endpoint by opening up your browser and hitting http://localhost:8080/hello/jd.in

 


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

@RestController
class GreetingController {

@RequestMapping("/hello/{name}")
String hello(@PathVariable String name) {
return "Hello, " + name + "!";
}
}

 

8. To change the default port from 8080 to some other port number,

  • Implement EmbeddedServletContainerCustomizer and override customize method and set the port.
  • Add this entry in application.properties file server.port=12345
  • Generate the jar through maven and run like this, java -jar -Dserver.port=12345 spring-boot-example-1.0.jar

 

9. application.properties is the one and only file/property file which helps to configure hibernate/ehcache/application specific config’s like port number etc…

 

 

Related spring boot article:

Spring Boot + EhCache Hibernate Working Example

 

10. pom.xml is the only xml file used in spring boot project. No more web.xml or deployment descriptors with spring boot. All the annotated classes and methods are internally loaded and managed by spring without web.xml file.

 

 

Do you know some interesting facts and truths about spring boot ?

Feel free to comment in the below comments section.

Learn from some1, teach to some1

 

References:

Spring.io

Stackoverflow.com

398 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *