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.support;
018
019import org.springframework.beans.BeanMetadataAttributeAccessor;
020import org.springframework.util.Assert;
021
022/**
023 * Qualifier for resolving autowire candidates. A bean definition that
024 * includes one or more such qualifiers enables fine-grained matching
025 * against annotations on a field or parameter to be autowired.
026 *
027 * @author Mark Fisher
028 * @author Juergen Hoeller
029 * @since 2.5
030 * @see org.springframework.beans.factory.annotation.Qualifier
031 */
032@SuppressWarnings("serial")
033public class AutowireCandidateQualifier extends BeanMetadataAttributeAccessor {
034
035        /**
036         * The name of the key used to store the value.
037         */
038        public static final String VALUE_KEY = "value";
039
040        private final String typeName;
041
042
043        /**
044         * Construct a qualifier to match against an annotation of the
045         * given type.
046         * @param type the annotation type
047         */
048        public AutowireCandidateQualifier(Class<?> type) {
049                this(type.getName());
050        }
051
052        /**
053         * Construct a qualifier to match against an annotation of the
054         * given type name.
055         * <p>The type name may match the fully-qualified class name of
056         * the annotation or the short class name (without the package).
057         * @param typeName the name of the annotation type
058         */
059        public AutowireCandidateQualifier(String typeName) {
060                Assert.notNull(typeName, "Type name must not be null");
061                this.typeName = typeName;
062        }
063
064        /**
065         * Construct a qualifier to match against an annotation of the
066         * given type whose {@code value} attribute also matches
067         * the specified value.
068         * @param type the annotation type
069         * @param value the annotation value to match
070         */
071        public AutowireCandidateQualifier(Class<?> type, Object value) {
072                this(type.getName(), value);
073        }
074
075        /**
076         * Construct a qualifier to match against an annotation of the
077         * given type name whose {@code value} attribute also matches
078         * the specified value.
079         * <p>The type name may match the fully-qualified class name of
080         * the annotation or the short class name (without the package).
081         * @param typeName the name of the annotation type
082         * @param value the annotation value to match
083         */
084        public AutowireCandidateQualifier(String typeName, Object value) {
085                Assert.notNull(typeName, "Type name must not be null");
086                this.typeName = typeName;
087                setAttribute(VALUE_KEY, value);
088        }
089
090
091        /**
092         * Retrieve the type name. This value will be the same as the
093         * type name provided to the constructor or the fully-qualified
094         * class name if a Class instance was provided to the constructor.
095         */
096        public String getTypeName() {
097                return this.typeName;
098        }
099
100}