001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.beans.factory.annotation;
018:
019: import java.lang.annotation.Annotation;
020: import java.util.Iterator;
021: import java.util.Set;
022:
023: import org.springframework.beans.BeansException;
024: import org.springframework.beans.factory.BeanClassLoaderAware;
025: import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
026: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
027: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
028: import org.springframework.core.Ordered;
029: import org.springframework.util.ClassUtils;
030:
031: /**
032: * A {@link org.springframework.beans.factory.config.BeanFactoryPostProcessor}
033: * implementation that allows for convenient registration of custom autowire
034: * qualifier types.
035: *
036: * <pre class="code">
037: * <bean id="customAutowireConfigurer" class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
038: * <property name="customQualifierTypes">
039: * <set>
040: * <value>mypackage.MyQualifier</value>
041: * </set>
042: * </property>
043: * </bean></pre>
044: *
045: * @author Mark Fisher
046: * @author Juergen Hoeller
047: * @since 2.5
048: * @see org.springframework.beans.factory.annotation.Qualifier
049: */
050: public class CustomAutowireConfigurer implements
051: BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered {
052:
053: private int order = Ordered.LOWEST_PRECEDENCE; // default: same as non-Ordered
054:
055: private Set customQualifierTypes;
056:
057: private ClassLoader beanClassLoader = ClassUtils
058: .getDefaultClassLoader();
059:
060: public void setOrder(int order) {
061: this .order = order;
062: }
063:
064: public int getOrder() {
065: return this .order;
066: }
067:
068: public void setBeanClassLoader(ClassLoader beanClassLoader) {
069: this .beanClassLoader = beanClassLoader;
070: }
071:
072: /**
073: * Register custom qualifier annotation types to be considered
074: * when autowiring beans. Each element of the provided set may
075: * be either a Class instance or a String representation of the
076: * fully-qualified class name of the custom annotation.
077: * <p>Note that any annotation that is itself annotated with Spring's
078: * {@link org.springframework.beans.factory.annotation.Qualifier}
079: * does not require explicit registration.
080: * @param customQualifierTypes the custom types to register
081: */
082: public void setCustomQualifierTypes(Set customQualifierTypes) {
083: this .customQualifierTypes = customQualifierTypes;
084: }
085:
086: public void postProcessBeanFactory(
087: ConfigurableListableBeanFactory beanFactory)
088: throws BeansException {
089: if (this .customQualifierTypes != null) {
090: if (!(beanFactory instanceof DefaultListableBeanFactory)) {
091: throw new IllegalStateException(
092: "CustomAutowireConfigurer needs to operate on a DefaultListableBeanFactory");
093: }
094: DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
095: if (!(dlbf.getAutowireCandidateResolver() instanceof QualifierAnnotationAutowireCandidateResolver)) {
096: throw new IllegalStateException(
097: "CustomAutowireConfigurer needs to operate on a QualifierAnnotationAutowireCandidateResolver");
098: }
099: QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver) dlbf
100: .getAutowireCandidateResolver();
101: for (Iterator it = this .customQualifierTypes.iterator(); it
102: .hasNext();) {
103: Class customType = null;
104: Object value = it.next();
105: if (value instanceof Class) {
106: customType = (Class) value;
107: } else if (value instanceof String) {
108: String className = (String) value;
109: customType = ClassUtils.resolveClassName(className,
110: this .beanClassLoader);
111: } else {
112: throw new IllegalArgumentException(
113: "Invalid value ["
114: + value
115: + "] for custom qualifier type: needs to be Class or String.");
116: }
117: if (!Annotation.class.isAssignableFrom(customType)) {
118: throw new IllegalArgumentException(
119: "Qualifier type [" + customType.getName()
120: + "] needs to be annotation type");
121: }
122: resolver.addQualifierType(customType);
123: }
124: }
125: }
126:
127: }
|