001/*
002 * Copyright 2002-2017 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.wiring;
018
019import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
020import org.springframework.lang.Nullable;
021import org.springframework.util.Assert;
022
023/**
024 * Holder for bean wiring metadata information about a particular class. Used in
025 * conjunction with the {@link org.springframework.beans.factory.annotation.Configurable}
026 * annotation and the AspectJ {@code AnnotationBeanConfigurerAspect}.
027 *
028 * @author Rod Johnson
029 * @author Juergen Hoeller
030 * @since 2.0
031 * @see BeanWiringInfoResolver
032 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory
033 * @see org.springframework.beans.factory.annotation.Configurable
034 */
035public class BeanWiringInfo {
036
037        /**
038         * Constant that indicates autowiring bean properties by name.
039         * @see #BeanWiringInfo(int, boolean)
040         * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
041         */
042        public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
043
044        /**
045         * Constant that indicates autowiring bean properties by type.
046         * @see #BeanWiringInfo(int, boolean)
047         * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
048         */
049        public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
050
051
052        @Nullable
053        private String beanName;
054
055        private boolean isDefaultBeanName = false;
056
057        private int autowireMode = AutowireCapableBeanFactory.AUTOWIRE_NO;
058
059        private boolean dependencyCheck = false;
060
061
062        /**
063         * Create a default BeanWiringInfo that suggests plain initialization of
064         * factory and post-processor callbacks that the bean class may expect.
065         */
066        public BeanWiringInfo() {
067        }
068
069        /**
070         * Create a new BeanWiringInfo that points to the given bean name.
071         * @param beanName the name of the bean definition to take the property values from
072         * @throws IllegalArgumentException if the supplied beanName is {@code null},
073         * is empty, or consists wholly of whitespace
074         */
075        public BeanWiringInfo(String beanName) {
076                this(beanName, false);
077        }
078
079        /**
080         * Create a new BeanWiringInfo that points to the given bean name.
081         * @param beanName the name of the bean definition to take the property values from
082         * @param isDefaultBeanName whether the given bean name is a suggested
083         * default bean name, not necessarily matching an actual bean definition
084         * @throws IllegalArgumentException if the supplied beanName is {@code null},
085         * is empty, or consists wholly of whitespace
086         */
087        public BeanWiringInfo(String beanName, boolean isDefaultBeanName) {
088                Assert.hasText(beanName, "'beanName' must not be empty");
089                this.beanName = beanName;
090                this.isDefaultBeanName = isDefaultBeanName;
091        }
092
093        /**
094         * Create a new BeanWiringInfo that indicates autowiring.
095         * @param autowireMode one of the constants {@link #AUTOWIRE_BY_NAME} /
096         * {@link #AUTOWIRE_BY_TYPE}
097         * @param dependencyCheck whether to perform a dependency check for object
098         * references in the bean instance (after autowiring)
099         * @throws IllegalArgumentException if the supplied {@code autowireMode}
100         * is not one of the allowed values
101         * @see #AUTOWIRE_BY_NAME
102         * @see #AUTOWIRE_BY_TYPE
103         */
104        public BeanWiringInfo(int autowireMode, boolean dependencyCheck) {
105                if (autowireMode != AUTOWIRE_BY_NAME && autowireMode != AUTOWIRE_BY_TYPE) {
106                        throw new IllegalArgumentException("Only constants AUTOWIRE_BY_NAME and AUTOWIRE_BY_TYPE supported");
107                }
108                this.autowireMode = autowireMode;
109                this.dependencyCheck = dependencyCheck;
110        }
111
112
113        /**
114         * Return whether this BeanWiringInfo indicates autowiring.
115         */
116        public boolean indicatesAutowiring() {
117                return (this.beanName == null);
118        }
119
120        /**
121         * Return the specific bean name that this BeanWiringInfo points to, if any.
122         */
123        @Nullable
124        public String getBeanName() {
125                return this.beanName;
126        }
127
128        /**
129         * Return whether the specific bean name is a suggested default bean name,
130         * not necessarily matching an actual bean definition in the factory.
131         */
132        public boolean isDefaultBeanName() {
133                return this.isDefaultBeanName;
134        }
135
136        /**
137         * Return one of the constants {@link #AUTOWIRE_BY_NAME} /
138         * {@link #AUTOWIRE_BY_TYPE}, if autowiring is indicated.
139         */
140        public int getAutowireMode() {
141                return this.autowireMode;
142        }
143
144        /**
145         * Return whether to perform a dependency check for object references
146         * in the bean instance (after autowiring).
147         */
148        public boolean getDependencyCheck() {
149                return this.dependencyCheck;
150        }
151
152}