5 min read

Spring Boot Beginner mistakes and how to avoid them

October 02, 2020

There has been a lot of interest over Spring Boot in the last few years. Let’s face it. Spring Boot is currently the Go-To solution for developing a web service by many developers. For some people, it is even synonymous to microservices. So it is understandable that every beginner wants to use Spring Boot.

But here is a problem. Spring Boot makes the development simple. But it is simple only when the developers know what they are doing. Here are some of the Spring Boot mistakes that Rookie developers make.

Not Reading the Documentation

Reading documentation is common sense for anything that you might want to use. The documentation for Spring Boot covers pretty much everything that is there to understand (If you know Spring Framework). Of course, the documentation won’t tell you how to build for your application requirements. But it can help to enhance your knowledge.

Poor Dependency Management

Spring Boot takes care of most of the dependencies for you. Here is the list of dependencies that Spring Boot manages by default. For instance, Take a look at this screenshot. spring boot managed dependencies The dependency entry for h2 and Lombok doesn’t have any versions. So is all the starter dependencies. But I have added version to IText-pdf because I know for sure it is not managed by Spring Boot. And if you have to override the default versions, then use the version properties.

Some other mistakes are to include Spring dependencies manually. If you have to add a jar for a spring module, REMEMBER ONE THING. They may already be a spring-boot-starter for that.

Not everything is @Something

To a man with a hammer, everything looks like a nail. This statement is accurate for most of the developers who use spring boot too. Marking everything as a Bean or Service or a Component is a bad idea. It is unnecessary and will make the application slow at the starting time.

And things will look ugly when you decide to switch to a different framework later. So it is better to write interfaces and implementations then define beans when necessary.

Let’s say you have a code that looks like this.

@Service
public class MyServiceImpl {
...
...
}
@Service
public class MyOtherServiceImpl implements OtherService{
@Autowired
private  MyServiceImpl myServiceImpl;
...
...
}

In the above code, OtherServiceImpl can initialize MyserviceImpl within itself. No need to define it as a service and auto-wire it later. The correct way to write this code is as shown below.

public class SomeServiceImpl {
...
...
}
public class SomeOtherServiceImpl implements OtherService{

private SomeServiceImpl someServiceImpl;

public SomeOtherServiceImpl (SomeServiceImpl someServiceImpl){
	this.someServiceImpl=someServiceImpl;
}
...
...
}
@Beanpublic OtherService getOtherService(){   SomeServiceImpl someServiceImpl= new SomeServiceImpl ();   return new MyOtherServiceImpl(someServiceImpl);}

Not Following Convention

Spring Boot has some reasonable defaults and ideal behaviours. If you are not following these, then you are most likely shooting yourself in the foot.

For Example, The Main class should always be under the root package. Otherwise, you will have to add @ComponentScan which would look ugly and will be hard to refactor. Spring boot supports JSON for requests and responses by default. You can probably add further code to support xml. But the same can be done simply by adding a jackson parser dependency for XML.

For every spring-module, there is probably a starter with appropriate configuration options for them. So avoid adding dependencies manually and write less code. Remember, You don’t have bugs on the code you didn’t write.

The AutoConfiguration is opinionated. However, it gives ways to customize those behaviours progressively. Feel free to do those changes.

Not understanding AutoConfigurations

The reasonable defaults are coming from the AutoConfigurations of each starters. If you want to change the behaviour of these, then you should first understand how they work. some of these starters use properties. And some other expect a certain class in the classpath or a Bean to be in the context. Knowing this may not only help developing the application but

Not knowing Spring-Framework

Creating a RESTful Webservice is a no-brainer. Download the template from start.spring.io. Add a bunch of annotations that you see on the internet and voila. You got yourself a service running on port 8080.

But the fact that this is simple is because Spring Boot internally manages MVC related settings for you. These are called auto-configurations. And guess what? All of these uses spring beans and some of the core features of Spring Framework.

As I said earlier, Spring boot makes the development simple. But once the application starts to evolve, You have to understand the internal components. For this reason, you should learn the basics of Spring Framework like Beans and Autowiring, Annotations in Java etc.





These are few of the mistakes that I think every beginner should know. Do you have any? Comment Below. Thanks.
Raja Anbazhagan

About the author

Raja is a Software Engineer with over 7 years of experience in working with Enterprise Java applications. Lately, He is focused in cloud-based Java applications and serverless technologies. He spends his spare time in stackoverflow.

Browse Categories