2 * Copyright 2004-2006 Graeme Rocher
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.codehaus
.groovy
.grails
.orm
.support
;
18 import org
.springframework
.beans
.BeansException
;
19 import org
.springframework
.beans
.factory
.BeanFactory
;
20 import org
.springframework
.beans
.factory
.BeanFactoryAware
;
21 import org
.springframework
.beans
.factory
.BeanFactoryUtils
;
22 import org
.springframework
.beans
.factory
.config
.ConfigurableListableBeanFactory
;
23 import org
.springframework
.beans
.factory
.config
.InstantiationAwareBeanPostProcessorAdapter
;
24 import org
.springframework
.transaction
.PlatformTransactionManager
;
27 * This is a bean post processor that injects the platform transaction
28 * manager into beans that implement the {@link TransactionManagerAware}
30 * @author Graeme Rocher
33 public class TransactionManagerPostProcessor
extends InstantiationAwareBeanPostProcessorAdapter
implements BeanFactoryAware
{
34 private ConfigurableListableBeanFactory beanFactory
;
35 private PlatformTransactionManager transactionManager
;
36 private boolean initialized
= false;
39 * Gets the platform transaction manager from the bean factory if
41 * @param beanFactory The bean factory handling this post processor.
42 * @throws BeansException
44 public void setBeanFactory(BeanFactory beanFactory
) throws BeansException
{
45 if (!(beanFactory
instanceof ConfigurableListableBeanFactory
)) {
46 throw new IllegalArgumentException(
47 "TransactionManagerPostProcessor requires a ConfigurableListableBeanFactory");
50 this.beanFactory
= (ConfigurableListableBeanFactory
) beanFactory
;
54 * Injects the platform transaction manager into the given bean if
55 * that bean implements the {@link TransactionManagerAware} interface.
56 * @param bean The bean to process.
57 * @param name The name of the bean.
58 * @return The bean after the transaction manager has been injected.
59 * @throws BeansException
61 public synchronized boolean postProcessAfterInstantiation(Object bean
, String name
) throws BeansException
{
62 // Lazily retrieve the transaction manager from the bean factory.
63 // Attempting to retrieve it within 'setBeanFactory()' blocks
64 // other bean post processors from processing the beans in the
66 if (!this.initialized
) {
67 // Fetch the names of all the beans that are of type
68 // PlatformTransactionManager. Note that we have to pass
69 // "false" for the last argument to avoid eager initialisation,
70 // otherwise we end up in an endless loop (it triggers the
72 String
[] beanNames
= BeanFactoryUtils
.beanNamesForTypeIncludingAncestors(
74 PlatformTransactionManager
.class,
78 // If at least one is found, use the first of them as the
79 // transaction manager for the application.
80 if (beanNames
.length
> 0) {
81 this.transactionManager
= (PlatformTransactionManager
)beanFactory
.getBean(beanNames
[0]);
84 // Don't attempt to retrieve the transaction manager again.
85 this.initialized
= true;
88 if (bean
instanceof TransactionManagerAware
) {
89 TransactionManagerAware tma
= (TransactionManagerAware
) bean
;
90 tma
.setTransactionManager(this.transactionManager
);