Overuse of Annotations in Spring Good or Bad Practice ?

Chaudhary Vivek Kadiyan
3 min readMar 1, 2024

--

Hello everyone, welcome to another interesting blog post where we will discuss another important topic which we usually avoid or ignore while coding, so without any delay let’s dive deep into it.

“Spring Annotations”

We are going to cover the topic with the help of 3–4 examples.

Let’s get started

Case #1 :=========================================>>>>>>>>>>>

@Component
public class AnyUtilityService{
//In this method let suppose we are doing some string manipulation
public String encryptString(String input){
//returning the value
}
}

Problem With Above Approach

So you can see in the above class we have a utility method and also in this class we are not injecting or using any other
Dependency class So there is no benefit of creating this class as Spring Service Bean or adding it to Spring Bean Registry

Rather we can use like this, declare the method as static and use it wherever required

Better Approach would be as below

class AnyUtilityService{
//In this method let suppose we are doing some string manipulation
public static String encryptString(String input){
//returning the value
}
}

NOTE

Although there will be no error or problem in making the upper class a Spring Bean class but there is no such benefit in making this Spring Bean. This will count in excessive use of Spring annotations .

The above case is applicable not only for @Component but also for all dependency injection annotations like @Service etc.
The issue would be that this approach could be a maintenance issue.

CASE #2 : =========================================>>>>>>>>>>

@RestController
public class ApplicationController{
@Autowired
private ApplicationService applicationService;
@GetMapping("/users")
public @ResponseBody List<Users> getAllUsers(){
return applicationService.getAllUsers();
}
}

Problem with Above

As per above example if we are using @RestController then what is the benefit of using
@ResponseBody, it would just be redundant so you can change the case like below.
There are many annotation hierarchies in Spring MVC where we have parent child pairs so adding both parent and child will clutter the code and
It would appear that there is no proper understanding of how to implement this. So this case will also count in excessive use of spring annotations.

Better Approach for Above

@RestController
public class ApplicationController{
@Autowired
private ApplicationService applicationService;

@GetMapping("/users")
public List<Users> getAllUsers(){
return applicationService.getAllUsers();
}
}

Case #3:=========================================>>>>>>>>>>>

/**
Code is written by @VIVEK
**/
@Service
public class ApplicationService{
@Autowired
private final ApplicationRepossitory applicationRepo;
@Autowired
private final UserRepository userRepo;
@Autowired
private final AddressRepository addressRepo;


public List<Address> getAllAddress(){
return addressRepo.findAll();
}
public List<Application> getAllApplications(){
return applicationRepo.findAll();
}
public List<Users> getAllUsers(){
return userRepo.findAll();
}
}

Problem with above example

Putting @Autowired annotation on every field in the above example would be an excessive annotation approach as it is also not a recommended approach for injecting a bean, so we can easily replace it with “Constructor Base Injection”.

Better Approach for above case would be as

/** 
Code is Written by @Vivek
**/
@Service
public class ApplicationService{

private final ApplicationRepossitory applicationRepo;

private final UserRepository userRepo;

private final AddressRepository addressRepo;

public ApplicationService(ApplicationRepossitory applicationRepo , UserRepository userRepo , AddressRepository addressRepo){
this.applicationRepo = applicationRepo;
this.userRepo = userRepo;
this.addressRepo = addressRepo;
}
public List<Address> getAllAddress(){
return addressRepo.findAll();
}
public List<Application> getAllApplications(){
return applicationRepo.findAll();
}
public List<Users> getAllUsers(){
return userRepo.findAll();
}
}

So It is very much important where ever we are using spring annotation , use wisely so there would no clutter code or no maintainability issue and there would be better readability of your code.

That’s all for today .

If you like the blog’s content then please don’t forget to clap and subscribe my medium channel for such interesting topics.

Thanks Friends !!!!!!!!!!!!!!!!!!!!.

--

--