简体   繁体   中英

Constants in Properties File

My team is new to Spring Boot and most members were previously working on a J2EE legacy application. I have previous experience with Spring Boot and microservices. My question is about a pattern they are using that I've never seen before and want to understand why this may or may not be a good idea.

They have been creating several properties files and adding key value pairs to these property files which get injected into object fields using @Value or ResourceBundle.

For example something I would normally think of as an Enum like color will be added to a property file, the same with constants.

#color.properties
color.r = red
color.b = blue
color.g = green

It seems like a lot of unnecessary code is getting written, like injecting all the values into a class then creating an array with the values in them. We are also battling to get this working.

I thought the point of the properties file was for externalized config. Why is my team doing this when Java provides data types and structures for these things?

I suppose the way I would want to do this is by creating Enums and utility classes with static constant fields.

You are correct in saying

the point of the properties file was for externalized config

Put stuff into properties only if you need to allow this property to be configurable with different values per customer/environment/execution. Even adding some properties that are rarely changed can make your properties excessively large and cumbersome. So if those are constants not meant to be changed they are DEFINITELY do NOT belong in properties. Either use Enum or put them into an Interface or as static public members into abstract class. Call it Constants or something like this

Its not the right way,You are right we should use ENUM for such type of constants.

Normally, properties file is used to store project configuration data or settings

If you talking about Spring's application.properties then its the worst thing

please go through the below link for best use of Spring's application.properties

Spring Application.properties

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM