View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.context.annotation;
18  
19  import java.lang.annotation.Annotation;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.LinkedHashSet;
23  import java.util.List;
24  import java.util.Set;
25  import java.util.regex.Pattern;
26  
27  import org.springframework.beans.BeanUtils;
28  import org.springframework.beans.factory.config.BeanDefinitionHolder;
29  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
30  import org.springframework.beans.factory.support.BeanNameGenerator;
31  import org.springframework.context.ConfigurableApplicationContext;
32  import org.springframework.core.annotation.AnnotationAttributes;
33  import org.springframework.core.env.Environment;
34  import org.springframework.core.io.ResourceLoader;
35  import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter;
36  import org.springframework.core.type.filter.AnnotationTypeFilter;
37  import org.springframework.core.type.filter.AspectJTypeFilter;
38  import org.springframework.core.type.filter.AssignableTypeFilter;
39  import org.springframework.core.type.filter.RegexPatternTypeFilter;
40  import org.springframework.core.type.filter.TypeFilter;
41  import org.springframework.util.Assert;
42  import org.springframework.util.ClassUtils;
43  import org.springframework.util.StringUtils;
44  
45  /**
46   * Parser for the @{@link ComponentScan} annotation.
47   *
48   * @author Chris Beams
49   * @author Juergen Hoeller
50   * @since 3.1
51   * @see ClassPathBeanDefinitionScanner#scan(String...)
52   * @see ComponentScanBeanDefinitionParser
53   */
54  class ComponentScanAnnotationParser {
55  
56  	private final ResourceLoader resourceLoader;
57  
58  	private final Environment environment;
59  
60  	private final BeanDefinitionRegistry registry;
61  
62  	private final BeanNameGenerator beanNameGenerator;
63  
64  
65  	public ComponentScanAnnotationParser(ResourceLoader resourceLoader, Environment environment,
66  			BeanNameGenerator beanNameGenerator, BeanDefinitionRegistry registry) {
67  
68  		this.resourceLoader = resourceLoader;
69  		this.environment = environment;
70  		this.beanNameGenerator = beanNameGenerator;
71  		this.registry = registry;
72  	}
73  
74  
75  	public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
76  		ClassPathBeanDefinitionScanner scanner =
77  				new ClassPathBeanDefinitionScanner(this.registry, componentScan.getBoolean("useDefaultFilters"));
78  
79  		Assert.notNull(this.environment, "Environment must not be null");
80  		scanner.setEnvironment(this.environment);
81  
82  		Assert.notNull(this.resourceLoader, "ResourceLoader must not be null");
83  		scanner.setResourceLoader(this.resourceLoader);
84  
85  		Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
86  		boolean useInheritedGenerator = BeanNameGenerator.class.equals(generatorClass);
87  		scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
88  				BeanUtils.instantiateClass(generatorClass));
89  
90  		ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");
91  		if (scopedProxyMode != ScopedProxyMode.DEFAULT) {
92  			scanner.setScopedProxyMode(scopedProxyMode);
93  		}
94  		else {
95  			Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");
96  			scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));
97  		}
98  
99  		scanner.setResourcePattern(componentScan.getString("resourcePattern"));
100 
101 		for (AnnotationAttributes filter : componentScan.getAnnotationArray("includeFilters")) {
102 			for (TypeFilter typeFilter : typeFiltersFor(filter)) {
103 				scanner.addIncludeFilter(typeFilter);
104 			}
105 		}
106 		for (AnnotationAttributes filter : componentScan.getAnnotationArray("excludeFilters")) {
107 			for (TypeFilter typeFilter : typeFiltersFor(filter)) {
108 				scanner.addExcludeFilter(typeFilter);
109 			}
110 		}
111 
112 		boolean lazyInit = componentScan.getBoolean("lazyInit");
113 		if (lazyInit) {
114 			scanner.getBeanDefinitionDefaults().setLazyInit(true);
115 		}
116 
117 		Set<String> basePackages = new LinkedHashSet<String>();
118 		Set<String> specifiedPackages = new LinkedHashSet<String>();
119 		specifiedPackages.addAll(Arrays.asList(componentScan.getStringArray("value")));
120 		specifiedPackages.addAll(Arrays.asList(componentScan.getStringArray("basePackages")));
121 
122 		for (String pkg : specifiedPackages) {
123 			String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),
124 					ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
125 			basePackages.addAll(Arrays.asList(tokenized));
126 		}
127 		for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {
128 			basePackages.add(ClassUtils.getPackageName(clazz));
129 		}
130 		if (basePackages.isEmpty()) {
131 			basePackages.add(ClassUtils.getPackageName(declaringClass));
132 		}
133 
134 		scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
135 			@Override
136 			protected boolean matchClassName(String className) {
137 				return declaringClass.equals(className);
138 			}
139 		});
140 		return scanner.doScan(StringUtils.toStringArray(basePackages));
141 	}
142 
143 	private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
144 		List<TypeFilter> typeFilters = new ArrayList<TypeFilter>();
145 		FilterType filterType = filterAttributes.getEnum("type");
146 
147 		for (Class<?> filterClass : filterAttributes.getClassArray("value")) {
148 			switch (filterType) {
149 				case ANNOTATION:
150 					Assert.isAssignable(Annotation.class, filterClass,
151 							"An error occured while processing a @ComponentScan ANNOTATION type filter: ");
152 					@SuppressWarnings("unchecked")
153 					Class<Annotation> annotationType = (Class<Annotation>) filterClass;
154 					typeFilters.add(new AnnotationTypeFilter(annotationType));
155 					break;
156 				case ASSIGNABLE_TYPE:
157 					typeFilters.add(new AssignableTypeFilter(filterClass));
158 					break;
159 				case CUSTOM:
160 					Assert.isAssignable(TypeFilter.class, filterClass,
161 							"An error occured while processing a @ComponentScan CUSTOM type filter: ");
162 					typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
163 					break;
164 				default:
165 					throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType);
166 			}
167 		}
168 
169 		for (String expression : filterAttributes.getStringArray("pattern")) {
170 			switch (filterType) {
171 				case ASPECTJ:
172 					typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
173 					break;
174 				case REGEX:
175 					typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
176 					break;
177 				default:
178 					throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
179 			}
180 		}
181 
182 		return typeFilters;
183 	}
184 
185 }