I have this bean in my Spring Java config:
@Bean
@Scope( proxyMode=ScopedProxyMode.TARGET_CLASS, value=SpringScopes.DESKTOP )
public BirtSession birtSession() {
return new BirtSession();
}
For tests, I need a mock without a scope (there is no “Desktop” scope in the test). But when I create a configuration for my test which imports the above configuration and contains:
@Bean
public BirtSession birtSession() {
return new MockSession();
}
I get a “Desktop” scoped mocked bean 🙁
How do I make Spring “forget” the @Scope annotation?
PS: It works when I don’t use @Import and use copy&paste but I don’t want to do that.
The problem seems to be in
ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod()that usesScopedProxyCreator.createScopedProxy()static method to create the scoped bean definition:As the
BeanDefinitionHolderreturns aRootBeanDefinitioninstead ofConfiguratioClassBeanDenitionthe scoped proxy bean definition (ie, theScopedProxyFactoryBean) cannot be overriden by another Java Configuration class.A workaround could be declaring the scoped beans to override in a xml configuration file and importing it with
@ImportResource.