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