如何在初始化之前读取Spring Boot Tomcat属性中的另一个用户定义的bean?

huangapple go评论79阅读模式
英文:

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(&quot;NAME_OF_YOUR_PROPERTIES&quot;)

then you can create and add your custom property source to the environment (with 'server.ssl.key-password' property)

Map&lt;String, Object&gt; properties = new HashMap&lt;&gt;();
properties.put(&quot;server.ssl.key-password&quot;, property); // property = environment.getProperty(&quot;NAME_OF_YOUR_PROPERTIES&quot;)
environment.getPropertySources().addLast(new MapPropertySource(&quot;muCustomPropertySource&quot;, properties));

also, read the following article - https://www.baeldung.com/spring-boot-environmentpostprocessor

huangapple
  • 本文由 发表于 2020年8月1日 22:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63206319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定