001/*
002 * Copyright 2002-2016 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;
018
019import java.util.Arrays;
020import java.util.Collection;
021
022import org.springframework.util.StringUtils;
023
024/**
025 * Exception thrown when a {@code BeanFactory} is asked for a bean instance for which
026 * multiple matching candidates have been found when only one matching bean was expected.
027 *
028 * @author Juergen Hoeller
029 * @since 3.2.1
030 * @see BeanFactory#getBean(Class)
031 */
032@SuppressWarnings("serial")
033public class NoUniqueBeanDefinitionException extends NoSuchBeanDefinitionException {
034
035        private int numberOfBeansFound;
036
037        private Collection<String> beanNamesFound;
038
039
040        /**
041         * Create a new {@code NoUniqueBeanDefinitionException}.
042         * @param type required type of the non-unique bean
043         * @param numberOfBeansFound the number of matching beans
044         * @param message detailed message describing the problem
045         */
046        public NoUniqueBeanDefinitionException(Class<?> type, int numberOfBeansFound, String message) {
047                super(type, message);
048                this.numberOfBeansFound = numberOfBeansFound;
049        }
050
051        /**
052         * Create a new {@code NoUniqueBeanDefinitionException}.
053         * @param type required type of the non-unique bean
054         * @param beanNamesFound the names of all matching beans (as a Collection)
055         */
056        public NoUniqueBeanDefinitionException(Class<?> type, Collection<String> beanNamesFound) {
057                this(type, beanNamesFound.size(), "expected single matching bean but found " + beanNamesFound.size() + ": " +
058                                StringUtils.collectionToCommaDelimitedString(beanNamesFound));
059                this.beanNamesFound = beanNamesFound;
060        }
061
062        /**
063         * Create a new {@code NoUniqueBeanDefinitionException}.
064         * @param type required type of the non-unique bean
065         * @param beanNamesFound the names of all matching beans (as an array)
066         */
067        public NoUniqueBeanDefinitionException(Class<?> type, String... beanNamesFound) {
068                this(type, Arrays.asList(beanNamesFound));
069        }
070
071
072        /**
073         * Return the number of beans found when only one matching bean was expected.
074         * For a NoUniqueBeanDefinitionException, this will usually be higher than 1.
075         * @see #getBeanType()
076         */
077        @Override
078        public int getNumberOfBeansFound() {
079                return this.numberOfBeansFound;
080        }
081
082        /**
083         * Return the names of all beans found when only one matching bean was expected.
084         * Note that this may be {@code null} if not specified at construction time.
085         * @since 4.3
086         * @see #getBeanType()
087         */
088        public Collection<String> getBeanNamesFound() {
089                return this.beanNamesFound;
090        }
091
092}