简体   繁体   中英

Setting class properties in Java, Spring config Vs System Properties file

We can use both Spring config file OR a .properties file to store and retrieve some properties, like for a database connection. (db url, db password and etc)

We can also use Spring config file and a .properties file together, where we reference the property from a .property file (like in ant)

What would be the advantages or disadvantages for the following scenarios:

1 - Using only .properties file.
2 - Using only Spring config file.
3 - Using both together.

Would any of the scenarios be better when it comes to maintenance?

I need to choose between the three, and I would like to have a better judgement before I go with any of the option!

Thanks in advance!
- Ivar

Both together. Use a properties file that's externalizable from your project to configure Spring. Spring then configures your project. Mostly, you don't write code to read from properties files. Let Spring manage that and inject your objects with the appropriate values. Then you have appropriate dependency injection and the artifact you build isn't environment-specific.

Disadvantages:

  1. How does your code know what file to load the properties from? Isn't that a property? It also violated dependency injection by having code go find a resource rather than passively accepting one.
  2. Configuration is tightly coupled to your artifact and can't change between environments without rebuilding (BAD).
  3. The way you seem to think of it, this combines the disadvantages of the other two, but if you do it the way I described, it eliminates those disadvantages, which is an advantage.

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