|
| 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.context.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.Documented; |
| 20 | +import java.lang.annotation.ElementType; |
| 21 | +import java.lang.annotation.Retention; |
| 22 | +import java.lang.annotation.RetentionPolicy; |
| 23 | +import java.lang.annotation.Target; |
| 24 | + |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.FactoryBean; |
| 29 | +import org.springframework.beans.factory.InitializingBean; |
| 30 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 31 | +import org.springframework.core.type.AnnotationMetadata; |
| 32 | +import org.springframework.util.Assert; |
| 33 | + |
| 34 | +import static org.junit.Assert.*; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Dave Syer |
| 38 | + */ |
| 39 | +public class Spr11202Tests { |
| 40 | + |
| 41 | + private AnnotationConfigApplicationContext context; |
| 42 | + |
| 43 | + @After |
| 44 | + public void close() { |
| 45 | + if (context != null) { |
| 46 | + context.close(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test // Fails |
| 51 | + public void testWithImporter() { |
| 52 | + context = new AnnotationConfigApplicationContext(Wrapper.class); |
| 53 | + assertEquals("foo", context.getBean("value")); |
| 54 | + } |
| 55 | + |
| 56 | + @Test // Passes |
| 57 | + public void testWithoutImporter() { |
| 58 | + context = new AnnotationConfigApplicationContext(Config.class); |
| 59 | + assertEquals("foo", context.getBean("value")); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + @Configuration |
| 64 | + @Import(Selector.class) |
| 65 | + protected static class Wrapper { |
| 66 | + } |
| 67 | + |
| 68 | + protected static class Selector implements ImportSelector { |
| 69 | + |
| 70 | + @Override |
| 71 | + public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
| 72 | + return new String[] {Config.class.getName()}; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Configuration |
| 77 | + protected static class Config { |
| 78 | + |
| 79 | + @Bean |
| 80 | + public FooFactoryBean foo() { |
| 81 | + return new FooFactoryBean(); |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + public String value() throws Exception { |
| 86 | + String name = foo().getObject().getName(); |
| 87 | + Assert.state(name != null, "Name cannot be null"); |
| 88 | + return name; |
| 89 | + } |
| 90 | + |
| 91 | + @Bean |
| 92 | + @Conditional(NoBarCondition.class) |
| 93 | + public String bar() throws Exception { |
| 94 | + return "bar"; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + protected static class NoBarCondition implements Condition { |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 102 | + if (context.getBeanFactory().getBeanNamesForAnnotation(Bar.class).length > 0) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Retention(RetentionPolicy.RUNTIME) |
| 110 | + @Documented |
| 111 | + @Target(ElementType.TYPE) |
| 112 | + protected static @interface Bar { |
| 113 | + } |
| 114 | + |
| 115 | + protected static class FooFactoryBean implements FactoryBean<Foo>, InitializingBean { |
| 116 | + |
| 117 | + private Foo foo = new Foo(); |
| 118 | + |
| 119 | + @Override |
| 120 | + public Foo getObject() throws Exception { |
| 121 | + return foo; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Class<?> getObjectType() { |
| 126 | + return Foo.class; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean isSingleton() { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void afterPropertiesSet() throws Exception { |
| 136 | + this.foo.name = "foo"; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + protected static class Foo { |
| 141 | + |
| 142 | + private String name; |
| 143 | + |
| 144 | + public String getName() { |
| 145 | + return name; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments