How to add Security provider in a Spring application using Javaconfig?

Friday, July 11, 2014

I'm trying to add BouncyCastle to my Spring application but I am not sure how to add the provider to the provider list using JavaConfig.

Using XML configuration, I can use the MethodInvokingFactoryBean similar the following:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value=""/>
<property name="arguments">
<bean class="org.bouncycastle.jce.provider.BouncyCastleProvider"/>

However, I'm not sure of the right way to do this using JavaConfig. Should I still be using the MethodInvokingFactoryBean? I presumed since it is pure java, there would be a more direct approach. At the moment, I've added the directive to a @PostConstruct method in the JavaConfig object, but not too thrilled about it - it seems a little "hacky" to me:

public class AppConfig {
// other @Bean definitions

public void init(){
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());