@@ -38,6 +38,7 @@ public class TransactionManagerPostProcessor extends InstantiationAwareBeanPostP
3838 private ConfigurableListableBeanFactory beanFactory ;
3939 private PlatformTransactionManager transactionManager ;
4040 private int order = Ordered .LOWEST_PRECEDENCE ;
41+ private boolean initialized = false ;
4142
4243 /**
4344 * Gets the platform transaction manager from the bean factory if
@@ -50,7 +51,6 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
5051 "TransactionManagerPostProcessor requires a ConfigurableListableBeanFactory" );
5152
5253 this .beanFactory = (ConfigurableListableBeanFactory ) beanFactory ;
53- initialize ();
5454 }
5555
5656 /**
@@ -64,28 +64,34 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
6464 @ Override
6565 public boolean postProcessAfterInstantiation (Object bean , String name ) throws BeansException {
6666 if (bean instanceof TransactionManagerAware ) {
67- TransactionManagerAware tma = (TransactionManagerAware ) bean ;
68- tma .setTransactionManager (transactionManager );
67+ initialize ();
68+ if (transactionManager != null ) {
69+ TransactionManagerAware tma = (TransactionManagerAware ) bean ;
70+ tma .setTransactionManager (transactionManager );
71+ }
6972 }
7073 return true ;
7174 }
7275
7376 private void initialize () {
74- if (beanFactory .containsBean (GrailsApplication .TRANSACTION_MANAGER_BEAN )) {
75- transactionManager = beanFactory .getBean (GrailsApplication .TRANSACTION_MANAGER_BEAN , PlatformTransactionManager .class );
76- } else {
77- // Fetch the names of all the beans that are of type
78- // PlatformTransactionManager. Note that we have to pass
79- // "false" for the last argument to avoid eager initialisation,
80- // otherwise we end up in an endless loop (it triggers the current method).
81- String [] beanNames = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (
82- beanFactory , PlatformTransactionManager .class , false , false );
77+ if (transactionManager == null && beanFactory != null && !initialized ) {
78+ if (beanFactory .containsBean (GrailsApplication .TRANSACTION_MANAGER_BEAN )) {
79+ transactionManager = beanFactory .getBean (GrailsApplication .TRANSACTION_MANAGER_BEAN , PlatformTransactionManager .class );
80+ } else {
81+ // Fetch the names of all the beans that are of type
82+ // PlatformTransactionManager. Note that we have to pass
83+ // "false" for the last argument to avoid eager initialisation,
84+ // otherwise we end up in an endless loop (it triggers the current method).
85+ String [] beanNames = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (
86+ beanFactory , PlatformTransactionManager .class , false , false );
8387
84- // If at least one is found, use the first of them as the
85- // transaction manager for the application.
86- if (beanNames .length > 0 ) {
87- transactionManager = (PlatformTransactionManager )beanFactory .getBean (beanNames [0 ]);
88+ // If at least one is found, use the first of them as the
89+ // transaction manager for the application.
90+ if (beanNames .length > 0 ) {
91+ transactionManager = (PlatformTransactionManager )beanFactory .getBean (beanNames [0 ]);
92+ }
8893 }
94+ initialized = true ;
8995 }
9096 }
9197
0 commit comments