丢弃重口味的xml配置--spring4用groovy配置bean(转)
spring4之前,bean的配置可以主要分为两种方式,一种是使用基于xml,个人非常讨厌这种方式,因为明明一件很简单的事,放在xml中就会多了不少繁杂的信息。另一种方式,是从spring3.0开始,spring提供了是基于java的配置,相比于xml的配置方式,看起来会好一点儿。而在几天前release的spring4.0中,我们可以用groovy作为spring的配置文件啦!比起最早的基于xml配置,使用groovy会更加灵活,而且干扰信息会更少。比起基于java的配置,groovy配置还要更加精炼!
这篇博客就对现有的xml,java配置方式进行回顾,然后再简单的展示spring4提供的groovy配置方式。本篇博客的代码在github上:https://github.com/kiwiwin/spring4-bean-demo
例子中,我们使用gradle引入依赖:
compile("org.springframework:spring-core:4.0.0.RELEASE",
"org.springframework:spring-context:4.0.0.RELEASE")
下面展示一个很简单的例子,一场足球赛,有homeTeam主队和awayTeam客队,这两个bean是通过构造函数传入。另外还有homeScore和awayScore表示主队和客队分别得到的分数,通过property的方式传入。
下面比较xml、java、groovy三种配置方式
1.使用xml进行bean配置
football-match-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="footballMatch" class="org.kiwi.spring.groovy.FootballMatch">
<constructor-arg ref="homeTeam"/>
<constructor-arg ref="awayTeam"/>
<property name="homeScore" value="3"/>
<property name="awayScore" value="1"/>
</bean> <bean id="homeTeam" class="org.kiwi.spring.groovy.FootballTeam">
<constructor-arg value="Manchester United"/>
</bean> <bean id="awayTeam" class="org.kiwi.spring.groovy.FootballTeam">
<constructor-arg value="AC Milan"/>
</bean> </beans>
虽然我们的IDE已经足够聪明能够帮我们生成xml的各种标签,但是这样的东西看起来确实还是让人觉得不爽。不过如果你真的还是很喜欢这种,那我只能说你开心就好。
2.使用java进行bean配置
首先定义bean:
FootballMatchConfig.java
package org.kiwi.spring.groovy; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class FootballMatchConfig {
@Bean
public FootballMatch footballMatch() {
final FootballMatch footballMatch = new FootballMatch(homeTeam(), awayTeam());
footballMatch.setHomeScore(3);
footballMatch.setAwayScore(1);
return footballMatch;
} @Bean
public FootballTeam homeTeam() {
return new FootballTeam("Manchester United");
} @Bean
public FootballTeam awayTeam() {
return new FootballTeam("AC Milan");
}
}
然后从context中定义java配置文件的位置:
football-match-java-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan
base-package="org.kiwi.spring.groovy" />
</beans>
比起基于纯粹xml的bean配置,基于java的bean配置不再受限于xml本身。配置本身更清晰。
3.使用groovy进行bean配置
FootballMatchConfig.groovy
import org.kiwi.spring.groovy.FootballMatch
import org.kiwi.spring.groovy.FootballTeam beans {
homeTeam(FootballTeam, "Manchester United")
awayTeam(FootballTeam, "AC Milan") footballMatch(FootballMatch, homeTeam, awayTeam) {
homeScore = 3
awayScore = 1
}
}
简单吧!谁看到这种简单的配置方式不会心动呢?
与之前的ApplcationContext不同,为了支持基于groovy的bean配置。spring4中提供了新的正对Groovy的
ApplicationContext:GenericGroovyApplicationContext。 public class FootballApp {
public static void main(String[] args) {
final GenericGroovyApplicationContext context = new GenericGroovyApplicationContext("FootballMatchConfig.groovy");
final FootballMatch footballMatch = (FootballMatch) context.getBean("footballMatch");
System.out.println(footballMatch.display());
}
}
重口味的程序员可能觉得这是吃饱了没事儿撑的用groovy进行bean配置。但是想下maven和gradle,maven基于xml,gradle基于groovy。同样的配置,在gradle中显得十分精炼,同时,因为gradle使用了groovy这样一种语言,而不是xml这种文件,使其更加灵活,在定义task方面会更加的方便。
在这篇博客中,虽然没有写太多基于groovy配置更加强大的方面,但是简洁是显而易见的。简洁就是美。
我们可以非常保守的想一下,在我们所有可以使用xml的地方,其最终都会被其他简洁的语言所替代。
温馨提示:
在初次在IntelliJ中尝试使用groovy进行bean配置时,发生过找不到配置文件的问题。这是因为groovy的配置文件后缀是.groovy。IntelliJ的编译器会把它当成是源文件,而不是配置文件。所以需要在Compiler设置中的Resource Patterns那一栏中去掉groovy对应的!?*.groovy即可
丢弃重口味的xml配置--spring4用groovy配置bean(转)的更多相关文章
- Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC 配置校验器
Spring4新特性——泛型限定式依赖注入 Spring4新特性——核心容器的其他改进 Spring4新特性——Web开发的增强 Spring4新特性——集成Bean Validation 1.1(J ...
- .NET Core采用的全新配置系统[6]: 深入了解三种针对文件(JSON、XML与INI)的配置源
物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonConfigurationSource.XmlConfigura ...
- Java Web的web.xml文件作用及基本配置(转)
其实web.xml就是asp.net的web.config一个道理. 说明: 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. web.xml文件是用来 ...
- maven pom.xml加载不同properties配置[转]
可以参考http://www.openwebx.org/docs/autoconfig.html 1.pom.xml =========================== <!-- 不同的打包 ...
- maven pom.xml加载不同properties配置
1.pom.xml =========================== <!-- 不同的打包环境配置: test=开发/测试测试环境, product=生产环境; 命令行方式: mvn c ...
- Spring4.0支持Groovy配置
介绍 前一段时间观注了一下Spring4.0的一些特性,当中就有对Groovy配置的支持.因为临时还没有很深入的研究.所以举个小样例来说明一下怎样支持Groovy配置. package shuai.s ...
- 深入了解三种针对文件(JSON、XML与INI)的配置源
深入了解三种针对文件(JSON.XML与INI)的配置源 物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonCon ...
- JSP/JSF从web.xml中取出context-param的配置信息
JSP/JSF从web.xml中取出context-param的配置信息. 应用场景:我们配置了项目的版本信息,想让他显示在页面上,如: <context-param><!-- ## ...
- 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置
首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...
随机推荐
- Mysql iot表
我们知道一般的表都以堆(heap)的形式来组织的,这是无序的组织方式. Oracle还提供了一种有序的表,它就是索引组织表,简称IOT表.IOT表上必须要有主键,而IOT表本身不对应segment,表 ...
- Android---OpenGL ES之添加动作
本文译自:http://developer.android.com/training/graphics/opengl/motion.html 在屏幕上绘制对象是OpenGL的最基本功能,你可以使用其他 ...
- 【夯实基础】Spring在ssh中的作用
尊重版权:http://blog.csdn.net/qjlsharp/archive/2009/03/21/4013255.aspx 写的真不错. 在SSH框假中spring充当了管理容器的角色.我们 ...
- linux中vsftpd配置文件详解
vsftpd配置文件采用“#”作为注释符,以“#”开头的行和空白行在解析时将被忽略,其余的行被视为配置命令行,每个配置命令的“=”两边不要留有空格.对于每个配置命令,在配置文件中还列出了相关的配置说明 ...
- Java程序员们最常犯的10个错误(转)
1.将数组转化为列表 将数组转化为一个列表时,程序员们经常这样做: 1 List<String> list = Arrays.asList(arr); Arrays.asList(&quo ...
- 解决Qt程序在Linux下无法输入中文的办法
解决Qt程序在Linux下无法输入中文的办法 一位网友问我怎样在Linux的Qt的应用程序中输入中文,我一開始认为不是什么问题,可是后面自己尝试了一下还真不行.不仅是Qt制作的应用程序,就连Qt Cr ...
- 解析php混淆加密解密的手段,如 phpjm,phpdp神盾,php威盾
原文 解析php混淆加密解密的手段,如 phpjm,phpdp神盾,php威盾 php做为一门当下非常流行的web语言,常常看到有人求解密php文件,想当年的asp也是一样.一些人不理解为什么要混淆( ...
- thinkphp3.2
1.安装WAMPServer,到D:\wamp\. 2.下载ThinkPHP3.2.2核心版.解压缩后,放到D:\wamp\www\MyWeb\.打开浏览器,输入网址:http://localhost ...
- js编码、解码
js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1 ...
- oracle 之 内存—鞭辟近里(四)
oracle 之 内存—鞭辟近里(四) 今天是2013-07-11日,首先我非常感谢我的哥们也是我的网友杨工,非常感谢他能在大数据库内帮我执行一下我所需要的信息.就是他说的网络真是一个互助友爱的平台. ...