Development

Making all spring beans lazy-init

Spring applications configured with XML take an age to start.

Using a horrible hack, we can override the parsing of the XML to set all “beans” to be lazy-init.

This might mean that the application being tested can start more quickly.

Better to not use spring, but when forced to…

public class Junk extends TestCase {
 
 
 
    public void testnothing() throws Exception {
 
        BeanFactory factory = new JamesClasspathXMLApplicationConext("config.xml");
    }
 
    public static class JamesClasspathXMLApplicationConext extends ClassPathXmlApplicationContext {
        public JamesClasspathXMLApplicationConext(String configLocation) throws BeansException {
            super(configLocation);
        }
 
        public JamesClasspathXMLApplicationConext(String[] configLocations) throws BeansException {
            super(configLocations);
        }
 
        public JamesClasspathXMLApplicationConext(String[] configLocations, boolean refresh) throws BeansException {
            super(configLocations, refresh);
        }
 
        public JamesClasspathXMLApplicationConext(String[] configLocations, ApplicationContext parent) throws BeansException {
            super(configLocations, parent);
        }
 
        public JamesClasspathXMLApplicationConext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
            super(configLocations, refresh, parent);
        }
 
 
        protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
            super.initBeanDefinitionReader(beanDefinitionReader);
            beanDefinitionReader.setParserClass(JamesParserClass.class);
        }
 
    }
 
    public static class JamesParserClass extends DefaultXmlBeanDefinitionParser {
        protected void initDefaults(Element root) {
            super.initDefaults(root);
            setDefaultLazyInit("true");
        }
    }
 
}
comments powered by Disqus