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 org.springframework.beans.BeansException; 020import org.springframework.core.ResolvableType; 021import org.springframework.util.ClassUtils; 022import org.springframework.util.StringUtils; 023 024/** 025 * Exception thrown when a {@code BeanFactory} is asked for a bean instance for which it 026 * cannot find a definition. This may point to a non-existing bean, a non-unique bean, 027 * or a manually registered singleton instance without an associated bean definition. 028 * 029 * @author Rod Johnson 030 * @author Juergen Hoeller 031 * @author Stephane Nicoll 032 * @see BeanFactory#getBean(String) 033 * @see BeanFactory#getBean(Class) 034 * @see NoUniqueBeanDefinitionException 035 */ 036@SuppressWarnings("serial") 037public class NoSuchBeanDefinitionException extends BeansException { 038 039 private String beanName; 040 041 private ResolvableType resolvableType; 042 043 044 /** 045 * Create a new {@code NoSuchBeanDefinitionException}. 046 * @param name the name of the missing bean 047 */ 048 public NoSuchBeanDefinitionException(String name) { 049 super("No bean named '" + name + "' available"); 050 this.beanName = name; 051 } 052 053 /** 054 * Create a new {@code NoSuchBeanDefinitionException}. 055 * @param name the name of the missing bean 056 * @param message detailed message describing the problem 057 */ 058 public NoSuchBeanDefinitionException(String name, String message) { 059 super("No bean named '" + name + "' available: " + message); 060 this.beanName = name; 061 } 062 063 /** 064 * Create a new {@code NoSuchBeanDefinitionException}. 065 * @param type required type of the missing bean 066 */ 067 public NoSuchBeanDefinitionException(Class<?> type) { 068 this(ResolvableType.forClass(type)); 069 } 070 071 /** 072 * Create a new {@code NoSuchBeanDefinitionException}. 073 * @param type required type of the missing bean 074 * @param message detailed message describing the problem 075 */ 076 public NoSuchBeanDefinitionException(Class<?> type, String message) { 077 this(ResolvableType.forClass(type), message); 078 } 079 080 /** 081 * Create a new {@code NoSuchBeanDefinitionException}. 082 * @param type full type declaration of the missing bean 083 * @since 4.3.4 084 */ 085 public NoSuchBeanDefinitionException(ResolvableType type) { 086 super("No qualifying bean of type '" + type + "' available"); 087 this.resolvableType = type; 088 } 089 090 /** 091 * Create a new {@code NoSuchBeanDefinitionException}. 092 * @param type full type declaration of the missing bean 093 * @param message detailed message describing the problem 094 * @since 4.3.4 095 */ 096 public NoSuchBeanDefinitionException(ResolvableType type, String message) { 097 super("No qualifying bean of type '" + type + "' available: " + message); 098 this.resolvableType = type; 099 } 100 101 /** 102 * Create a new {@code NoSuchBeanDefinitionException}. 103 * @param type required type of the missing bean 104 * @param dependencyDescription a description of the originating dependency 105 * @param message detailed message describing the problem 106 * @deprecated as of 4.3.4, in favor of {@link #NoSuchBeanDefinitionException(ResolvableType, String)} 107 */ 108 @Deprecated 109 public NoSuchBeanDefinitionException(Class<?> type, String dependencyDescription, String message) { 110 super("No qualifying bean" + (!StringUtils.hasLength(dependencyDescription) ? 111 " of type '" + ClassUtils.getQualifiedName(type) + "'" : "") + " found for dependency" + 112 (StringUtils.hasLength(dependencyDescription) ? " [" + dependencyDescription + "]" : "") + 113 ": " + message); 114 this.resolvableType = ResolvableType.forClass(type); 115 } 116 117 118 /** 119 * Return the name of the missing bean, if it was a lookup <em>by name</em> that failed. 120 */ 121 public String getBeanName() { 122 return this.beanName; 123 } 124 125 /** 126 * Return the required type of the missing bean, if it was a lookup <em>by type</em> 127 * that failed. 128 */ 129 public Class<?> getBeanType() { 130 return (this.resolvableType != null ? this.resolvableType.resolve() : null); 131 } 132 133 /** 134 * Return the required {@link ResolvableType} of the missing bean, if it was a lookup 135 * <em>by type</em> that failed. 136 * @since 4.3.4 137 */ 138 public ResolvableType getResolvableType() { 139 return this.resolvableType; 140 } 141 142 /** 143 * Return the number of beans found when only one matching bean was expected. 144 * For a regular NoSuchBeanDefinitionException, this will always be 0. 145 * @see NoUniqueBeanDefinitionException 146 */ 147 public int getNumberOfBeansFound() { 148 return 0; 149 } 150 151}