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.lang.Nullable;
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        @Nullable
036        private final InjectionPoint injectionPoint;
037
038
039        /**
040         * Create a new UnsatisfiedDependencyException.
041         * @param resourceDescription description of the resource that the bean definition came from
042         * @param beanName the name of the bean requested
043         * @param propertyName the name of the bean property that couldn't be satisfied
044         * @param msg the detail message
045         */
046        public UnsatisfiedDependencyException(
047                        @Nullable String resourceDescription, @Nullable String beanName, String propertyName, String msg) {
048
049                super(resourceDescription, beanName,
050                                "Unsatisfied dependency expressed through bean property '" + propertyName + "'" +
051                                (StringUtils.hasLength(msg) ? ": " + msg : ""));
052                this.injectionPoint = null;
053        }
054
055        /**
056         * Create a new UnsatisfiedDependencyException.
057         * @param resourceDescription description of the resource that the bean definition came from
058         * @param beanName the name of the bean requested
059         * @param propertyName the name of the bean property that couldn't be satisfied
060         * @param ex the bean creation exception that indicated the unsatisfied dependency
061         */
062        public UnsatisfiedDependencyException(
063                        @Nullable String resourceDescription, @Nullable String beanName, String propertyName, BeansException ex) {
064
065                this(resourceDescription, beanName, propertyName, "");
066                initCause(ex);
067        }
068
069        /**
070         * Create a new UnsatisfiedDependencyException.
071         * @param resourceDescription description of the resource that the bean definition came from
072         * @param beanName the name of the bean requested
073         * @param injectionPoint the injection point (field or method/constructor parameter)
074         * @param msg the detail message
075         * @since 4.3
076         */
077        public UnsatisfiedDependencyException(
078                        @Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, String msg) {
079
080                super(resourceDescription, beanName,
081                                "Unsatisfied dependency expressed through " + injectionPoint +
082                                (StringUtils.hasLength(msg) ? ": " + msg : ""));
083                this.injectionPoint = injectionPoint;
084        }
085
086        /**
087         * Create a new UnsatisfiedDependencyException.
088         * @param resourceDescription description of the resource that the bean definition came from
089         * @param beanName the name of the bean requested
090         * @param injectionPoint the injection point (field or method/constructor parameter)
091         * @param ex the bean creation exception that indicated the unsatisfied dependency
092         * @since 4.3
093         */
094        public UnsatisfiedDependencyException(
095                        @Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, BeansException ex) {
096
097                this(resourceDescription, beanName, injectionPoint, "");
098                initCause(ex);
099        }
100
101
102        /**
103         * Return the injection point (field or method/constructor parameter), if known.
104         * @since 4.3
105         */
106        @Nullable
107        public InjectionPoint getInjectionPoint() {
108                return this.injectionPoint;
109        }
110
111}