Features & Tips That Every Developer Must Know About Spring Boot

Features & Tips That Every Developer Must Know About Spring Boot

If you are not living under the rock, then you must have heard about Spring Boot, the framework which provides a simpler and faster way to set up, configure, and run both simple and web-based applications.?Spring Boot is a?framework created to simplify the bootstrapping and development of a new Spring application?by the Pivotal team.

History

Well, Pivotal was heavily criticized for its heavy reliance on XML based configurations. In 2013,?the CTO of Pivotal made the company mission to make an XML-free development platform?that would not only simplify the development of the applications but also would simplify dependency management, which was a nightmare back then.


In the third quarter of 2013, Spring Boot gained huge popularity by demonstrating its simplicity with a runnable web application that fit in under 140-characters, delivered in a tweet. This tweet came after its first beta release and was enough to make the developers talk about it.

No alt text provided for this image

Spring Boot, in a single line, means:

( Spring Framework — XML Configuration ) + Integrated Server

Some Components of Spring Boot

Spring Boot Core?is a base for other Spring models and provides functionalities that work on their own with validation.?Spring Boot CLI?is a command-line interface based on ruby and rails for its function. However, to start and stop the application, spring boot is required.

Spring Boot Actuator?enables enterprise features that can be used in your application which can auto-detect frameworks and features of your application and use it accordingly as and when required. Integrating actuators with spring boot application can be done by including the?spring-boot-starter-actuator?starter in the?pom.xml?file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>        

Spring Boot Starters?help to initiate the project and are included as a dependency in your built file. It automatically adds starter projects and dependencies for the kind of application you are developing. There are more than 50 starters at our disposal. The most commonly used are:

  • spring-boot-starter:?core starter, including auto-configuration support, logging, and YAML
  • spring-boot-starter-data-jpa:?starter for using Spring Data JPA with Hibernate
  • spring-boot-starter-security:?starter for using Spring Security
  • spring-boot-starter-test:?starter for testing Spring Boot applications
  • spring-boot-starter-web:?starter for building web, including RESTful, applications using Spring MVC

Features of Spring Boot

There are tonnes of features that are proven to make the lives of the developers easier. However, the below features are at the top of the list.

Dependency Management

Prior to the release of the spring-boot framework, dependency management was quite an uphill task especially for newbie developers or even seasoned developers as it was required to know the compatible dependencies required in order to make your application up and running.

Spring Boot manages dependencies and configuration automatically. Each release of Spring Boot provides a list of dependencies that it supports. The list of dependencies is available as a part of the?Bills of Materials?or?BOM?which is essentially spring-boot-dependencies that can be used with?Maven. This means that you don’t necessarily need to mention the version of the dependency as Spring manages it.?This avoids the mismatch of different versions of Spring Boot libraries and is quite useful if you are working on a multi-module project.

Auto-configuration

If you ask me, this is the?most important feature?of Spring Boot is auto-configuration. It auto-configures your application according to your dependencies. It is not only intelligent and effective but also contextually smart and keep a record of your requirements.

Let us take the example of a database feature. In case you have added a requirement to a pom.xml, which somehow relates to a database, Spring boot implies by itself that you would like to use a database and thus it allows your application to make use of the precise database anytime.

The annotation?@EnableAutoConfiguration?enables auto-configuration for spring boot, using which the?framework looks for?auto-configuration beans?on its classpath and automatically applies them. It is always used with?@Configuration?as shown below:

@Configuration
@EnableAutoConfiguration
class BootAutoConfigExample{
…
}        

Most auto-configuration respects your own configuration and backs off silently if you have provided your own configuration via your own beans.

Designs Standalone Applications

Spring boot allows you to design?stand-alone, production-grade quality applications?that you can run on any website without wasting time.?You might think that running a java application is extremely simple and easy. All you need to do is give a run command and everything starts happening exactly the way it should be. But that’s just your assumption (it was mine anyway???).

To run a java application, the following steps are required:

  1. Package your application
  2. Choose the type of web server where you want to run your application and download it
  3. Configure that webserver
  4. Organize the deployment process

BUT, if you’re using the Spring Boot framework to run your application, you just need to follow the below two steps:

  1. Package your application
  2. Run your application using commands such as?java -jar my-application.jar

That’s all you need to do.?Spring Boot takes care of the rest of your requirements by simply?configuring and deploying an embedded web server?like?Apache Tomcat?to your application.

Opinionated Configuration

As mentioned in the?spring.io documentation,


“We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.”

I cannot explain this feature any easier than this ??. Spring Boot takes an opinionated view before it starts building or deploying a new Spring application. When you use Java, you have adequate options to choose from, starting from the web, logging, collection framework, or the build tool you use.

Instead of having so many choices in Java, developers like to use only the popular libraries.?All that the Spring Boot does is that it loads and configures them in the most standard way. Hence, the developers don’t need to spend a lot of time configuring up the same thing over and over again. In this way, they have more time for writing code and meeting business requirements.

Spring Boot,?is Spring on steroids?if you will. It’s a great way to get started very quickly with almost the entire Spring stack. It helps you to set up a fully working application very quickly by providing you with intelligent default configurations that you are most likely to be satisfied, to begin with.

If you are like me, you would want more. Hence, concluding this with reference articles, you might want to look at.

Tips Every Spring Boot Developer Should Know

1.?Using @ModelAttribute Annotation

I saw several developers using Map<String, String> when there are many request parameters.

No alt text provided for this image

While there is nothing wrong with it, it misses readability. If another developer wants to know which params are supported, then the developer needs to go through the code and have to find all the params?the hard way.?Also, Swagger 2.0 specification does not support Map<String, String>.

@ModelAttribute?annotation can be used to map request parameters to a Java object. The Java object can have all the request parameters that an API is expecting. This way you can use all the javax validations on the java object.

No alt text provided for this image

2. When Using Feign Client, Use @Controlleradvice To Handle All the Unhandled Feignexception’s

It is good to have a global exception handler for all FeignExceptions. Most of the time, you want to send the same error response that the underlying service is sending.

No alt text provided for this image

3. Sometimes You Can Avoid Starting the Spring Application Context for Integration Tests

For integration tests, you’ll most likely annotate the tests classes with @SpringBootTest; the problem with this annotation is it will start the whole application context. But sometimes you can avoid it, consider you’re testing only the service layer and the only thing you need is a JPA connection.

In that case, you can use?@DataJpaTest?annotation that starts JPA components and Repository beans. and use?@Import?annotation to load the service class itself.

No alt text provided for this image

4. If You Want You Can Avoid Creating Multiple Properties Files for Each Spring Profile

If you prefer to have only one configuration (application.yml) file in your project, then you can separate each profile configuration using three dashes.

No alt text provided for this image

If you see in the above example, there are three sections, which are separated by three dashes. The first section is for common properties which are enabled for all the spring profiles. The second section is for integration-test spring profile and the third one is for the prod profile.

Thanks To : Vivek Naska & Hari Tummala


要查看或添加评论,请登录

Omar Ismail的更多文章

社区洞察

其他会员也浏览了