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.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 public static final String VALUE_KEY = "value"; 036 037 private final String typeName; 038 039 040 /** 041 * Construct a qualifier to match against an annotation of the 042 * given type. 043 * @param type the annotation type 044 */ 045 public AutowireCandidateQualifier(Class<?> type) { 046 this(type.getName()); 047 } 048 049 /** 050 * Construct a qualifier to match against an annotation of the 051 * given type name. 052 * <p>The type name may match the fully-qualified class name of 053 * the annotation or the short class name (without the package). 054 * @param typeName the name of the annotation type 055 */ 056 public AutowireCandidateQualifier(String typeName) { 057 Assert.notNull(typeName, "Type name must not be null"); 058 this.typeName = typeName; 059 } 060 061 /** 062 * Construct a qualifier to match against an annotation of the 063 * given type whose {@code value} attribute also matches 064 * the specified value. 065 * @param type the annotation type 066 * @param value the annotation value to match 067 */ 068 public AutowireCandidateQualifier(Class<?> type, Object value) { 069 this(type.getName(), value); 070 } 071 072 /** 073 * Construct a qualifier to match against an annotation of the 074 * given type name whose {@code value} attribute also matches 075 * the specified value. 076 * <p>The type name may match the fully-qualified class name of 077 * the annotation or the short class name (without the package). 078 * @param typeName the name of the annotation type 079 * @param value the annotation value to match 080 */ 081 public AutowireCandidateQualifier(String typeName, Object value) { 082 Assert.notNull(typeName, "Type name must not be null"); 083 this.typeName = typeName; 084 setAttribute(VALUE_KEY, value); 085 } 086 087 088 /** 089 * Retrieve the type name. This value will be the same as the 090 * type name provided to the constructor or the fully-qualified 091 * class name if a Class instance was provided to the constructor. 092 */ 093 public String getTypeName() { 094 return this.typeName; 095 } 096 097}