org.springframework.context.annotation
@annotation.Target({ElementType.TYPE})
@annotation.Retention(RetentionPolicy.RUNTIME)
@annotation.Documented
@org.springframework.stereotype.Component
public interface Configuration
extends annotation.Annotation
Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime, for example:

  @Configuration
   public class AppConfig {
       @Bean
       public MyBean myBean() {
           // instantiate, configure and return bean ...
       }
   }
   

Bootstrapping @Configuration classes
Via AnnotationConfigApplicationContext
@Configuration classes are typically bootstrapped using either AnnotationConfigApplicationContext or its web-capable variant, AnnotationConfigWebApplicationContext. A simple example with the former follows:

   AnnotationConfigApplicationContext ctx =
       new AnnotationConfigApplicationContext();
   ctx.register(AppConfig.class);
   ctx.refresh();
   MyBean myBean = ctx.getBean(MyBean.class);
   // use myBean ...

See AnnotationConfigApplicationContext Javadoc for further details and see AnnotationConfigWebApplicationContext for web.xml configuration instructions.
Via Spring XML
As an alternative to registering @Configuration classes directly against an AnnotationConfigApplicationContext, @Configuration classes may be declared as normal definitions within Spring XML files:

   <beans>
     <context:annotation-config/>
     <bean class="com.acme.AppConfig"/>
  </beans>

