Save This Page
Home » spring-framework-2.5.6-with-dependencies » org.springframework » dao » annotation » [javadoc | source]
    1   /*
    2    * Copyright 2002-2008 the original author or authors.
    3    *
    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
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   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.
   15    */
   16   
   17   package org.springframework.dao.annotation;
   18   
   19   import java.lang.annotation.Annotation;
   20   
   21   import org.springframework.aop.framework.Advised;
   22   import org.springframework.aop.framework.ProxyConfig;
   23   import org.springframework.aop.framework.ProxyFactory;
   24   import org.springframework.aop.support.AopUtils;
   25   import org.springframework.beans.BeansException;
   26   import org.springframework.beans.factory.BeanClassLoaderAware;
   27   import org.springframework.beans.factory.BeanFactory;
   28   import org.springframework.beans.factory.BeanFactoryAware;
   29   import org.springframework.beans.factory.ListableBeanFactory;
   30   import org.springframework.beans.factory.config.BeanPostProcessor;
   31   import org.springframework.core.Ordered;
   32   import org.springframework.stereotype.Repository;
   33   import org.springframework.util.Assert;
   34   import org.springframework.util.ClassUtils;
   35   
   36   /**
   37    * Bean post-processor that automatically applies persistence exception
   38    * translation to any bean that carries the
   39    * {@link org.springframework.stereotype.Repository} annotation,
   40    * adding a corresponding {@link PersistenceExceptionTranslationAdvisor}
   41    * to the exposed proxy (either an existing AOP proxy or a newly generated
   42    * proxy that implements all of the target's interfaces).
   43    *
   44    * <p>Translates native resource exceptions to Spring's
   45    * {@link org.springframework.dao.DataAccessException} hierarchy.
   46    * Autodetects beans that implement the
   47    * {@link org.springframework.dao.support.PersistenceExceptionTranslator}
   48    * interface, which are subsequently asked to translate candidate exceptions.
   49    *
   50    * <p>All of Spring's applicable resource factories implement the
   51    * <code>PersistenceExceptionTranslator</code> interface out of the box.
   52    * As a consequence, all that is usually needed to enable automatic exception
   53    * translation is marking all affected beans (such as DAOs) with the
   54    * <code>Repository</code> annotation, along with defining this post-processor
   55    * as bean in the application context.
   56    *
   57    * @author Rod Johnson
   58    * @author Juergen Hoeller
   59    * @since 2.0
   60    * @see PersistenceExceptionTranslationAdvisor
   61    * @see org.springframework.stereotype.Repository
   62    * @see org.springframework.dao.DataAccessException
   63    * @see org.springframework.dao.support.PersistenceExceptionTranslator
   64    */
   65   public class PersistenceExceptionTranslationPostProcessor extends ProxyConfig
   66   		implements BeanPostProcessor, BeanClassLoaderAware, BeanFactoryAware, Ordered {
   67   
   68   	private Class<? extends Annotation> repositoryAnnotationType = Repository.class;
   69   
   70   	private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
   71   
   72   	private PersistenceExceptionTranslationAdvisor persistenceExceptionTranslationAdvisor;
   73   
   74   
   75   	/**
   76   	 * Set the 'repository' annotation type.
   77   	 * The default required annotation type is the {@link Repository} annotation.
   78   	 * <p>This setter property exists so that developers can provide their own
   79   	 * (non-Spring-specific) annotation type to indicate that a class has a
   80   	 * repository role.
   81   	 * @param repositoryAnnotationType the desired annotation type
   82   	 */
   83   	public void setRepositoryAnnotationType(Class<? extends Annotation> repositoryAnnotationType) {
   84   		Assert.notNull(repositoryAnnotationType, "'requiredAnnotationType' must not be null");
   85   		this.repositoryAnnotationType = repositoryAnnotationType;
   86   	}
   87   
   88   	public void setBeanClassLoader(ClassLoader classLoader) {
   89   		this.beanClassLoader = classLoader;
   90   	}
   91   
   92   	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
   93   		if (!(beanFactory instanceof ListableBeanFactory)) {
   94   			throw new IllegalArgumentException(
   95   					"Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory");
   96   		}
   97   		this.persistenceExceptionTranslationAdvisor = new PersistenceExceptionTranslationAdvisor(
   98   				(ListableBeanFactory) beanFactory, this.repositoryAnnotationType);
   99   	}
  100   
  101   	public int getOrder() {
  102   		// This should run after all other post-processors, so that it can just add
  103   		// an advisor to existing proxies rather than double-proxy.
  104   		return LOWEST_PRECEDENCE;
  105   	}
  106   
  107   
  108   	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  109   		return bean;
  110   	}
  111   
  112   	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  113   		Class<?> targetClass =
  114   				(bean instanceof Advised ? ((Advised) bean).getTargetSource().getTargetClass() : bean.getClass());
  115   		if (targetClass == null) {
  116   			// Can't do much here
  117   			return bean;
  118   		}
  119   		
  120   		if (AopUtils.canApply(this.persistenceExceptionTranslationAdvisor, targetClass)) {
  121   			if (bean instanceof Advised) {
  122   				((Advised) bean).addAdvisor(this.persistenceExceptionTranslationAdvisor);
  123   				return bean;
  124   			}
  125   			else {
  126   				ProxyFactory proxyFactory = new ProxyFactory(bean);
  127   				// Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
  128   				proxyFactory.copyFrom(this);
  129   				proxyFactory.addAdvisor(this.persistenceExceptionTranslationAdvisor);
  130   				return proxyFactory.getProxy(this.beanClassLoader);
  131   			}
  132   		}
  133   		else {
  134   			// This is not a repository.
  135   			return bean;
  136   		}
  137   	}
  138   
  139   }

Save This Page
Home » spring-framework-2.5.6-with-dependencies » org.springframework » dao » annotation » [javadoc | source]