Skip to content

Preventing overrides using ClassPathXmlApplicationContext [SPR-4374] #9052

Closed

Description

@spring-projects-issues

Karl Baum opened SPR-4374 and commented

We ran into an issue in which we unknowingly had two bean configurations with the same id. Since we are using the simple ClassPathXmlApplicationContext retrieved from a SingletonBeanFactoryLoader, the default behaviour is to override one definition with another. This is not what we want, however, since we use regexp to load our app ctx files from the classpath. We would consider this an issue and would rather spring treated this as a fatal exception on initialization. DefaultListableBeanFactory provides a field allowBeanDefinionOverriding, but this option is not available within the ClassPathXmlApplicationContext.

Can we include the allowBeanDefinitionOverriding="false" option within the ClassPathXmlApplicationContext hiearchy?

thx.

-karl


Affects: 2.5.1

Issue Links:

Activity

  1. spring-projects-issues commented on Feb 6, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I've added "allowBeanDefinitionOverriding" and "allowCircularReferences" properties to AbstractRefreshableApplicationContext, which ClassPathXmlApplicationContext derives from. Those will be propagated to each underlying DefaultListableBeanFactory.

    This fits nicely with ClassPathXmlApplicationContext's new bean-style configuration variant: You can use the default constructor and a "configLocations" property there now, as well as set other properties (such as "allowBeanDefinitionOverriding"), before calling refresh (triggered by ClassPathXmlApplicationContext's new afterPropertiesSet implementation as well).

    All of this is going to be available in tonight's 2.5.2 snapshot.

    Juergen

  2. spring-projects-issues commented on Feb 6, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Paul Benedict commented

    Juergen, can you add these also to the XSD? I would like to set these attributes globally at the root tag.

  3. spring-projects-issues commented on Feb 6, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I'm afraid those attributes are not really candidates for the <beans> tag... mainly because they apply to an entire ApplicationContext, not to an isolated bean definition file with a set of bean definitions. Overriding in particular is a characteristic that emerges from loading multiple bean definition files to begin with.

    We could support a "contextProperties" attribute analogous to "contextClass", containing key-value pairs of ApplicationContext bean properties... applicable to web.xml-driven setup. For beanRefContext.xml-style setup, those properties are easy enough to set via standard <property> elements. And for programmatic setup, they can obviously be called directly.

    Juergen

  4. spring-projects-issues commented on Apr 13, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Grzegorz Olędzki commented

    Shouldn't setAllowBeanDefinitionOverriding() method be promoted to ConfigurableWebApplicationContext interface?
    I'm trying to subclass ContextLoaderListener and ContextLoader so that they set the allowBeanDefinitionOverriding property on the web application context being created, but customizeContext method accepts ConfigurableWebApplicationContext as parameter.
    So currently a cast to AbstractRefreshableApplicationContext is needed.

  5. spring-projects-issues commented on Apr 13, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Grzegorz Olędzki commented

    My code is:

    public class NoBeanOverridingContextLoader extends ContextLoader {
    
    	@Override
    	protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
    		super.customizeContext(servletContext, applicationContext);
    		AbstractRefreshableApplicationContext arac = (AbstractRefreshableApplicationContext) applicationContext;
    		arac.setAllowBeanDefinitionOverriding(false);
    	}
    
    	
    }
    
  6. spring-projects-issues commented on Apr 13, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Grzegorz Olędzki commented

    and ContextLoaderListener:

    public class ContextLoaderListener extends org.springframework.web.context.ContextLoaderListener {
    
    	@Override
    	protected ContextLoader createContextLoader() {
    		return new NoBeanOverridingContextLoader();
    	}
    	
    }
    
    
  7. added
    type: enhancement
    in: core
    on Jan 11, 2019
  8. added this to the 2.5.2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions