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