I load additional singleton beans definitions at runtime from external jar file into existing XmlWebApplicationContext of my application:
BeanFactory beanFactory = xmlWebApplicationContext.getBeanFactory();
DefaultListableBeanFactory defaultFactory = (DefaultListableBeanFactory)beanFactory;
final URL url = new URL("external.jar");
final URL[] urls = {url};
ClassLoader loader = new URLClassLoader(urls, this.getClass().getClassLoader());
defaultFactory.setBeanClassLoader(loader);
final ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(defaultFactory);
final DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
resourceLoader.setClassLoader(loader);
scanner.setResourceLoader(resourceLoader);
scanner.scan("com.*");
Object bean = xmlWebApplicationContext.getBean("externalBean");
After all above xmlWebApplicationContext contains all external definitions of beans. But when i am trying to get bean from context exception is thrown:
Couldn't generate CGLIB proxy for class ...
I saw in debug mode that in the bean initialization process first time proxy is generated by
org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator
and than it is tried to generate proxy with
org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
but fails with mentioned exception.