1 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.ProxyFactory; 23 import org.springframework.aop.support.AopUtils; 24 import org.springframework.beans.BeansException; 25 import org.springframework.beans.factory.BeanClassLoaderAware; 26 import org.springframework.beans.factory.BeanFactory; 27 import org.springframework.beans.factory.BeanFactoryAware; 28 import org.springframework.beans.factory.ListableBeanFactory; 29 import org.springframework.beans.factory.config.BeanPostProcessor; 30 import org.springframework.core.Ordered; 31 import org.springframework.stereotype.Repository; 32 import org.springframework.util.Assert; 33 import org.springframework.util.ClassUtils; 34 35 64 public class PersistenceExceptionTranslationPostProcessor 65 implements BeanPostProcessor, BeanClassLoaderAware, BeanFactoryAware, Ordered { 66 67 private Class <? extends Annotation > repositoryAnnotationType = Repository.class; 68 69 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 70 71 private PersistenceExceptionTranslationAdvisor persistenceExceptionTranslationAdvisor; 72 73 74 82 public void setRepositoryAnnotationType(Class <? extends Annotation > repositoryAnnotationType) { 83 Assert.notNull(repositoryAnnotationType, "'requiredAnnotationType' must not be null"); 84 this.repositoryAnnotationType = repositoryAnnotationType; 85 } 86 87 public void setBeanClassLoader(ClassLoader classLoader) { 88 this.beanClassLoader = classLoader; 89 } 90 91 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 92 if (!(beanFactory instanceof ListableBeanFactory)) { 93 throw new IllegalArgumentException ( 94 "Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory"); 95 } 96 this.persistenceExceptionTranslationAdvisor = new PersistenceExceptionTranslationAdvisor( 97 (ListableBeanFactory) beanFactory, this.repositoryAnnotationType); 98 } 99 100 public int getOrder() { 101 return LOWEST_PRECEDENCE; 104 } 105 106 107 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 108 return bean; 109 } 110 111 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 112 Class <?> targetClass = 113 (bean instanceof Advised ? ((Advised) bean).getTargetSource().getTargetClass() : bean.getClass()); 114 if (targetClass == null) { 115 return bean; 117 } 118 119 if (AopUtils.canApply(this.persistenceExceptionTranslationAdvisor, targetClass)) { 120 if (bean instanceof Advised) { 121 ((Advised) bean).addAdvisor(this.persistenceExceptionTranslationAdvisor); 122 return bean; 123 } 124 else { 125 ProxyFactory pf = new ProxyFactory(bean); 126 pf.addAdvisor(this.persistenceExceptionTranslationAdvisor); 127 return pf.getProxy(this.beanClassLoader); 128 } 129 } 130 else { 131 return bean; 133 } 134 } 135 136 } 137 | Popular Tags |