Skip to content

Regression in 4.0.0 - A scoped-proxy bean cannot override another bean [SPR-11229] #15854

Closed

Description

@spring-projects-issues

Francisco Lozano opened SPR-11229 and commented

It seems the behavior between 4.0.0.RELEASE and 3.2.5.RELEASE has changed with regard to bean overrides.

In the sample project (https://github.com/flozano/spring-override-behavior-changed) there is a singleton bean with a "sayHello" method, which returns "Hello "+name.

The original bean is configured thru Java Config in the AppConfiguration class.

The overriding bean is configured thru overridingApplicationContext.xml.

There is a test that checks that the correct string is returned for each bean ("Hello Lisa" for the original not-overriden app-ctx, "Hello Maria" for the overriden one).

  • If Spring 3.2.5.RELEASE is used in the pom.xml, the test always passes.
  • If Spring 4.0.0.RELEASE is used in the pom.xml and the overriding bean is a scoped-proxy, the test fails.
  • If Spring 4.0.0.RELEASE is used in the pom.xml and the overriding bean is NOT a scoped-proxy, the test passes.
  • Changing the scope of the overriding bean or the original bean has no effect in the results
  • Making the original bean also a scoped-proxy has no effect either
  • Configuring the original bean with XML instead of Java Config makes it work (???): removing the component-scan and adding this to originalApplicationContext.xml instead makes it work:
<bean id="theService" class="com.flozano.testbeanoverride.impl.TheServiceBean"
     scope="singleton" p:name="Lisa">
</bean>

Affects: 4.0 GA

Reference URL: https://github.com/flozano/spring-override-behavior-changed

Issue Links:

Referenced from: commits 61a3d04

1 votes, 3 watchers

Activity

  1. spring-projects-issues commented on Dec 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Francisco Lozano commented

    3.2.6 is not affected.
    In 4.0.0, with aop:scoped-proxy, I see:

    INFO: Overriding bean definition for bean 'theService': replacing [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in class path resource [�overridingApplicationContext.xml]] with [Root bean: class [null]; scope=singleton; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfiguration; factoryMethodName=theService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/flozano/testbeanoverride/config/AppConfiguration.class]]

    while without aop:scoped-proxy, I see:

    INFO: Skipping bean definition for [BeanMethod:name=theService,declaringClass=com.flozano.testbeanoverride.config.AppConfiguration]: a definition for bean 'theService' already exists. This top-level bean definition is considered as an override.

  2. spring-projects-issues commented on Dec 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Looks like our overriding check doesn't properly detect the scenario there. With a Java/XML mix, it's not just the order of registration that matters, since XML is typically processed first but is still supposed to override same-named @Bean methods. That special override check seems to fail in your scenario.

    If you're defining both beans in XML, it is simply the order of registration that matters, so you never run into that part of the algorithm and never experience such a mismatch.

    Juergen

  3. spring-projects-issues commented on Dec 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Turns out that this is a side effect of #15236 where the bean definition 'role' is being considered in the overriding algorithm now. Since scoped proxy definitions were marked as ROLE_INFRASTRUCTURE, they were excluded according to that new rule there. I've changed the scoped proxy role to use the same role as the target bean definition, which makes your scenario work properly again.

    Juergen

  4. spring-projects-issues commented on Dec 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Francisco Lozano commented

    Wow that was fast, thank you. I'll wait for 4.0.1 then.

  5. spring-projects-issues commented on Dec 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    You could try a 4.0.1 snapshot - see http://projects.spring.io/spring-framework/ for repo information. Would be great to verify that it actually works for you now...

    Juergen

  6. spring-projects-issues commented on Dec 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Francisco Lozano commented

    I can confirm it works with the 4.0.1 SNAPSHOT:

    Dec 17, 2013 8:24:48 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32fcc6e8: startup date [Tue Dec 17 08:24:48 CET 2013]; root of context hierarchy
    Dec 17, 2013 8:24:49 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [originalApplicationContext.xml]
    Dec 17, 2013 8:24:49 AM org.springframework.context.support.AbstractApplicationContext doClose
    INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@32fcc6e8: startup date [Tue Dec 17 08:24:48 CET 2013]; root of context hierarchy
    Dec 17, 2013 8:24:49 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1fabeb4c: startup date [Tue Dec 17 08:24:49 CET 2013]; root of context hierarchy
    Dec 17, 2013 8:24:49 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [�overridingApplicationContext.xml]
    Dec 17, 2013 8:24:49 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [originalApplicationContext.xml]
    Dec 17, 2013 8:24:49 AM org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader isOverriddenByExistingDefinition
    INFO: Skipping bean definition for [BeanMethod:name=theService,declaringClass=com.flozano.testbeanoverride.config.AppConfiguration]: a definition for bean 'theService' already exists. This top-level bean definition is considered as an override.
    Dec 17, 2013 8:24:49 AM org.springframework.context.support.SimpleThreadScope registerDestructionCallback
    WARNING: SimpleThreadScope does not support descruction callbacks. Consider using a RequestScope in a Web environment.
    Dec 17, 2013 8:24:49 AM org.springframework.context.support.AbstractApplicationContext doClose
    INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1fabeb4c: startup date [Tue Dec 17 08:24:49 CET 2013]; root of context hierarchy

  7. spring-projects-issues commented on Dec 17, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Great to know! Thanks for giving it a try...

    Juergen

  8. added
    type: regression
    in: core
    on Jan 11, 2019
  9. added this to the 4.0.1 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: regression

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions