|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* Copyright 2002-2016 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -27,12 +27,14 @@ import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Indicates that a class declares one or more {@link Bean @Bean} methods and may be processed |
|
|
|
* Indicates that a class declares one or more {@link Bean @Bean} methods and |
|
|
|
* by the Spring container to generate bean definitions and service requests for those |
|
|
|
* may be processed by the Spring container to generate bean definitions and |
|
|
|
* beans at runtime, for example: |
|
|
|
* service requests for those beans at runtime, for example: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* public class AppConfig { |
|
|
|
* public class AppConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
* public MyBean myBean() { |
|
|
|
* public MyBean myBean() { |
|
|
|
* // instantiate, configure and return bean ...
|
|
|
|
* // instantiate, configure and return bean ...
|
|
|
|
@ -40,25 +42,28 @@ import org.springframework.stereotype.Component; |
|
|
|
* }</pre> |
|
|
|
* }</pre> |
|
|
|
* |
|
|
|
* |
|
|
|
* <h2>Bootstrapping {@code @Configuration} classes</h2> |
|
|
|
* <h2>Bootstrapping {@code @Configuration} classes</h2> |
|
|
|
|
|
|
|
* |
|
|
|
* <h3>Via {@code AnnotationConfigApplicationContext}</h3> |
|
|
|
* <h3>Via {@code AnnotationConfigApplicationContext}</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* {@code @Configuration} classes are typically bootstrapped using either |
|
|
|
* {@code @Configuration} classes are typically bootstrapped using either |
|
|
|
* {@link AnnotationConfigApplicationContext} or its web-capable variant, |
|
|
|
* {@link AnnotationConfigApplicationContext} or its web-capable variant, |
|
|
|
* {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext |
|
|
|
* {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext |
|
|
|
* AnnotationConfigWebApplicationContext}. |
|
|
|
* AnnotationConfigWebApplicationContext}. A simple example with the former follows: |
|
|
|
* A simple example with the former follows: |
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* AnnotationConfigApplicationContext ctx = |
|
|
|
* AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
* new AnnotationConfigApplicationContext(); |
|
|
|
|
|
|
|
* ctx.register(AppConfig.class); |
|
|
|
* ctx.register(AppConfig.class); |
|
|
|
* ctx.refresh(); |
|
|
|
* ctx.refresh(); |
|
|
|
* MyBean myBean = ctx.getBean(MyBean.class); |
|
|
|
* MyBean myBean = ctx.getBean(MyBean.class); |
|
|
|
* // use myBean ...</pre>
|
|
|
|
* // use myBean ...
|
|
|
|
|
|
|
|
* </pre> |
|
|
|
* |
|
|
|
* |
|
|
|
* See {@link AnnotationConfigApplicationContext} Javadoc for further details and see |
|
|
|
* See {@link AnnotationConfigApplicationContext} Javadoc for further details and see |
|
|
|
* {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext |
|
|
|
* {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext |
|
|
|
* AnnotationConfigWebApplicationContext} for {@code web.xml} configuration instructions. |
|
|
|
* AnnotationConfigWebApplicationContext} for {@code web.xml} configuration instructions. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>Via Spring {@code <beans>} XML</h3> |
|
|
|
* <h3>Via Spring {@code <beans>} XML</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* <p>As an alternative to registering {@code @Configuration} classes directly against an |
|
|
|
* <p>As an alternative to registering {@code @Configuration} classes directly against an |
|
|
|
* {@code AnnotationConfigApplicationContext}, {@code @Configuration} classes may be |
|
|
|
* {@code AnnotationConfigApplicationContext}, {@code @Configuration} classes may be |
|
|
|
* declared as normal {@code <bean>} definitions within Spring XML files: |
|
|
|
* declared as normal {@code <bean>} definitions within Spring XML files: |
|
|
|
@ -74,6 +79,7 @@ import org.springframework.stereotype.Component; |
|
|
|
* post processors that facilitate handling {@code @Configuration} classes. |
|
|
|
* post processors that facilitate handling {@code @Configuration} classes. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>Via component scanning</h3> |
|
|
|
* <h3>Via component scanning</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* <p>{@code @Configuration} is meta-annotated with {@link Component @Component}, therefore |
|
|
|
* <p>{@code @Configuration} is meta-annotated with {@link Component @Component}, therefore |
|
|
|
* {@code @Configuration} classes are candidates for component scanning (typically using |
|
|
|
* {@code @Configuration} classes are candidates for component scanning (typically using |
|
|
|
* Spring XML's {@code <context:component-scan/>} element) and therefore may also take |
|
|
|
* Spring XML's {@code <context:component-scan/>} element) and therefore may also take |
|
|
|
@ -82,6 +88,7 @@ import org.springframework.stereotype.Component; |
|
|
|
* <p>{@code @Configuration} classes may not only be bootstrapped using |
|
|
|
* <p>{@code @Configuration} classes may not only be bootstrapped using |
|
|
|
* component scanning, but may also themselves <em>configure</em> component scanning using |
|
|
|
* component scanning, but may also themselves <em>configure</em> component scanning using |
|
|
|
* the {@link ComponentScan @ComponentScan} annotation: |
|
|
|
* the {@link ComponentScan @ComponentScan} annotation: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* @ComponentScan("com.acme.app.services") |
|
|
|
* @ComponentScan("com.acme.app.services") |
|
|
|
@ -89,18 +96,20 @@ import org.springframework.stereotype.Component; |
|
|
|
* // various @Bean definitions ...
|
|
|
|
* // various @Bean definitions ...
|
|
|
|
* }</pre> |
|
|
|
* }</pre> |
|
|
|
* |
|
|
|
* |
|
|
|
* See {@link ComponentScan @ComponentScan} Javadoc for details. |
|
|
|
* See the {@link ComponentScan @ComponentScan} javadoc for details. |
|
|
|
* |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* <h2>Working with externalized values</h2> |
|
|
|
* <h2>Working with externalized values</h2> |
|
|
|
|
|
|
|
* |
|
|
|
* <h3>Using the {@code Environment} API</h3> |
|
|
|
* <h3>Using the {@code Environment} API</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* Externalized values may be looked up by injecting the Spring |
|
|
|
* Externalized values may be looked up by injecting the Spring |
|
|
|
* {@link org.springframework.core.env.Environment Environment} into a |
|
|
|
* {@link org.springframework.core.env.Environment} into a {@code @Configuration} |
|
|
|
* {@code @Configuration} class using the {@code @Autowired} or the {@code @Inject} |
|
|
|
* class using the {@code @Autowired} or the {@code @Inject} annotation: |
|
|
|
* annotation: |
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* public class AppConfig { |
|
|
|
* public class AppConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Inject Environment env; |
|
|
|
* @Inject Environment env; |
|
|
|
* |
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
@ -115,10 +124,12 @@ import org.springframework.stereotype.Component; |
|
|
|
* source" objects, and {@code @Configuration} classes may contribute property sources to |
|
|
|
* source" objects, and {@code @Configuration} classes may contribute property sources to |
|
|
|
* the {@code Environment} object using |
|
|
|
* the {@code Environment} object using |
|
|
|
* the {@link org.springframework.core.env.PropertySources @PropertySources} annotation: |
|
|
|
* the {@link org.springframework.core.env.PropertySources @PropertySources} annotation: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* @PropertySource("classpath:/com/acme/app.properties") |
|
|
|
* @PropertySource("classpath:/com/acme/app.properties") |
|
|
|
* public class AppConfig { |
|
|
|
* public class AppConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Inject Environment env; |
|
|
|
* @Inject Environment env; |
|
|
|
* |
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
@ -131,12 +142,15 @@ import org.springframework.stereotype.Component; |
|
|
|
* and {@link PropertySource @PropertySource} Javadoc for further details. |
|
|
|
* and {@link PropertySource @PropertySource} Javadoc for further details. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>Using the {@code @Value} annotation</h3> |
|
|
|
* <h3>Using the {@code @Value} annotation</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* Externalized values may be 'wired into' {@code @Configuration} classes using |
|
|
|
* Externalized values may be 'wired into' {@code @Configuration} classes using |
|
|
|
* the {@link Value @Value} annotation: |
|
|
|
* the {@link Value @Value} annotation: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* @PropertySource("classpath:/com/acme/app.properties") |
|
|
|
* @PropertySource("classpath:/com/acme/app.properties") |
|
|
|
* public class AppConfig { |
|
|
|
* public class AppConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Value("${bean.name}") String beanName; |
|
|
|
* @Value("${bean.name}") String beanName; |
|
|
|
* |
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
@ -155,14 +169,18 @@ import org.springframework.stereotype.Component; |
|
|
|
* {@code PropertySourcesPlaceholderConfigurer}. |
|
|
|
* {@code PropertySourcesPlaceholderConfigurer}. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h2>Composing {@code @Configuration} classes</h2> |
|
|
|
* <h2>Composing {@code @Configuration} classes</h2> |
|
|
|
|
|
|
|
* |
|
|
|
* <h3>With the {@code @Import} annotation</h3> |
|
|
|
* <h3>With the {@code @Import} annotation</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* <p>{@code @Configuration} classes may be composed using the {@link Import @Import} annotation, |
|
|
|
* <p>{@code @Configuration} classes may be composed using the {@link Import @Import} annotation, |
|
|
|
* not unlike the way that {@code <import>} works in Spring XML. Because |
|
|
|
* not unlike the way that {@code <import>} works in Spring XML. Because |
|
|
|
* {@code @Configuration} objects are managed as Spring beans within the container, |
|
|
|
* {@code @Configuration} objects are managed as Spring beans within the container, |
|
|
|
* imported configurations may be injected using {@code @Autowired} or {@code @Inject}: |
|
|
|
* imported configurations may be injected using {@code @Autowired} or {@code @Inject}: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* public class DatabaseConfig { |
|
|
|
* public class DatabaseConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
* public DataSource dataSource() { |
|
|
|
* public DataSource dataSource() { |
|
|
|
* // instantiate, configure and return DataSource
|
|
|
|
* // instantiate, configure and return DataSource
|
|
|
|
@ -172,6 +190,7 @@ import org.springframework.stereotype.Component; |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* @Import(DatabaseConfig.class) |
|
|
|
* @Import(DatabaseConfig.class) |
|
|
|
* public class AppConfig { |
|
|
|
* public class AppConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Inject DatabaseConfig dataConfig; |
|
|
|
* @Inject DatabaseConfig dataConfig; |
|
|
|
* |
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
@ -188,13 +207,15 @@ import org.springframework.stereotype.Component; |
|
|
|
* new AnnotationConfigApplicationContext(AppConfig.class);</pre> |
|
|
|
* new AnnotationConfigApplicationContext(AppConfig.class);</pre> |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>With the {@code @Profile} annotation</h3> |
|
|
|
* <h3>With the {@code @Profile} annotation</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* {@code @Configuration} classes may be marked with the {@link Profile @Profile} annotation to |
|
|
|
* {@code @Configuration} classes may be marked with the {@link Profile @Profile} annotation to |
|
|
|
* indicate they should be processed only if a given profile or profiles are |
|
|
|
* indicate they should be processed only if a given profile or profiles are <em>active</em>: |
|
|
|
* <em>active</em>: |
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Profile("embedded") |
|
|
|
* @Profile("embedded") |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* public class EmbeddedDatabaseConfig { |
|
|
|
* public class EmbeddedDatabaseConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
* public DataSource dataSource() { |
|
|
|
* public DataSource dataSource() { |
|
|
|
* // instantiate, configure and return embedded DataSource
|
|
|
|
* // instantiate, configure and return embedded DataSource
|
|
|
|
@ -204,25 +225,29 @@ import org.springframework.stereotype.Component; |
|
|
|
* @Profile("production") |
|
|
|
* @Profile("production") |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* public class ProductionDatabaseConfig { |
|
|
|
* public class ProductionDatabaseConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
* public DataSource dataSource() { |
|
|
|
* public DataSource dataSource() { |
|
|
|
* // instantiate, configure and return production DataSource
|
|
|
|
* // instantiate, configure and return production DataSource
|
|
|
|
* } |
|
|
|
* } |
|
|
|
* }</pre> |
|
|
|
* }</pre> |
|
|
|
* |
|
|
|
* |
|
|
|
* See {@link Profile @Profile} and {@link org.springframework.core.env.Environment Environment} |
|
|
|
* See the {@link Profile @Profile} and {@link org.springframework.core.env.Environment} |
|
|
|
* Javadoc for further details. |
|
|
|
* javadocs for further details. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>With Spring XML using the {@code @ImportResource} annotation</h3> |
|
|
|
* <h3>With Spring XML using the {@code @ImportResource} annotation</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* As mentioned above, {@code @Configuration} classes may be declared as regular Spring |
|
|
|
* As mentioned above, {@code @Configuration} classes may be declared as regular Spring |
|
|
|
* {@code <bean>} definitions within Spring XML files. It is also possible to |
|
|
|
* {@code <bean>} definitions within Spring XML files. It is also possible to |
|
|
|
* import Spring XML configuration files into {@code @Configuration} classes using |
|
|
|
* import Spring XML configuration files into {@code @Configuration} classes using |
|
|
|
* the {@link ImportResource @ImportResource} annotation. Bean definitions imported from XML can be |
|
|
|
* the {@link ImportResource @ImportResource} annotation. Bean definitions imported from XML can be |
|
|
|
* injected using {@code @Autowired} or {@code @Inject}: |
|
|
|
* injected using {@code @Autowired} or {@code @Inject}: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* @ImportResource("classpath:/com/acme/database-config.xml") |
|
|
|
* @ImportResource("classpath:/com/acme/database-config.xml") |
|
|
|
* public class AppConfig { |
|
|
|
* public class AppConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Inject DataSource dataSource; // from XML
|
|
|
|
* @Inject DataSource dataSource; // from XML
|
|
|
|
* |
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
@ -233,10 +258,13 @@ import org.springframework.stereotype.Component; |
|
|
|
* }</pre> |
|
|
|
* }</pre> |
|
|
|
* |
|
|
|
* |
|
|
|
* <h3>With nested {@code @Configuration} classes</h3> |
|
|
|
* <h3>With nested {@code @Configuration} classes</h3> |
|
|
|
|
|
|
|
* |
|
|
|
* {@code @Configuration} classes may be nested within one another as follows: |
|
|
|
* {@code @Configuration} classes may be nested within one another as follows: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @Configuration |
|
|
|
* @Configuration |
|
|
|
* public class AppConfig { |
|
|
|
* public class AppConfig { |
|
|
|
|
|
|
|
* |
|
|
|
* @Inject DataSource dataSource; |
|
|
|
* @Inject DataSource dataSource; |
|
|
|
* |
|
|
|
* |
|
|
|
* @Bean |
|
|
|
* @Bean |
|
|
|
@ -264,6 +292,7 @@ import org.springframework.stereotype.Component; |
|
|
|
* enclosing {@code @Configuration} class. |
|
|
|
* enclosing {@code @Configuration} class. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h2>Configuring lazy initialization</h2> |
|
|
|
* <h2>Configuring lazy initialization</h2> |
|
|
|
|
|
|
|
* |
|
|
|
* <p>By default, {@code @Bean} methods will be <em>eagerly instantiated</em> at container |
|
|
|
* <p>By default, {@code @Bean} methods will be <em>eagerly instantiated</em> at container |
|
|
|
* bootstrap time. To avoid this, {@code @Configuration} may be used in conjunction with |
|
|
|
* bootstrap time. To avoid this, {@code @Configuration} may be used in conjunction with |
|
|
|
* the {@link Lazy @Lazy} annotation to indicate that all {@code @Bean} methods declared within |
|
|
|
* the {@link Lazy @Lazy} annotation to indicate that all {@code @Bean} methods declared within |
|
|
|
@ -271,9 +300,11 @@ import org.springframework.stereotype.Component; |
|
|
|
* individual {@code @Bean} methods as well. |
|
|
|
* individual {@code @Bean} methods as well. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h2>Testing support for {@code @Configuration} classes</h2> |
|
|
|
* <h2>Testing support for {@code @Configuration} classes</h2> |
|
|
|
|
|
|
|
* |
|
|
|
* The Spring <em>TestContext framework</em> available in the {@code spring-test} module |
|
|
|
* The Spring <em>TestContext framework</em> available in the {@code spring-test} module |
|
|
|
* provides the {@code @ContextConfiguration} annotation, which as of Spring 3.1 can |
|
|
|
* provides the {@code @ContextConfiguration} annotation, which as of Spring 3.1 can |
|
|
|
* accept an array of {@code @Configuration} {@code Class} objects: |
|
|
|
* accept an array of {@code @Configuration} {@code Class} objects: |
|
|
|
|
|
|
|
* |
|
|
|
* <pre class="code"> |
|
|
|
* <pre class="code"> |
|
|
|
* @RunWith(SpringJUnit4ClassRunner.class) |
|
|
|
* @RunWith(SpringJUnit4ClassRunner.class) |
|
|
|
* @ContextConfiguration(classes={AppConfig.class, DatabaseConfig.class}) |
|
|
|
* @ContextConfiguration(classes={AppConfig.class, DatabaseConfig.class}) |
|
|
|
@ -292,6 +323,7 @@ import org.springframework.stereotype.Component; |
|
|
|
* See TestContext framework reference documentation for details. |
|
|
|
* See TestContext framework reference documentation for details. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h2>Enabling built-in Spring features using {@code @Enable} annotations</h2> |
|
|
|
* <h2>Enabling built-in Spring features using {@code @Enable} annotations</h2> |
|
|
|
|
|
|
|
* |
|
|
|
* Spring features such as asynchronous method execution, scheduled task execution, |
|
|
|
* Spring features such as asynchronous method execution, scheduled task execution, |
|
|
|
* annotation driven transaction management, and even Spring MVC can be enabled and |
|
|
|
* annotation driven transaction management, and even Spring MVC can be enabled and |
|
|
|
* configured from {@code @Configuration} |
|
|
|
* configured from {@code @Configuration} |
|
|
|
@ -304,6 +336,7 @@ import org.springframework.stereotype.Component; |
|
|
|
* for details. |
|
|
|
* for details. |
|
|
|
* |
|
|
|
* |
|
|
|
* <h2>Constraints when authoring {@code @Configuration} classes</h2> |
|
|
|
* <h2>Constraints when authoring {@code @Configuration} classes</h2> |
|
|
|
|
|
|
|
* |
|
|
|
* <ul> |
|
|
|
* <ul> |
|
|
|
* <li>@Configuration classes must be non-final |
|
|
|
* <li>@Configuration classes must be non-final |
|
|
|
* <li>@Configuration classes must be non-local (may not be declared within a method) |
|
|
|
* <li>@Configuration classes must be non-local (may not be declared within a method) |
|
|
|
|