Shuk Liu
Shuk Liu

Reputation: 28

How to wire value in library as default and overwrite it once application defines properties explicitly

I tried to create a email util and it needs some server properties. So I'd like to create a package and let application use it. If applications specify properties for email util, it should work fine. If applications don't, it should work fine as well by using default properties in email util. That means applications don't need to specify email properties for that util.

Is it possible to do that with spring annotation @Value since applications are spring mvc or spring family? Or what is the best practice for it?

Example: Application:A , EmailUtils:E, Properties:P

  1. A use E without given P in A. P in E is used.
  2. A use E with given P in A. P in A is used.

ps: I would like not to hardcode properties in util and then overwrite it, even pass them to constructor.

Upvotes: 1

Views: 36

Answers (1)

Essex Boy
Essex Boy

Reputation: 7968

Just default it on the value annotation in EmailUtils

   @Value("${some.key:my default value}")
   private String stringWithDefaultValue;

Upvotes: 1

Related Questions