一. Xml配置法

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

[xml] view
plain
 copy
  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] view
plain
 copy
  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml");
  2. OrderService orderService = (OrderService) ctx.getBean("orderService");

二. @Configuration配置法

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

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

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

[java] view
plain
 copy
  1. JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class);
  2. OrderService orderService = ctx.getBean(OrderService.class);

 

三. 这么做有什么好处呢?

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

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

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

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

转载自:http://blog.csdn.net/tanksyg/article/details/8556769

Spring的@Configuration来代替xml配置的更多相关文章

  1. MongoDB和Java(4):Spring Data整合MongoDB(XML配置)

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  2. Spring声明式事务(xml配置事务方式)

    Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...

  3. spring RedisTemplate的使用(一)--xml配置或JavaConfig配置

    1.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="h ...

  4. spring启动,spring mvc ,要不要xml配置,基于注解配置

    老项目是09-11年搞的,用的是spring+struts2,没有用注解,全xml配置.web.xml中也配置了一大堆. 现在启动新项目,在项目中用spring+springmvc ,主要用注解,也用 ...

  5. 死磕Spring之AOP篇 - Spring AOP注解驱动与XML配置

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  6. spring Ioc容器之使用XML配置Bean

    1.项目截图 2.创建xml文件 3.打印机接口 package com.example.demo.computerTest; public interface Printer { void init ...

  7. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  8. Spring 及 SpringMVC的web.xml配置详解

    出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...

  9. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

随机推荐

  1. JavaScript判断数组是否包含指定元素的方法

    本文实例讲述了JavaScript判断数组是否包含指定元素的方法.分享给大家供大家参考.具体如下: 这段代码通过prototype定义了数组方法,这样就可以在任意数组调用contains方法 /** ...

  2. nodejs:遍历文件夹文件统计文件大小

    根据 http://blog.csdn.net/hero82748274/article/details/45700465这里的思路对读写文件做了一个 封装: webpack在打包的时候可以借助ass ...

  3. 换个语言学一下 Golang (1)

    做技术的总是有些拗.这么多年一直在.net的框框里打转转.直到现在市场上.net工作越来越难找,项目越来越老才发现不做出改变不行了.就从学习Go开始吧. Go语言的特点 简洁.快速.安全 并行.有趣. ...

  4. 674. Longest Continuous Increasing Subsequence@python

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  5. [LUOGU] P1536 村村通

    题目描述 某市调查城镇交通状况,得到现有城镇道路统计表.表中列出了每条道路直接连通的城镇.市政府"村村通工程"的目标是使全市任何两个城镇间都可以实现交通(但不一定有直接的道路相连, ...

  6. NFS网络共享服务 挂载参数及优化 内核优化建议

    配置NFS服务端 nfs01上安装软件 [root@nfs01 ~]# yum install nfs-utils rpcbind -y nfs-utils:NFS服务的主程序,包括rpc.nfsd. ...

  7. systemverilog之OOP

    what is oop terminology an example class default methods for classes static attibute assigment and c ...

  8. perl学习之:编译、执行与内存关系(转)

    1.所谓在编译期间分配空间指的是静态分配空间(相对于用new动态申请空间),如全局变量或静态变量(包括一些复杂类型的 常量),它们所需要的空间大小可以明确计算出来,并且不会再改变,因此它们可以直接存放 ...

  9. (转)5个Xcode开发调试技巧

    1.Enable NSZombie Objects(开启僵尸对象) Enable NSZombie Objects可能是整个Xcode开发环境中最有用的调试技巧.这个技巧非常非常容易追踪到重复释放的问 ...

  10. Java并发编程的艺术 记录(一)

    模拟死锁 package com.gjjun.concurrent; /** * 模拟死锁,来源于<Java并发编程的艺术> * @Author gjjun * @Create 2018/ ...