|
1 | 1 | /* |
2 | | - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
19 | 19 | import org.junit.Test; |
20 | 20 |
|
21 | 21 | import org.springframework.beans.factory.FactoryBean; |
| 22 | +import org.springframework.beans.factory.InitializingBean; |
22 | 23 | import org.springframework.beans.factory.annotation.Autowired; |
23 | 24 | import org.springframework.util.Assert; |
24 | 25 |
|
| 26 | +import static org.junit.Assert.*; |
| 27 | + |
25 | 28 | /** |
26 | 29 | * Tests cornering bug SPR-8514. |
27 | 30 | * |
28 | 31 | * @author Chris Beams |
| 32 | + * @author Juergen Hoeller |
29 | 33 | * @since 3.1 |
30 | 34 | */ |
31 | 35 | public class ConfigurationWithFactoryBeanAndAutowiringTests { |
@@ -77,138 +81,188 @@ public void withWildcardParameterizedFactoryBeanInterfaceAsReturnType() { |
77 | 81 | ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class); |
78 | 82 | ctx.refresh(); |
79 | 83 | } |
80 | | -} |
81 | | - |
82 | 84 |
|
83 | | -class DummyBean { |
84 | | -} |
| 85 | + @Test |
| 86 | + public void withFactoryBeanCallingBean() { |
| 87 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 88 | + ctx.register(AppConfig.class); |
| 89 | + ctx.register(FactoryBeanCallingConfig.class); |
| 90 | + ctx.refresh(); |
| 91 | + assertEquals("true", ctx.getBean("myString")); |
| 92 | + } |
85 | 93 |
|
86 | 94 |
|
87 | | -class MyFactoryBean implements FactoryBean<String> { |
88 | | - @Override |
89 | | - public String getObject() throws Exception { |
90 | | - return "foo"; |
91 | | - } |
92 | | - @Override |
93 | | - public Class<String> getObjectType() { |
94 | | - return String.class; |
95 | | - } |
96 | | - @Override |
97 | | - public boolean isSingleton() { |
98 | | - return true; |
| 95 | + static class DummyBean { |
99 | 96 | } |
100 | | -} |
101 | 97 |
|
102 | 98 |
|
103 | | -class MyParameterizedFactoryBean<T> implements FactoryBean<T> { |
| 99 | + static class MyFactoryBean implements FactoryBean<String>, InitializingBean { |
104 | 100 |
|
105 | | - private final T obj; |
| 101 | + private boolean initialized = false; |
106 | 102 |
|
107 | | - public MyParameterizedFactoryBean(T obj) { |
108 | | - this.obj = obj; |
109 | | - } |
| 103 | + @Override |
| 104 | + public void afterPropertiesSet() throws Exception { |
| 105 | + this.initialized = true; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String getObject() throws Exception { |
| 110 | + return "foo"; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Class<String> getObjectType() { |
| 115 | + return String.class; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean isSingleton() { |
| 120 | + return true; |
| 121 | + } |
110 | 122 |
|
111 | | - @Override |
112 | | - public T getObject() throws Exception { |
113 | | - return obj; |
| 123 | + public String getString() { |
| 124 | + return Boolean.toString(this.initialized); |
| 125 | + } |
114 | 126 | } |
115 | 127 |
|
116 | | - @Override |
117 | | - @SuppressWarnings("unchecked") |
118 | | - public Class<T> getObjectType() { |
119 | | - return (Class<T>)obj.getClass(); |
| 128 | + |
| 129 | + static class MyParameterizedFactoryBean<T> implements FactoryBean<T> { |
| 130 | + |
| 131 | + private final T obj; |
| 132 | + |
| 133 | + public MyParameterizedFactoryBean(T obj) { |
| 134 | + this.obj = obj; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public T getObject() throws Exception { |
| 139 | + return obj; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + @SuppressWarnings("unchecked") |
| 144 | + public Class<T> getObjectType() { |
| 145 | + return (Class<T>)obj.getClass(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public boolean isSingleton() { |
| 150 | + return true; |
| 151 | + } |
120 | 152 | } |
121 | 153 |
|
122 | | - @Override |
123 | | - public boolean isSingleton() { |
124 | | - return true; |
| 154 | + |
| 155 | + @Configuration |
| 156 | + static class AppConfig { |
| 157 | + |
| 158 | + @Bean |
| 159 | + public DummyBean dummyBean() { |
| 160 | + return new DummyBean(); |
| 161 | + } |
125 | 162 | } |
126 | | -} |
127 | 163 |
|
128 | 164 |
|
129 | | -@Configuration |
130 | | -class AppConfig { |
131 | | - @Bean |
132 | | - public DummyBean dummyBean() { |
133 | | - return new DummyBean(); |
| 165 | + @Configuration |
| 166 | + static class ConcreteFactoryBeanImplementationConfig { |
| 167 | + |
| 168 | + @Autowired |
| 169 | + private DummyBean dummyBean; |
| 170 | + |
| 171 | + @Bean |
| 172 | + public MyFactoryBean factoryBean() { |
| 173 | + Assert.notNull(dummyBean, "DummyBean was not injected."); |
| 174 | + return new MyFactoryBean(); |
| 175 | + } |
134 | 176 | } |
135 | | -} |
136 | 177 |
|
137 | 178 |
|
138 | | -@Configuration |
139 | | -class ConcreteFactoryBeanImplementationConfig { |
140 | | - @Autowired |
141 | | - private DummyBean dummyBean; |
| 179 | + @Configuration |
| 180 | + static class ParameterizedFactoryBeanImplementationConfig { |
| 181 | + |
| 182 | + @Autowired |
| 183 | + private DummyBean dummyBean; |
142 | 184 |
|
143 | | - @Bean |
144 | | - public MyFactoryBean factoryBean() { |
145 | | - Assert.notNull(dummyBean, "DummyBean was not injected."); |
146 | | - return new MyFactoryBean(); |
| 185 | + @Bean |
| 186 | + public MyParameterizedFactoryBean<String> factoryBean() { |
| 187 | + Assert.notNull(dummyBean, "DummyBean was not injected."); |
| 188 | + return new MyParameterizedFactoryBean<String>("whatev"); |
| 189 | + } |
147 | 190 | } |
148 | | -} |
149 | 191 |
|
150 | 192 |
|
151 | | -@Configuration |
152 | | -class ParameterizedFactoryBeanImplementationConfig { |
153 | | - @Autowired |
154 | | - private DummyBean dummyBean; |
| 193 | + @Configuration |
| 194 | + static class ParameterizedFactoryBeanInterfaceConfig { |
| 195 | + |
| 196 | + @Autowired |
| 197 | + private DummyBean dummyBean; |
155 | 198 |
|
156 | | - @Bean |
157 | | - public MyParameterizedFactoryBean<String> factoryBean() { |
158 | | - Assert.notNull(dummyBean, "DummyBean was not injected."); |
159 | | - return new MyParameterizedFactoryBean<String>("whatev"); |
| 199 | + @Bean |
| 200 | + public FactoryBean<String> factoryBean() { |
| 201 | + Assert.notNull(dummyBean, "DummyBean was not injected."); |
| 202 | + return new MyFactoryBean(); |
| 203 | + } |
160 | 204 | } |
161 | | -} |
162 | 205 |
|
163 | 206 |
|
164 | | -@Configuration |
165 | | -class ParameterizedFactoryBeanInterfaceConfig { |
166 | | - @Autowired |
167 | | - private DummyBean dummyBean; |
| 207 | + @Configuration |
| 208 | + static class NonPublicParameterizedFactoryBeanInterfaceConfig { |
| 209 | + |
| 210 | + @Autowired |
| 211 | + private DummyBean dummyBean; |
168 | 212 |
|
169 | | - @Bean |
170 | | - public FactoryBean<String> factoryBean() { |
171 | | - Assert.notNull(dummyBean, "DummyBean was not injected."); |
172 | | - return new MyFactoryBean(); |
| 213 | + @Bean |
| 214 | + FactoryBean<String> factoryBean() { |
| 215 | + Assert.notNull(dummyBean, "DummyBean was not injected."); |
| 216 | + return new MyFactoryBean(); |
| 217 | + } |
173 | 218 | } |
174 | | -} |
175 | 219 |
|
176 | 220 |
|
177 | | -@Configuration |
178 | | -class NonPublicParameterizedFactoryBeanInterfaceConfig { |
179 | | - @Autowired |
180 | | - private DummyBean dummyBean; |
| 221 | + @Configuration |
| 222 | + static class RawFactoryBeanInterfaceConfig { |
181 | 223 |
|
182 | | - @Bean |
183 | | - FactoryBean<String> factoryBean() { |
184 | | - Assert.notNull(dummyBean, "DummyBean was not injected."); |
185 | | - return new MyFactoryBean(); |
| 224 | + @Autowired |
| 225 | + private DummyBean dummyBean; |
| 226 | + |
| 227 | + @Bean |
| 228 | + @SuppressWarnings("rawtypes") |
| 229 | + public FactoryBean factoryBean() { |
| 230 | + Assert.notNull(dummyBean, "DummyBean was not injected."); |
| 231 | + return new MyFactoryBean(); |
| 232 | + } |
186 | 233 | } |
187 | | -} |
188 | 234 |
|
189 | 235 |
|
190 | | -@Configuration |
191 | | -class RawFactoryBeanInterfaceConfig { |
192 | | - @Autowired |
193 | | - private DummyBean dummyBean; |
| 236 | + @Configuration |
| 237 | + static class WildcardParameterizedFactoryBeanInterfaceConfig { |
194 | 238 |
|
195 | | - @Bean |
196 | | - @SuppressWarnings("rawtypes") |
197 | | - public FactoryBean factoryBean() { |
198 | | - Assert.notNull(dummyBean, "DummyBean was not injected."); |
199 | | - return new MyFactoryBean(); |
| 239 | + @Autowired |
| 240 | + private DummyBean dummyBean; |
| 241 | + |
| 242 | + @Bean |
| 243 | + public FactoryBean<?> factoryBean() { |
| 244 | + Assert.notNull(dummyBean, "DummyBean was not injected."); |
| 245 | + return new MyFactoryBean(); |
| 246 | + } |
200 | 247 | } |
201 | | -} |
202 | 248 |
|
203 | 249 |
|
204 | | -@Configuration |
205 | | -class WildcardParameterizedFactoryBeanInterfaceConfig { |
206 | | - @Autowired |
207 | | - private DummyBean dummyBean; |
| 250 | + @Configuration |
| 251 | + static class FactoryBeanCallingConfig { |
| 252 | + |
| 253 | + @Autowired |
| 254 | + private DummyBean dummyBean; |
208 | 255 |
|
209 | | - @Bean |
210 | | - public FactoryBean<?> factoryBean() { |
211 | | - Assert.notNull(dummyBean, "DummyBean was not injected."); |
212 | | - return new MyFactoryBean(); |
| 256 | + @Bean |
| 257 | + public MyFactoryBean factoryBean() { |
| 258 | + Assert.notNull(dummyBean, "DummyBean was not injected."); |
| 259 | + return new MyFactoryBean(); |
| 260 | + } |
| 261 | + |
| 262 | + @Bean |
| 263 | + public String myString() { |
| 264 | + return factoryBean().getString(); |
| 265 | + } |
213 | 266 | } |
| 267 | + |
214 | 268 | } |
0 commit comments