|
84 | 84 | * {@code @Configuration} classes are candidates for component scanning (typically using |
85 | 85 | * Spring XML's {@code <context:component-scan/>} element) and therefore may also take |
86 | 86 | * advantage of {@link Autowired @Autowired}/{@link javax.inject.Inject @Inject} |
87 | | - * at the field and method level (but not at the constructor level). |
| 87 | + * like any regular {@code @Component}. In particular, if a single constructor is present |
| 88 | + * autowiring semantics will be applied transparently: |
| 89 | + * |
| 90 | + * <pre class="code"> |
| 91 | + * @Configuration |
| 92 | + * public class AppConfig { |
| 93 | + * private final SomeBean someBean; |
| 94 | + * |
| 95 | + * public AppConfig(SomeBean someBean) { |
| 96 | + * this.someBean = someBean; |
| 97 | + * } |
| 98 | + * |
| 99 | + * // @Bean definition using "SomeBean" |
| 100 | + * |
| 101 | + * }</pre> |
| 102 | + * |
88 | 103 | * <p>{@code @Configuration} classes may not only be bootstrapped using |
89 | 104 | * component scanning, but may also themselves <em>configure</em> component scanning using |
90 | 105 | * the {@link ComponentScan @ComponentScan} annotation: |
|
104 | 119 | * |
105 | 120 | * Externalized values may be looked up by injecting the Spring |
106 | 121 | * {@link org.springframework.core.env.Environment} into a {@code @Configuration} |
107 | | - * class using the {@code @Autowired} or the {@code @Inject} annotation: |
| 122 | + * class the usual (e.g. using the {@code @Autowired} annotation): |
108 | 123 | * |
109 | 124 | * <pre class="code"> |
110 | 125 | * @Configuration |
111 | 126 | * public class AppConfig { |
112 | 127 | * |
113 | | - * @Inject Environment env; |
| 128 | + * @Autowired Environment env; |
114 | 129 | * |
115 | 130 | * @Bean |
116 | 131 | * public MyBean myBean() { |
|
175 | 190 | * <p>{@code @Configuration} classes may be composed using the {@link Import @Import} annotation, |
176 | 191 | * not unlike the way that {@code <import>} works in Spring XML. Because |
177 | 192 | * {@code @Configuration} objects are managed as Spring beans within the container, |
178 | | - * imported configurations may be injected using {@code @Autowired} or {@code @Inject}: |
| 193 | + * imported configurations may be injected the usual way (e.g. via constructor injection): |
179 | 194 | * |
180 | 195 | * <pre class="code"> |
181 | 196 | * @Configuration |
|
191 | 206 | * @Import(DatabaseConfig.class) |
192 | 207 | * public class AppConfig { |
193 | 208 | * |
194 | | - * @Inject DatabaseConfig dataConfig; |
| 209 | + * private final DatabaseConfig dataConfig; |
| 210 | + * |
| 211 | + * public AppConfig(DatabaseConfig dataConfig) { |
| 212 | + * this.dataConfig = dataConfig; |
| 213 | + * } |
195 | 214 | * |
196 | 215 | * @Bean |
197 | 216 | * public MyBean myBean() { |
|
240 | 259 | * As mentioned above, {@code @Configuration} classes may be declared as regular Spring |
241 | 260 | * {@code <bean>} definitions within Spring XML files. It is also possible to |
242 | 261 | * import Spring XML configuration files into {@code @Configuration} classes using |
243 | | - * the {@link ImportResource @ImportResource} annotation. Bean definitions imported from XML can be |
244 | | - * injected using {@code @Autowired} or {@code @Inject}: |
| 262 | + * the {@link ImportResource @ImportResource} annotation. Bean definitions imported from |
| 263 | + * XML can be injected the usual way (e.g. using the {@code Inject} annotation): |
245 | 264 | * |
246 | 265 | * <pre class="code"> |
247 | 266 | * @Configuration |
|
340 | 359 | * <ul> |
341 | 360 | * <li>@Configuration classes must be non-final |
342 | 361 | * <li>@Configuration classes must be non-local (may not be declared within a method) |
343 | | - * <li>@Configuration classes must have a default/no-arg constructor and may not use |
344 | | - * {@link Autowired @Autowired} constructor parameters. Any nested configuration classes |
345 | | - * must be {@code static}. |
| 362 | + * <li>Any nested configuration classes must be {@code static}. |
346 | 363 | * </ul> |
347 | 364 | * |
348 | 365 | * @author Rod Johnson |
|
0 commit comments