View Javadoc
1   /*
2    * Copyright 2002-2013 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.beans.factory.annotation;
18  
19  import org.junit.Test;
20  
21  import org.springframework.beans.factory.config.BeanDefinitionHolder;
22  import org.springframework.beans.factory.config.DependencyDescriptor;
23  import org.springframework.beans.factory.support.AutowireCandidateResolver;
24  import org.springframework.beans.factory.support.BeanDefinitionReader;
25  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
26  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
27  import org.springframework.core.io.Resource;
28  
29  import static org.junit.Assert.*;
30  import static org.springframework.tests.TestResourceUtils.*;
31  
32  /**
33   * Unit tests for {@link CustomAutowireConfigurer}.
34   *
35   * @author Mark Fisher
36   * @author Juergen Hoeller
37   * @author Chris Beams
38   */
39  public final class CustomAutowireConfigurerTests {
40  
41  	private static final Resource CONTEXT = qualifiedResource(CustomAutowireConfigurerTests.class, "context.xml");
42  
43  	@Test
44  	public void testCustomResolver() {
45  		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
46  		BeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
47  		reader.loadBeanDefinitions(CONTEXT);
48  		CustomAutowireConfigurer cac = new CustomAutowireConfigurer();
49  		CustomResolver customResolver = new CustomResolver();
50  		bf.setAutowireCandidateResolver(customResolver);
51  		cac.postProcessBeanFactory(bf);
52  		TestBean testBean = (TestBean) bf.getBean("testBean");
53  		assertEquals("#1!", testBean.getName());
54  	}
55  
56  
57  	public static class TestBean {
58  
59  		private String name;
60  
61  		public TestBean(String name) {
62  			this.name = name;
63  		}
64  
65  		public String getName() {
66  			return this.name;
67  		}
68  	}
69  
70  
71  	public static class CustomResolver implements AutowireCandidateResolver {
72  
73  		@Override
74  		public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
75  			if (!bdHolder.getBeanDefinition().isAutowireCandidate()) {
76  				return false;
77  			}
78  			if (!bdHolder.getBeanName().matches("[a-z-]+")) {
79  				return false;
80  			}
81  			if (bdHolder.getBeanDefinition().getAttribute("priority").equals("1")) {
82  				return true;
83  			}
84  			return false;
85  		}
86  
87  		@Override
88  		public Object getSuggestedValue(DependencyDescriptor descriptor) {
89  			return null;
90  		}
91  
92  		@Override
93  		public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, String beanName) {
94  			return null;
95  		}
96  	}
97  
98  }