下面是一个典型的spring配置文件(application-config.xml):

  1. <beans>
  2. <bean id="orderService" class="com.acme.OrderService"/>
  3. <constructor-arg ref="orderRepository"/>
  4. </bean>
  5. <bean id="orderRepository" class="com.acme.OrderRepository"/>
  6. <constructor-arg ref="dataSource"/>
  7. </bean>
  8. </beans>

然后你就可以像这样来使用是bean了:

Java代码

  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml");
  2. OrderService orderService = (OrderService) ctx.getBean("orderService");

现在Spring JavaConfiguration这个项目提供了一种通过java代码来装配bean的方案:

  1. @Configuration
  2. public class ApplicationConfig {
  3. public @Bean OrderService orderService() {
  4. return new OrderService(orderRepository());
  5. }
  6. public @Bean OrderRepository orderRepository() {
  7. return new OrderRepository(dataSource());
  8. }
  9. public @Bean DataSource dataSource() {
  10. // instantiate and return an new DataSource …
  11. }
  12. }

然后你就可以像这样来使用是bean了:

  1. JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class);
  2. OrderService orderService = ctx.getBean(OrderService.class);

这么做有什么好处呢?

1.使用纯java代码,不在需要xml

2.在配置中也可享受OO带来的好处

3.类型安全对重构也能提供良好的支持

4.依旧能享受到所有springIoC容器提供的功能

Spring @Configuration的更多相关文章

  1. Spring Configuration Check Unmapped Spring configuration files found

    Spring Configuration Check Unmapped Spring configuration files found 项目中有xml文件,但没有被用IntelliJ 导入现有工程时 ...

  2. IntelliJ 15 unmapped spring configuration files found

    IntelliJ Spring Configuration Check 用IntelliJ 导入现有工程时,如果原来的工程中有spring,每次打开工程就会提示:Spring Configuratio ...

  3. Spring @Configuration 和 @Component 区别

    Spring @Configuration 和 @Component 区别 一句话概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个 ...

  4. 出现unmapped spring configuration files found

    intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.

  5. IntelliJ IDEA 2017 提示“Unmapped Spring configuration files found.Please configure Spring facet.”解决办法

    当把自己的一个项目导入IDEA之后,Event Log提示“Unmapped Spring configuration files found.Please configure Spring face ...

  6. "Unmapped Spring configuration files found.Please configure Spring facet."解决办法

    最近在学习使用IDEA工具,觉得与Eclipse相比,还是有很多的方便之处. 但是,当把自己的一个项目导入IDEA之后,Event Log提示"Unmapped Spring configu ...

  7. spring configuration 注解

    org.springframework.context.annotation @annotation.Target({ElementType.TYPE}) @annotation.Retention( ...

  8. Spring Configuration注解使用

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

  9. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

随机推荐

  1. 关于 android 返回键 代码实现

    转自:http://www.dewen.io/q/11313/android+%E6%A8%A1%E6%8B%9F%E8%BF%94%E5%9B%9E%E9%94%AE%E5%8A%9F%E8%83% ...

  2. poj3190 Stall Reservations (贪心+优先队列)

    Cleaning Shifts Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  3. Python 示例 饮水记录

    因为每天都需要喝水  这是非常重要的 目录结构: ├─bin│ │ start.py│ ││ └─__pycache__│ start.cpython-36.pyc│├─core│ │ src.py│ ...

  4. centos svn 的搭建

    一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库(repository) 中. ...

  5. Vector的小知识点

    预留容量的两类方式: 1.不调用默认的构造函数 vector<int> v; v.push_back(111); v.reserve(20); std::copy(v.begin(), v ...

  6. CentOS 7.3 CDH 5.10.0 Druid0.12.4安装记录

    CentOS 7.3 CDH 5.10.0安装记录 0. 集群规划192.167.1.247 realtime247 realtime+hadoopdata192.167.1.248 broker24 ...

  7. OpenGL chapter5 基础纹理

    Chapter5 基础纹理 Contents: ==================================================== | 任务 | 使用的函数 ========== ...

  8. webpack(4)--module

    Module module的配置如何处理模块. 配置Loader rules 配置模块的读取和解析规则, 通常用来配置loader, 其类型是一个数组, 数组里每一项都描述了如何去处理部分文件. 配置 ...

  9. 代码生成器 CodeSmith 的使用(三)

    在第二篇中,介绍了用 codesmith 生成数据库中的一些字段,可生成的属性不够简洁,这次对上一次的版本进行重构,生成一些简洁的属性访问器.代码如下: Camel 规则: <%-- Name: ...

  10. PyQt5系列教程(九)QInputDialog的使用

    软硬件环境 Ubuntu 15.10 32bit Python 3.5.1 PyQt 5.5.1 前言 输入框是界面开发中非常常见的控件,本文就来看看PyQt5中QInputDialog的使用 实例 ...