Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct approach to Properties

I am working in Java on a fairly large project. My question is about how to best structure the set of Properties for my application.

Approach 1: Have some static Properties object that's accessible by every class. (Disadvantages: then, some classes lose their generality should they be taken out of the context of the application; they also require explicit calls to some static object that is located in a different class and may in the future disappear; it just doesn't feel right, am I wrong?)

Approach 2: Have the Properties be instantiated by the main class and handed down to the other application classes. (Disadvantages: you end up passing a pointer to the Properties object to almost every class and it seems to become very redundant and cumbersome; I don't like it.)

Any suggestions?

like image 563
Jake Avatar asked Oct 30 '08 15:10

Jake


1 Answers

I like using Spring dependency injection for many of the properties. You can treat your application like building blocks and inject the properties directly into the component that needs them. This preserves (encourages) encapsulation. Then, you assemble your components together and create the "main class".

A nice side effect of the dependency injection is that your code should be more easily testable.

like image 107
Alex B Avatar answered Oct 13 '22 14:10

Alex B