Sönke Reimer opened SPR-15184 and commented

If a ProxyFactoryBean returning a bean of type 'PlatformTransactionManager' is annotated with @Primary in the configuration and another bean having the same interface type is also annotated with @Primary spring states out:

more than one 'primary' bean found among candidates: [targetTransactionManager, transactionManagerProxyFactory]

which seems to be o.k. On the other hand the ProxyFactoryBean annotated with @Primary is not detected as 'Primary' among other beans of the same type, when we remove the @Primary annotation from the other beans with the same type and and error is shown, that the bean can not be uniquely resolved in autowiring. Example:


    @Primary
    @Bean
    public PlatformTransactionManager targetTransactionManager(EntityManagerFactory entityManagerFactory) {
      return new JpaTransactionManager(entityManagerFactory);
    }

    @Primary
    @Bean
    public ProxyFactoryBean transactionManagerProxyFactory(
        @Qualifier("targetTransactionManager") PlatformTransactionManager transactionManager) {
      ProxyFactoryBean proxyFactory = new ProxyFactoryBean();
      proxyFactory.setInterfaces(PlatformTransactionManager.class);
      proxyFactory.setInterceptorNames("transactionLoggingInterceptor");
      proxyFactory.setTarget(transactionManager);
      return proxyFactory;
    }

This seems to be inconsistent.

Regards Sönke ```


Affects: 4.3.4

Comment From: snicoll

I can't reproduce the inconsistency. With the two primary annotations, nothing happens with a recent Spring version. This is because the second type does not expose a type that's know at the time where primary bean computation occurs.

In the code above, there's nothing that forces the factory bean to be created. The first bean has been elected as the primary candidate which explains why it's injected, even if primary is set only on the proxy. This happens because you need the transaction manager to be resolved in order to create the second bean.