001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.dao.annotation;
018
019import java.lang.annotation.Annotation;
020
021import org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor;
022import org.springframework.beans.factory.BeanFactory;
023import org.springframework.beans.factory.ListableBeanFactory;
024import org.springframework.stereotype.Repository;
025import org.springframework.util.Assert;
026
027/**
028 * Bean post-processor that automatically applies persistence exception translation to any
029 * bean marked with Spring's @{@link org.springframework.stereotype.Repository Repository}
030 * annotation, adding a corresponding {@link PersistenceExceptionTranslationAdvisor} to
031 * the exposed proxy (either an existing AOP proxy or a newly generated proxy that
032 * implements all of the target's interfaces).
033 *
034 * <p>Translates native resource exceptions to Spring's
035 * {@link org.springframework.dao.DataAccessException DataAccessException} hierarchy.
036 * Autodetects beans that implement the
037 * {@link org.springframework.dao.support.PersistenceExceptionTranslator
038 * PersistenceExceptionTranslator} interface, which are subsequently asked to translate
039 * candidate exceptions.
040 *
041
042 * <p>All of Spring's applicable resource factories (e.g.
043 * {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean})
044 * implement the {@code PersistenceExceptionTranslator} interface out of the box.
045 * As a consequence, all that is usually needed to enable automatic exception
046 * translation is marking all affected beans (such as Repositories or DAOs)
047 * with the {@code @Repository} annotation, along with defining this post-processor
048 * as a bean in the application context.
049 *
050 * @author Rod Johnson
051 * @author Juergen Hoeller
052 * @since 2.0
053 * @see PersistenceExceptionTranslationAdvisor
054 * @see org.springframework.stereotype.Repository
055 * @see org.springframework.dao.DataAccessException
056 * @see org.springframework.dao.support.PersistenceExceptionTranslator
057 */
058@SuppressWarnings("serial")
059public class PersistenceExceptionTranslationPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor {
060
061        private Class<? extends Annotation> repositoryAnnotationType = Repository.class;
062
063
064        /**
065         * Set the 'repository' annotation type.
066         * The default repository annotation type is the {@link Repository} annotation.
067         * <p>This setter property exists so that developers can provide their own
068         * (non-Spring-specific) annotation type to indicate that a class has a
069         * repository role.
070         * @param repositoryAnnotationType the desired annotation type
071         */
072        public void setRepositoryAnnotationType(Class<? extends Annotation> repositoryAnnotationType) {
073                Assert.notNull(repositoryAnnotationType, "'repositoryAnnotationType' must not be null");
074                this.repositoryAnnotationType = repositoryAnnotationType;
075        }
076
077        @Override
078        public void setBeanFactory(BeanFactory beanFactory) {
079                super.setBeanFactory(beanFactory);
080
081                if (!(beanFactory instanceof ListableBeanFactory)) {
082                        throw new IllegalArgumentException(
083                                        "Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory");
084                }
085                this.advisor = new PersistenceExceptionTranslationAdvisor(
086                                (ListableBeanFactory) beanFactory, this.repositoryAnnotationType);
087        }
088
089}