Skip to content

PersistenceExceptionTranslationInterceptor attempting to instantiate prototype PersistenceExceptionTranslator beans #26412

Closed
@krm1312

Description

@krm1312

Affects: spring framework 5.3.3

Attempting to upgrade our application to boot 2.4.2 from 2.3.4 and ran into this issue.

We have a prototype bean defined as:

    @Bean(name = "protoConnectionFactory", autowireCandidate = false)
    @Scope(value = SCOPE_PROTOTYPE)
    public RedisConnectionFactory protoConnectionFactory(@NonNull Bar instance) {
    }

If there is a persistence exception this now causes the following exception (at runtime):

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'protoConnectionFactory' defined in FooConfig: Unsatisfied dependency expressed through method 'protoConnectionFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'foo.Bar' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
 at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
 at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1179)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:353)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory$1.orderedStream(DefaultListableBeanFactory.java:481)
 at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:167)
 at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:149)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
 at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
 at com.sun.proxy.$Proxy214.saveAndFlush(Unknown Source)

In other words, it is trying to instantiate the prototype bean now (RedisConnectionFactory is a PersistenceExceptionTranslator).

I believe this was caused by:

14839b1

Previously:

  Map<String, PersistenceExceptionTranslator> pets = BeanFactoryUtils.beansOfTypeIncludingAncestors(
  beanFactory, PersistenceExceptionTranslator.class, false, false);

Excluded prototypes (the first false).

I'm pretty sure we could workaround this behavior by dynamically registering these beans (not using prototypes), but, this seems like a potential regression.

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions