Dynamic Property Loader using Java Dynamic Proxy pattern
While reading through Stackoverflow, I came up to this interesting question: Java Properties File binding to Java Interface. The idea is simple but quite helpful.
Basically we create an interface like:
interface LoginConstants extends Constants { @DefaultStringValue ( "Welcome to my super app" ) @Key ( "appDescription" ) String appDescription(); @DefaultStringValue ( "Ok" ) @Key ( "okButtonLabel" ) String okButtonLabel(); } |
And whenever we want to use constant in our application, we simple use loginConstant.appDescription(). Our application framework creates a dynamic proxy implementation for this interface. All we have to do is just to call the method and we get the value either from property file or default one.
So below is the sample code:
So basically you create an interface with relevant methods and annotate them with @Key, @DefaultStringValue annotations.
Disclaimer: This by no means should be used in Production. The code is just for reference. A lot more optimizations can be done in the below code.
Read full article from Dynamic Property Loader using Java Dynamic Proxy pattern
No comments:
Post a Comment