Hello.

In our application we use XML context and <cache:annotation-driven mode="aspectj"/> declaration in combination with JCache API. Also we disable bean definition overrides by setting GenericApplicationContext.setAllowBeanDefinitionOverriding(false) in an ApplicationContextInitializer.

Such combination leads to BeanDefinitionOverrideException because the bean "org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource" is registered twice:

String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);
parserContext.registerBeanComponent(new BeanComponentDefinition(sourceDef, sourceName));

Comment From: pivotal-cla

@nivolg Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: nivolg

@pivotal-cla This is an Obvious Fix

Comment From: pivotal-cla

@nivolg This Pull Request contains an obvious fix. Signing the Contributor License Agreement is not necessary.

Comment From: snicoll

Thanks for the report and PR @nivolg. I wasn't able to reproduce the problem with a vanilla project started from start.spring.io. Please look at it and let us know what is missing.

If it turns out that a change is required in Spring Framework, I unfortunately disagree that this is an obvious fix so you'll have to sign the CLA.

Comment From: pivotal-cla

@nivolg Thank you for signing the Contributor License Agreement!

Comment From: nivolg

Thanks for quick response. I can't access your vanilla project repopsitory. But I made example project to reproduce the issue. I missed an important details in description: aspectj and jcache are used together.

Also I signed CLA.

Comment From: snicoll

@nivolg bummer, it got created as private by default, sorry about that. Thank you for the sample, I missed the aspectJ bits.

Comment From: sbrannen

Thanks for the repro project.

I have confirmed that the following exception occurs.

Exception in thread "main" org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource#0' defined in null: Cannot register bean definition [Root bean: class [org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource#0': There is already [Root bean: class [org.springframework.cache.jcache.interceptor.DefaultJCacheOperationSource]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:995)
    at org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerBeanDefinition(BeanDefinitionReaderUtils.java:164)
    at org.springframework.beans.factory.xml.ParserContext.registerBeanComponent(ParserContext.java:125)
    at org.springframework.cache.config.AnnotationDrivenCacheBeanDefinitionParser$JCacheCachingConfigurer.registerCacheAspect(AnnotationDrivenCacheBeanDefinitionParser.java:256)
    at org.springframework.cache.config.AnnotationDrivenCacheBeanDefinitionParser$JCacheCachingConfigurer.access$100(AnnotationDrivenCacheBeanDefinitionParser.java:206)
    at org.springframework.cache.config.AnnotationDrivenCacheBeanDefinitionParser.registerCacheAspect(AnnotationDrivenCacheBeanDefinitionParser.java:100)
    at org.springframework.cache.config.AnnotationDrivenCacheBeanDefinitionParser.parse(AnnotationDrivenCacheBeanDefinitionParser.java:87)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1391)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1371)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:179)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:149)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:96)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:511)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:338)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:671)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)

But I have not yet confirmed the proposed fix.

We'll look into this.

Comment From: sbrannen

This has been merged in 50ccb1bfcd93318508ec656e2cfe513b2e2bc6d2 and revised in 5bd90538b32a84fd84026bceeae17b901cc39910.

Applied to 5.3.x and main (6.0); to be backported to 5.2.x as well (#27547).

Thanks