001/*
002 * Copyright 2002-2018 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.beans.factory.support;
018
019import org.springframework.beans.factory.BeanDefinitionStoreException;
020import org.springframework.beans.factory.config.BeanDefinition;
021import org.springframework.lang.NonNull;
022
023/**
024 * Subclass of {@link BeanDefinitionStoreException} indicating an invalid override
025 * attempt: typically registering a new definition for the same bean name while
026 * {@link DefaultListableBeanFactory#isAllowBeanDefinitionOverriding()} is {@code false}.
027 *
028 * @author Juergen Hoeller
029 * @since 5.1
030 * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
031 * @see DefaultListableBeanFactory#registerBeanDefinition
032 */
033@SuppressWarnings("serial")
034public class BeanDefinitionOverrideException extends BeanDefinitionStoreException {
035
036        private final BeanDefinition beanDefinition;
037
038        private final BeanDefinition existingDefinition;
039
040
041        /**
042         * Create a new BeanDefinitionOverrideException for the given new and existing definition.
043         * @param beanName the name of the bean
044         * @param beanDefinition the newly registered bean definition
045         * @param existingDefinition the existing bean definition for the same name
046         */
047        public BeanDefinitionOverrideException(
048                        String beanName, BeanDefinition beanDefinition, BeanDefinition existingDefinition) {
049
050                super(beanDefinition.getResourceDescription(), beanName,
051                                "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
052                                "': There is already [" + existingDefinition + "] bound.");
053                this.beanDefinition = beanDefinition;
054                this.existingDefinition = existingDefinition;
055        }
056
057
058        /**
059         * Return the description of the resource that the bean definition came from.
060         */
061        @Override
062        @NonNull
063        public String getResourceDescription() {
064                return String.valueOf(super.getResourceDescription());
065        }
066
067        /**
068         * Return the name of the bean.
069         */
070        @Override
071        @NonNull
072        public String getBeanName() {
073                return String.valueOf(super.getBeanName());
074        }
075
076        /**
077         * Return the newly registered bean definition.
078         * @see #getBeanName()
079         */
080        public BeanDefinition getBeanDefinition() {
081                return this.beanDefinition;
082        }
083
084        /**
085         * Return the existing bean definition for the same name.
086         * @see #getBeanName()
087         */
088        public BeanDefinition getExistingDefinition() {
089                return this.existingDefinition;
090        }
091
092}