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.util.ClassUtils;
021import org.springframework.util.StringUtils;
022
023/**
024 * Exception thrown when a bean depends on other beans or simple properties
025 * that were not specified in the bean factory definition, although
026 * dependency checking was enabled.
027 *
028 * @author Rod Johnson
029 * @author Juergen Hoeller
030 * @since 03.09.2003
031 */
032@SuppressWarnings("serial")
033public class UnsatisfiedDependencyException extends BeanCreationException {
034
035        private InjectionPoint injectionPoint;
036
037
038        /**
039         * Create a new UnsatisfiedDependencyException.
040         * @param resourceDescription description of the resource that the bean definition came from
041         * @param beanName the name of the bean requested
042         * @param propertyName the name of the bean property that couldn't be satisfied
043         * @param msg the detail message
044         */
045        public UnsatisfiedDependencyException(
046                        String resourceDescription, String beanName, String propertyName, String msg) {
047
048                super(resourceDescription, beanName,
049                                "Unsatisfied dependency expressed through bean property '" + propertyName + "'" +
050                                (StringUtils.hasLength(msg) ? ": " + msg : ""));
051        }
052
053        /**
054         * Create a new UnsatisfiedDependencyException.
055         * @param resourceDescription description of the resource that the bean definition came from
056         * @param beanName the name of the bean requested
057         * @param propertyName the name of the bean property that couldn't be satisfied
058         * @param ex the bean creation exception that indicated the unsatisfied dependency
059         */
060        public UnsatisfiedDependencyException(
061                        String resourceDescription, String beanName, String propertyName, BeansException ex) {
062
063                this(resourceDescription, beanName, propertyName, "");
064                initCause(ex);
065        }
066
067        /**
068         * Create a new UnsatisfiedDependencyException.
069         * @param resourceDescription description of the resource that the bean definition came from
070         * @param beanName the name of the bean requested
071         * @param injectionPoint the injection point (field or method/constructor parameter)
072         * @param msg the detail message
073         * @since 4.3
074         */
075        public UnsatisfiedDependencyException(
076                        String resourceDescription, String beanName, InjectionPoint injectionPoint, String msg) {
077
078                super(resourceDescription, beanName,
079                                "Unsatisfied dependency expressed through " + injectionPoint +
080                                (StringUtils.hasLength(msg) ? ": " + msg : ""));
081                this.injectionPoint = injectionPoint;
082        }
083
084        /**
085         * Create a new UnsatisfiedDependencyException.
086         * @param resourceDescription description of the resource that the bean definition came from
087         * @param beanName the name of the bean requested
088         * @param injectionPoint the injection point (field or method/constructor parameter)
089         * @param ex the bean creation exception that indicated the unsatisfied dependency
090         * @since 4.3
091         */
092        public UnsatisfiedDependencyException(
093                        String resourceDescription, String beanName, InjectionPoint injectionPoint, BeansException ex) {
094
095                this(resourceDescription, beanName, injectionPoint, "");
096                initCause(ex);
097        }
098
099        /**
100         * Create a new UnsatisfiedDependencyException.
101         * @param resourceDescription description of the resource that the bean definition came from
102         * @param beanName the name of the bean requested
103         * @param ctorArgIndex the index of the constructor argument that couldn't be satisfied
104         * @param ctorArgType the type of the constructor argument that couldn't be satisfied
105         * @param msg the detail message
106         * @deprecated in favor of {@link #UnsatisfiedDependencyException(String, String, InjectionPoint, String)}
107         */
108        @Deprecated
109        public UnsatisfiedDependencyException(
110                        String resourceDescription, String beanName, int ctorArgIndex, Class<?> ctorArgType, String msg) {
111
112                super(resourceDescription, beanName,
113                                "Unsatisfied dependency expressed through constructor argument with index " +
114                                ctorArgIndex + " of type [" + ClassUtils.getQualifiedName(ctorArgType) + "]" +
115                                (msg != null ? ": " + msg : ""));
116        }
117
118        /**
119         * Create a new UnsatisfiedDependencyException.
120         * @param resourceDescription description of the resource that the bean definition came from
121         * @param beanName the name of the bean requested
122         * @param ctorArgIndex the index of the constructor argument that couldn't be satisfied
123         * @param ctorArgType the type of the constructor argument that couldn't be satisfied
124         * @param ex the bean creation exception that indicated the unsatisfied dependency
125         * @deprecated in favor of {@link #UnsatisfiedDependencyException(String, String, InjectionPoint, BeansException)}
126         */
127        @Deprecated
128        public UnsatisfiedDependencyException(
129                        String resourceDescription, String beanName, int ctorArgIndex, Class<?> ctorArgType, BeansException ex) {
130
131                this(resourceDescription, beanName, ctorArgIndex, ctorArgType, (ex != null ? ex.getMessage() : ""));
132                initCause(ex);
133        }
134
135
136        /**
137         * Return the injection point (field or method/constructor parameter), if known.
138         * @since 4.3
139         */
140        public InjectionPoint getInjectionPoint() {
141                return this.injectionPoint;
142        }
143
144}