Member-only story

Stop using @value annotation in spring boot.

Gain Java Knowledge
5 min readMar 29, 2024

Configuration is an important topic of every application which has more than a couple of hundred lines of code. In case you are using Spring, you would typically use Spring’s @Value annotation to load values from a Java properties file.

Let’s have a look at the classic @Value annotation in Spring, it may look something like this:

@Service
public class EmployeeServiceImpl implements EmployeeService {

private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeServiceImpl.class);

@Value("${com.demo.secret-key}")
private String secretKey;

@Override
public List<Employee> getAllEmployee() {
LOGGER.info("Inside EmployeeServiceImpl getAllEmployee method.");
LOGGER.info("=====MY Secret Key ===>"+secretKey);
return null;
}
}

The @Value annotation can be used for injecting values into Spring managed beans.

If you do so, Spring would inject the value for the key com.demo.secret-key from your properties file right into your class and you are done. Easy! Maybe too easy.

So what are the disadvantages of this approach?

Here are the some main disadvantages of using this approach. I will discuss here below.

--

--

Gain Java Knowledge
Gain Java Knowledge

Written by Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.

Responses (5)