We were using 4.2.x version of spring and we are using ContextSingletonBeanFactoryLocator to load bean like below
BeanFactoryLocator bfLocator = ContextSingletonBeanFactoryLocator.getInstance("classpath:customBeanRefFactory.xml");
BeanFactoryReference ref = bfLocator.useBeanFactory("sharedApplicationContext");
BeanFactory beanFactory = ref.getFactory();
((AbstractApplicationContext) beanFactory).getBeanFactory().setBeanClassLoader(CustomSpringBeanFactory.class.getClassLoader());
return (ApplicationContext) beanFactory
We are planning to upgrade to spring 5.0.x and figured out ContextSingletonBeanFactoryLocator and classes like BeanFactoryLocator and BeanFactoryReference are removed from spring 5.0 release.
So what are the suggested alternatives to get application context?
@Configuration
@ImportResource("classpath:ourxml")
public class OurApplicationConfiguration {
}
public class OurAppicationFactoryProvider {
ApplicationContext context;
public ApplicationContext getApplicationContext() {
if (context == null) {
synchronized (this) {
if (context == null) {
context = new AnnotationConfigApplicationContext(OurApplicationConfiguration.class);
}
}
}
return context;
}
}
Is this even right approach or there are other alternatives?
ApplicationContext
is already a flaw, in an application you shouldn't need it (assuming that Spring is creating the context). Your "solution" makes it even worse as that will load the context twice (assuming Spring is also instantiating the context). – Unscratched@Autowire
theApplicationContext
into yourOurAppicationFactoryProvider
. Don't create a new one (in the worst case you also have class loader issues and you load the application numerous times). – Unscratched