New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing overrides using ClassPathXmlApplicationContext [SPR-4374] #9052

Closed
spring-issuemaster opened this Issue Jan 22, 2008 · 6 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Jan 22, 2008

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:

  • #9063 Make AbstractApplicationContext BeanNameAware
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Feb 6, 2008

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Feb 6, 2008

Paul Benedict commented

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Feb 6, 2008

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Apr 13, 2009

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.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Apr 13, 2009

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);
	}

	
}
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Apr 13, 2009

Grzegorz Olędzki commented

and ContextLoaderListener:

public class ContextLoaderListener extends org.springframework.web.context.ContextLoaderListener {

	@Override
	protected ContextLoader createContextLoader() {
		return new NoBeanOverridingContextLoader();
	}
	
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment