I have a dependency of ExternalContext class which is defined as below. Dependency is added by jar, so modification not allowed.
public class ExternalContext {
private String emailUsername;
private String emailPassword;
// setters & getters
private InitialContext initialContext;
public void init() {
Hashtable<String, String> props = new Hashtable<String, String>();
if (!(emailUsername == null)) {
props.put("SEC_PRINCIPAL", emailUsername);
}
if (emailPassword != null) {
props.put("SEC_CREDENTIALS", emailPassword);
}
initialContext = new InitialContext(props);
}
}
I need to create bean of above class and also initialize properties by calling it's init method. This is how I am trying to do
@Configuration
public class AppConfig {
@Value("${email.username}")
private String emailUsername;
@Value("${email.password}")
private String password;
private final PasswordDecryptor passwordDecryptor;
public AppConfig(PasswordDecryptor passwordDecryptor) {
this.passwordDecryptor = passwordDecryptor;
}
@Bean
public String emailPassword(){
return passwordDecryptor.decrypt(password);
}
@Bean(initMethod = "init" )
public ExternalContext externalContext(){
return new ExternalContext();
}
}
I have created bean of emailPassword so that value is available when init method of ExternalContext class is called.
Is there any better approach of initializing property in this scenario?
Is there any alternative where I need not to create bean of emailPassword?
I am using spring boot 2.1.15.RELEASE