Eric Sirianni opened SPR-5146 and commented

There doesn't seem to be a way to configure Spring so that imported bean definition files inherit the default-lazy-init behavior from the parent.

For example:

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

\

The CXF beans are "eagerly" instantiated even though the parent bean XML speficies "default-lazy-init=true". Is there a way to get the CXF beans to obey the lazy-init behavior that I want (aside from editing the META-INF/cxf/cxf.xml files myself and re-jarring CXF?). (the CXF beans take a few seconds to initialize and this is a problem with client apps in short-running JVMs that may not hit CXF on all code paths)

It seems like either: 1. 'default-lazy-init' should be inherited from the parent bean definition reader 2. There should be an attribute on the import element that allows for specifying the desired default-lazy-init behavior for the imported bean defs.


Affects: 2.5.5

6 votes, 6 watchers

Comment From: spring-projects-issues

Olivier Lamy commented

no news ?

Comment From: spring-projects-issues

Rossen Stoyanchev commented

This issue has been resolved through a selective bulk update, as part of a larger effort to better manage unresolved issues. To qualify for the update, the issue was either created before Spring 3.0 or affects a version older than Spring 3.0 and is not a bug.

There is a good chance the request was made obsolete, or at least partly outdated, by changes in later versions of Spring including deprecations. It is also possible it didn't get enough traction or we didn't have enough time to address it. One way or another, we didn't get to it.

If you believe the issue, or some aspects of it, are still relevant and worth pursuing at present you may re-open this issue or create a new one with a more up-to-date description.

We thank you for your contributions and encourage you to become familiar with the current process of managing Spring Framework JIRA issues that has been in use for over a year.

Comment From: spring-projects-issues

Arnout Engelen commented

As far as I can see this is still relevant and interesting.

default-lazy-init is of type 'defaultable-boolean' (so it's 'true', 'false' or 'default', and 'default' by default).

It looks like that when there's nested \ tags it does take the value from the parent \ tag, but when you \ another xml, the imported xml doesn't get the value from the \ around the \.

Comment From: spring-projects-issues

Arnout Engelen commented

As a workaround I'm currently using this:

public class LazyBeanDefinitionDocumentReader extends DefaultBeanDefinitionDocumentReader {

    private Environment environment;

    public void setEnvironment(Environment environment) {
        super.setEnvironment(environment);
        this.environment = environment;
    }

    @Override
    protected BeanDefinitionParserDelegate createHelper(XmlReaderContext readerContext, Element root,
                                                        BeanDefinitionParserDelegate parentDelegate) {
        BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, environment) {
            @Override
            protected void populateDefaults(DocumentDefaultsDefinition defaults,
                                            DocumentDefaultsDefinition parentDefaults, Element root) {
                if (parentDefaults == null) {
                    parentDefaults = new DocumentDefaultsDefinition();
                    parentDefaults.setLazyInit(TRUE_VALUE);
                }
                super.populateDefaults(defaults, parentDefaults, root);
            }
        };
        delegate.initDefaults(root, parentDelegate);
        return delegate;
    }
}

....

context = new GenericApplicationContext();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
reader.setDocumentReaderClass(LazyBeanDefinitionDocumentReader.class);
reader.loadBeanDefinitions(new ClassPathResource(contextLocation));

Comment From: spring-projects-issues

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Comment From: Avinm

I think this is still relevant as resource imports should work just like nested bean imports.

Comment From: Avinm

Any plans on a fix for this? Could contribute myself if someone were to point in the right direction

Comment From: Avinm

As a workaround to enable lazy-init by default for all beans used in test suits, I have added the below post processor to my test configurations:

@Bean
public BeanFactoryPostProcessor lazyInitProcessor() {
    return beanFactory -> {
        for (String beanName : beanFactory.getBeanDefinitionNames()) {
            beanFactory.getBeanDefinition(beanName).setLazyInit(true);
        }
    };
}

Comment From: snicoll

So there are two requests here, really. The original one is about changing the defaults when importing dedicated XML configurations. Each XML file is self-contained and we have no desire to change that at this point.

The other request is about changing all beans to be lazy (typically in integration tests). The BeanFactroyPostProcessor above is a good solution for this (the method could be static though). Spring Boot has a similar solution that is exposed via the spring.main.lazy-initialization property.