kinaesthesia
kinaesthesia

Reputation: 703

Library (JAR) default value for properties spring

I have an application that need to send emails. I might need later to send emails in a second application so I try to do a library to re-use it.

My application is done with Spring-boot. My library just include Spring-core, Spring-context and Spring-test.

This is my EmailUtils in my library :

@Named
public class EmailUtils {

  @Value("${from}")
  private String from;

  @Value("${to}")
  private String to;
  ...
}

I would like to have a default file application.properties with that in the library :

from=default-from
to=default-to

now I would like if my application spring boot does something like that :

@Service
public void EmailService {

  @Inject
  private EmailUtils emailUtils;
  ...
} 

I would like if I don't define a new application.properties in my application to get the default value from the library but would be possible to override them by using an application.properties in my Spring-boot app.

Info : My Spring-boot app have ScanComponent package (just to mention it). But my library has nothing. I don't know if I have to use a beans.xml and define a ContextPlaceHolder ...

I am a little bit loss, maybe there is a better way to do it ?

Thank you.

Upvotes: 1

Views: 1570

Answers (1)

Ben M
Ben M

Reputation: 1892

If I am correctly understanding your question - you want to specify a default value for your @Value annotated properties. You can do this simply by simply using the format @Value("${property_key:default value}") so if you wished the default from value to be [email protected] and to to be [email protected] your code would read:

@Named
public class EmailUtils {

  @Value("${from:[email protected]}")
  private String from;

  @Value("${to:[email protected]}")
  private String to;
  ...
}

Upvotes: 3

Related Questions