英文:
How to read spring boot tomcat properties another user defined bean before initialization?
问题
I want to read spring boot ssl keystore password value from user defined spring bean and set to SSL property server.ssl.key-password.
@Component
public class KeyStorePasswordBean {
public String password() {
//implementation
}
}
I tried with EnvironmentPostProcessor but NullPointer exception thrown because KeyStorePasswordBean bean will be initialized after EnvironmentPostProcessor
public class PropertyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Autowired
KeyStorePasswordBean keyStorePasswordBean;
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String keystorePassword = keyStorePasswordBean.password(); //NullPointerException
System.out.println("password: " + keystorePassword);
}
}
How to handle this in spring boot?
Thinking on how to load KeyStorePasswordBean bean to spring context before tomcat server initialization start.
I am using spring boot version 2.25 and spring version 5.2.4
英文:
I want to read spring boot ssl keystore password value from user defined spring bean and set to SSL property server.ssl.key-password.
@Component
public class KeyStorePasswordBean {
public String password() {
//implementation
}
}
I tried with EnvironmentPostProcessor but NullPointer exception thrown because KeyStorePasswordBean bean will be initialized after EnvironmentPostProcessor
public class PropertyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Autowired
KeyStorePasswordBean keyStorePasswordBean;
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String keystorePassword = keyStorePasswordBean.password(); //NullPointerException
System.out.println("password: " + keystorePassword);
}
}
How to handle this in spring boot?
Thinking on how to load KeyStorePasswordBean bean to spring context before tomcat server initialization start.
I am using spring boot version 2.25 and spring version 5.2.4
答案1
得分: 1
You can read the following property via:
ConfigurableEnvironment environment
like that:
environment.getProperty("NAME_OF_YOUR_PROPERTIES")
then you can create and add your custom property source to the environment (with 'server.ssl.key-password' property)
Map<String, Object> properties = new HashMap<>();
properties.put("server.ssl.key-password", property); // property = environment.getProperty("NAME_OF_YOUR_PROPERTIES")
environment.getPropertySources().addLast(new MapPropertySource("muCustomPropertySource", properties));
also, read the following article - https://www.baeldung.com/spring-boot-environmentpostprocessor
英文:
you can read the following property via:
ConfigurableEnvironment environment
like that:
environment.getProperty("NAME_OF_YOUR_PROPERTIES")
then you can create and add your custom property source to the environment (with 'server.ssl.key-password' property)
Map<String, Object> properties = new HashMap<>();
properties.put("server.ssl.key-password", property); // property = environment.getProperty("NAME_OF_YOUR_PROPERTIES")
environment.getPropertySources().addLast(new MapPropertySource("muCustomPropertySource", properties));
also, read the following article - https://www.baeldung.com/spring-boot-environmentpostprocessor
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论