In the example above, is required in order to enable ConfigurationClassPostProcessor and other annotation-related post processors that facilitate handling @Configuration classes.
Via component scanning
@Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning (typically using Spring XML's element) and therefore may also take advantage of @Autowired/@Inject at the field and method level (but not at the constructor level).
@Configuration classes may not only be bootstrapped using component scanning, but may also themselves configure component scanning using the @ComponentScan annotation:

   @Configuration
   @ComponentScan("com.acme.app.services")
   public class AppConfig {
       // various @Bean definitions ...
   }

See @ComponentScan Javadoc for details.
Working with externalized values
Using the Environment API
Externalized values may be looked up by injecting the Spring Environment into a @Configuration class using the @Autowired or the @Inject annotation:

   @Configuration
   public class AppConfig {
       @Inject Environment env;

       @Bean
       public MyBean myBean() {
           MyBean myBean = new MyBean();
           myBean.setName(env.getProperty("bean.name"));
           return myBean;
       }
   }

Properties resolved through the Environment reside in one or more "property source" objects, and @Configuration classes may contribute property sources to the Environment object using the @PropertySources annotation:

   @Configuration
   @PropertySource("classpath:/com/acme/app.properties")
   public class AppConfig {
       @Inject Environment env;

       @Bean
       public MyBean myBean() {
           return new MyBean(env.getProperty("bean.name"));
       }
   }

See Environment and @PropertySource Javadoc for further details.
Using the @Value annotation
Externalized values may be 'wired into' @Configuration classes using the @Value annotation:

   @Configuration
   @PropertySource("classpath:/com/acme/app.properties")
   public class AppConfig {
       @Value("${bean.name}") String beanName;

       @Bean
       public MyBean myBean() {
           return new MyBean(beanName);
       }
   }

This approach is most useful when using Spring's PropertySourcesPlaceholderConfigurer, usually enabled via XML with . See the section below on composing @Configuration classes with Spring XML using @ImportResource, see @Value Javadoc, and see @Bean Javadoc for details on working with BeanFactoryPostProcessor types such as PropertySourcesPlaceholderConfigurer.
Composing @Configuration classes
With the @Import annotation
@Configuration classes may be composed using the @Import annotation, not unlike the way that works in Spring XML. Because @Configuration objects are managed as Spring beans within the container, imported configurations may be injected using @Autowired or @Inject:

  @Configuration
   public class DatabaseConfig {
       @Bean
       public DataSource dataSource() {
           // instantiate, configure and return DataSource
       }
   }

   @Configuration
   @Import(DatabaseConfig.class)
   public class AppConfig {
       @Inject DatabaseConfig dataConfig;

       @Bean
       public MyBean myBean() {
           // reference the dataSource() bean method
           return new MyBean(dataConfig.dataSource());
       }
   }

Now both AppConfig and the imported DatabaseConfig can be bootstrapped by registering only AppConfig against the Spring context:
new AnnotationConfigApplicationContext(AppConfig.class);
With the @Profile annotation

@Configuration classes may be marked with the @Profile annotation to indicate they should be processed only if a given profile or profiles are active:
   @Profile("embedded")
   @Configuration
   public class EmbeddedDatabaseConfig {
       @Bean
       public DataSource dataSource() {
           // instantiate, configure and return embedded DataSource
       }
   }

   @Profile("production")
   @Configuration
   public class ProductionDatabaseConfig {
       @Bean
       public DataSource dataSource() {
           // instantiate, configure and return production DataSource
       }
   }

See @Profile and Environment Javadoc for further details.
With Spring XML using the @ImportResource annotation
As mentioned above, @Configuration classes may be declared as regular Spring definitions within Spring XML files. It is also possible to import Spring XML configuration files into @Configuration classes using the @ImportResource annotation. Bean definitions imported from XML can be injected using @Autowired or @Inject:

  @Configuration
   @ImportResource("classpath:/com/acme/database-config.xml")
   public class AppConfig {
       @Inject DataSource dataSource; // from XML

       @Bean
       public MyBean myBean() {
           // inject the XML-defined dataSource bean
           return new MyBean(this.dataSource);
       }
   }

With nested @Configuration classes

@Configuration classes may be nested within one another as follows:
   @Configuration
   public class AppConfig {
       @Inject DataSource dataSource;

       @Bean
       public MyBean myBean() {
           return new MyBean(dataSource);
       }

       @Configuration
       static class DatabaseConfig {
           @Bean
           DataSource dataSource() {
               return new EmbeddedDatabaseBuilder().build();
           }
       }
   }
   

When bootstrapping such an arrangement, only AppConfig need be registered against the application context. By virtue of being a nested @Configuration class, DatabaseConfig will be registered automatically. This avoids the need to use an @Import annotation when the relationship between AppConfig DatabaseConfig is already implicitly clear.
Note also that nested @Configuration classes can be used to good effect with the @Profile annotation to provide two options of the same bean to the enclosing @Configuration class.
Configuring lazy initialization
By default, @Bean methods will be eagerly instantiated at container bootstrap time. To avoid this, @Configuration may be used in conjunction with the @Lazy annotation to indicate that all @Bean methods declared within the class are by default lazily initialized. Note that @Lazy may be used on individual @Bean methods as well.
Testing support for @Configuration classes
The Spring TestContext framework available in the spring-test module provides the @ContextConfiguration annotation, which as of Spring 3.1 can accept an array of @Configuration Class objects:

   @RunWith(SpringJUnit4ClassRunner.class)
   @ContextConfiguration(classes={AppConfig.class, DatabaseConfig.class})
   public class MyTests {

       @Autowired MyBean myBean;

       @Autowired DataSource dataSource;

       @Test
       public void test() {
           // assertions against myBean ...
       }
   }

最新文章

  1. PHP中遍历XML之SimpleXML
  2. ASP.NET Core 中文文档 第三章 原理(17)为你的服务器选择合适版本的.NET框架
  3. MyBatis之代理开发模式
  4. 免费电子书:Azure Web Apps开发者入门
  5. ZedBoard 引脚约束参考
  6. Mongodb For C# &quot;Query&quot; 对象常用的方法
  7. [git/svn]Git和SVN差异
  8. 【MySQL】关于MySQL错误日志信息的收集
  9. Unity3d 要点板书
  10. windows下架设SVN服务器并设置开机启动
  11. LINQ to XML编程之编程基础
  12. 关于oracle表名区分大小写的问题
  13. python-tqdm进度条
  14. Mtlab:抛物型方程的交替方向隐格式(ADI)
  15. Jenkins的初级应用(2)-Invoke Phing targets
  16. zabbix 与 nginx (五)
  17. 线程误区-join,wait(里边还是调用的wait)
  18. 【iCore4 双核心板_ARM】例程五:SYSTICK定时器 实验——定时点亮LED
  19. Linux防火墙操作
  20. WPF如何设置Image.Source为资源图片

热门文章

  1. web网页中使用vlc插件播放相机rtsp流视频
  2. 网站设置404页面 --nginx
  3. Linux启动界面切换:图形界面-字符界面(转)
  4. ASP.NET MVC和EF集成AngularJS开发
  5. [译]C#编码约定
  6. ubuntu14.04环境下spyder的安装
  7. 耿丹CS16-2班第六次作业汇总
  8. 我的js函数库(持续更新)
  9. python之路十二
  10. svn 合并分支