Reputation: 177
I'm working on service for sending email to customers. All settings like host, protocol etc, and also email sender or subject I have stored in application.properties.
In another place I have class:
@ConfigurationProperties(prefix = "email.properties")
public class EmailProperties {
private String sender;
private String subject;
and data from application.properties are copy to this class, and this works very well...
But what is my problem. I would like set subject e.g:
email.properties.subject=Hello CUSTOMER, thank you for registered.
and bind CUSTOMER for concrete customer name when i send email, like:
Hello Tom Rich, thank you for registered.
So I added to class EmailProperties method:
String getSubjectWithCustomer(User user){
...}
but I have no idea how can i bind CUSTOMER for concrete user. Probably i can do something like this:
String getSubjectWithCustomer(User user){
return subject.replaceAll("CUSTOMER", user.getUserName());
}
but i feel this is stupid solution. I would like use something better. Maybe do you have some idea? Maybe i can use SpEL for this? But i have no idea how, because everywhere i found only examples how binding XML files but not aplication.properties.
Maybe i can create some parser with using SpEL?
Regards.
Upvotes: 1
Views: 180
Reputation: 454
You could use Spring MessageSource where you can define parametrized and localized messages.
Something like:
public String getLocalizedSubject(Object[] params) {
return getLocalizedSubject(params, Locale.getDefault());
}
public String getLocalizedSubject(Object[] params, Locale locale) {
return messageSource.getMessage("subject", params, locale);
}
Using:
Object[] params = new Object[] { user.getUserName() };
getLocalizedSubject(params);
And define subject in the messages.properties
subject=Hello {0}, thank you for register.
Upvotes: 3