CustomAutowireConfigurerTests.java 2.8 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2008 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory.annotation;

19
import static org.junit.Assert.assertEquals;
C
Chris Beams 已提交
20
import static test.util.TestResourceUtils.qualifiedResource;
A
Arjen Poutsma 已提交
21

22
import org.junit.Test;
A
Arjen Poutsma 已提交
23 24 25 26 27 28
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.support.AutowireCandidateResolver;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
C
Chris Beams 已提交
29
import org.springframework.core.io.Resource;
A
Arjen Poutsma 已提交
30 31

/**
C
Chris Beams 已提交
32 33
 * Unit tests for {@link CustomAutowireConfigurer}.
 * 
A
Arjen Poutsma 已提交
34 35
 * @author Mark Fisher
 * @author Juergen Hoeller
36
 * @author Chris Beams
A
Arjen Poutsma 已提交
37
 */
C
Chris Beams 已提交
38
public final class CustomAutowireConfigurerTests {
A
Arjen Poutsma 已提交
39

C
Chris Beams 已提交
40
	private static final Resource CONTEXT = qualifiedResource(CustomAutowireConfigurerTests.class, "context.xml");
A
Arjen Poutsma 已提交
41

42
	@Test
A
Arjen Poutsma 已提交
43 44 45
	public void testCustomResolver() {
		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
		BeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
C
Chris Beams 已提交
46
		reader.loadBeanDefinitions(CONTEXT);
A
Arjen Poutsma 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		CustomAutowireConfigurer cac = new CustomAutowireConfigurer();
		CustomResolver customResolver = new CustomResolver();
		bf.setAutowireCandidateResolver(customResolver);
		cac.postProcessBeanFactory(bf);
		TestBean testBean = (TestBean) bf.getBean("testBean");
		assertEquals("#1!", testBean.getName());
	}


	public static class TestBean {

		private String name;

		public TestBean(String name) {
			this.name = name;
		}

		public String getName() {
			return this.name;
		}
	}


	public static class CustomResolver implements AutowireCandidateResolver {

		public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
			if (!bdHolder.getBeanDefinition().isAutowireCandidate()) {
				return false;
			}
			if (!bdHolder.getBeanName().matches("[a-z-]+")) {
				return false;
			}
			if (bdHolder.getBeanDefinition().getAttribute("priority").equals("1")) {
				return true;
			}
			return false;
		}
84 85 86 87

		public Object getSuggestedValue(DependencyDescriptor descriptor) {
			return null;
		}
A
Arjen Poutsma 已提交
88 89 90
	}

}