3

I am looking to override the SSLContext of Spring at runtime. Hence I am trying to find ways to register the below method as a bean dynamically.

For Ex. When a GetMapping endpoint is invoked, the below method should be injected as a bean into Spring IoC.

public static SSLContext getSSLContext() throws Exception {
    TrustManager[] trustManagers = new TrustManager[] {
            new ReloadableX509TrustManager(truststoreNewPath)
    };
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustManagers, null);
    return sslContext;
}

How can I do this?

1

4 Answers 4

4

Spring 5 provides Bean registration, which can be done dynamically. Please look at documentation here and example here.

Supplier<SSLContext> sslcontextSupplier = () -> getSSLContext();
context.registerBean("sslcontext",SSLContext.class,sslcontextSupplier);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the ConfigurableBeanFactory to register beans manually at runtime.

@Service
public class RegisterBeansDynamically implements BeanFactoryAware {

    private ConfigurableBeanFactory beanFactory;

    public <T> void registerBean(String beanName, T bean) {
        beanFactory.registerSingleton(beanName, bean);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = (ConfigurableBeanFactory) beanFactory;
    }
}

But keep in mind: Your context must be refreshed making other beans able getting your new bean automatically injected or they have to access them dynamically from the application context.

The BeanFactory can also be accessed directly through the application context see this answer.

Comments

1

Here is the demo.

public class Demo implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        ConfigurableApplicationContext context = (ConfigurableApplicationContext)applicationContext;
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(YourClass.class);

        beanDefinitionBuilder.addPropertyValue("property1", "propertyValue");
        beanDefinitionBuilder.addPropertyValue("property2", applicationContext.getBean(AnotherClass.class));


        beanFactory.registerBeanDefinition("yourBeanName", beanDefinitionBuilder.getBeanDefinition());


    }
}

You can move the register part to your method(start from BeanDefinitionBuilder). I suppose this will suit your demand.

Comments

0

For registering beans dynamically from metadata of Class use BeanDefinitionRegistryPostProcessor class of Spring boot

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.