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 ...
       }
   }

spring configuration 注解的更多相关文章

  1. Spring Configuration注解使用

    @Configuration是spring.xml的注解版. @ComponentScan是<context:component-scan base-package="com.cosh ...

  2. Spring @Configuration注解

    1 该注解的用途 这个注解表示这个类可以作为spring ioc容器bean的来源,其本质上它是对xml文件中创建bean的一种替换.有了这个注释,Spring framework就能在需要的时候构造 ...

  3. Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value

    Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...

  4. [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  5. Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

    SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...

  6. Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  7. spring和springmvc中,Configuration注解Bean重复加载

    问题:bean重复加载1.如下代码所示,开启Configuration注解,实现Bean代码注入,发现bean重复加载 @Configuration public class EhCacheConfi ...

  8. 品Spring:注解之王@Configuration和它的一众“小弟们”

    其实对Spring的了解达到一定程度后,你就会发现,无论是使用Spring框架开发的应用,还是Spring框架本身的开发都是围绕着注解构建起来的. 空口无凭,那就说个最普通的例子吧. 在Spring中 ...

  9. Spring源码之@Configuration注解解析

    1.前言 ​ Spring注解开发中,我们只需求要类上加上@Configuration注解,然后在类中的方法上面加上@Bean注解即可完成Spring Bean组件的注册.相较于之前的xml配置文件定 ...

随机推荐

  1. js访问php,返回数组时的注意事项

    用ajax访问php脚本返回值是数组的时候,php端需要使用json_encode()函数进行转码成json字符串,js端需要用JSON.parse()来吧json字符串转换成数组或对象. 直接返回会 ...

  2. 如何提高账户密码存储的安全性——PasswordSalt的使用

    使用 Salt + Hash 将密码加密后再存储进数据库 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的 ...

  3. Download Excel file with Angular

    源码连接(编写中) 用Angular下载后台返回的Excel文件,用Blob实现,引用FileSaver.js 后台C#代码: [WebMethod] public static byte[] Cal ...

  4. 用Canvas实现动画效果

    1.清除Canvas的内容 clearRect(x,y,width,height)函数用于清除图像中指定矩形区域的内容 <!doctype html> <html> <h ...

  5. Mac下Jenkins+SVN+Xcode构建持续

    1 安装Jenkins Jenkins是基于Java开发的一种持续集成工具.所以呢,要使用Jenkins必须使用先安装JDK. JDK安装 JDK 下载地址 jdk 1.8.png 安装JDK的过程略 ...

  6. 【学习笔记】load-on-startup Servlet

    创建Servlet实例有两个时机:用户请求之时和应用启动之时.应用启动之时创建的通常用于某些后台服务的Servlet.配置load-on-startup的Servlet有两种方式: 在web.xml文 ...

  7. The current identity (NT AUTHORITY/NETWORK SERVICE)

    IIS错误提示: The current identity (NT AUTHORITY/NETWORK SERVICE) does not have write access to 'C:/WINDO ...

  8. html5第二天

    哎..以为自己能每天坚持写呢.前面8天一直在D3的东西..都没有时间研究html5.草草的翻了一下HTML5和CSS3权威指南.对整个页面设计有了一个大概的把握,但是让自己做肯定还会有写问题.暂时ht ...

  9. 给Excel2013添加WebADI的Oracle加载项

    大家都知道,在Excel2013的加载项中是找不到WebADI的加载项的,EBS貌似有一个补丁,这里讲手动设置的步骤: 打开一个下载的WebADI的模板: 依次打开菜单: 文件>选项>自定 ...

  10. Android获取位置信息的方法总结

    1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...