Spring4.0支持Groovy配置
介绍
前一段时间观注了一下Spring4.0的一些特性,当中就有对Groovy配置的支持。因为临时还没有很深入的研究。所以举个小样例来说明一下怎样支持Groovy配置。
package shuai.study.spring.bean;
public class Gasoline {
private int capacity = 0;
public Gasoline(int capacity) {
this.capacity = capacity;
}
public int getCapacity() {
return this.capacity;
}
}
package shuai.study.spring.bean;
public class Car {
private String name = null;
private Gasoline gasoline = null;
public Car(Gasoline gasoline) {
this.gasoline = gasoline;
}
public void setName(String name) {
this.name = name;
}
public void run() {
int capacity = gasoline.getCapacity();
if (gasoline.getCapacity() > 100) {
System.out.println("Gasoline capacity is " + capacity + ", " + this.name + " could start to run.");
} else {
System.out.println("Gasoline capacity is " + capacity + ", " + this.name + " is not enough to run.");
}
}
}
car-config.groovy configuration file:
import shuai.study.spring.bean.Car
import shuai.study.spring.bean.Gasoline beans {
/*
In class Car, we know that we defined variable gasoline as parameter of Car's constructor.
Here we point out this variable's class(i.e. Gasoline), meanwhile set parameter's value of
(Gasoline's) constructor
*/
gasoline(Gasoline, 80) /*
Define bean(i.e. car), and point out this bean's class(i.e. Car), meanwhile inject variable
gasoline to it. naturally we inject its variable(i.e. name) as well.
*/
car(Car, gasoline) {
name = "Benz"
}
}
package shuai.study.spring.bean;
import org.springframework.context.support.GenericGroovyApplicationContext;
public class CarApp {
@SuppressWarnings("resource")
public static void main(String[] args) {
GenericGroovyApplicationContext context = new GenericGroovyApplicationContext("classpath:spring/bean/car-config.groovy");
Car car = (Car) context.getBean("car");
car.run();
}
}
Spring4.0支持Groovy配置的更多相关文章
- 丢弃重口味的xml配置--spring4用groovy配置bean(转)
spring4之前,bean的配置可以主要分为两种方式,一种是使用基于xml,个人非常讨厌这种方式,因为明明一件很简单的事,放在xml中就会多了不少繁杂的信息.另一种方式,是从spring3.0开始, ...
- spring4.0之八:Groovy DSL
4.0的一个重要特征就是完全支持Groovy,Groovy是Spring主导的一门基于JVM的脚本语言(动态语言).在spring 2.x,脚本语言通过 Java scripting engine在S ...
- Spring4.0编程式定时任务配置
看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...
- [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合
原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...
- [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...
- 项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度
前言 系列文章:[传送门] 项目需求: http://www.cnblogs.com/Alandre/p/3733249.html 上一博客写的是基本调度,后来这只能用于,像每天定个时间 进行数据库备 ...
- 项目ITP(五) spring4.0 整合 Quartz 实现任务调度
前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...
- [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口(转)
转自:[CXF REST标准实战系列] 二.Spring4.0 整合 CXF3.0,实现测试接口 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现W ...
- Spring4.0系列9-websocket简单应用
http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...
随机推荐
- Web控件
Web控件可分三类 HTML控件 html服务器控件是在HTML控件的基础上,额外增加了一个在当前页面唯一的ID属性值和一个runat = "server" 属性html服务器控件 ...
- JQuery jsonp使用小记
在一个不支持PHP的主机上,需要对某些页面做访问统计.我的方案是在静态的HTML页面上,用JSONP向能够执行PHP的主机进行跨域请求,从而使用PHP解决这个访问量统计问题. 在服务器端,PHP页面返 ...
- java高精度进制转换
POJ1131 由于本题只有小数部分(整数部分均为0),故在进制转换的之后只能自己手写转换方法了. 8进制转换10进制的方法为,以0.75为例,应是7*8^-1 + 5*8^-2.所以呢,可以 ...
- B题 - A+B for Input-Output Practice (I)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...
- 转:Github上最受关注的前端大牛,快来膜拜吧!
原文来自于:http://code.csdn.net/news/2820990 本文列出了Github上最受关注的10位前端大牛.看看他们负责的项目和提交的代码,你是不是能从中学到些什么? 1. Pa ...
- BZOJ 1208 宠物收养所
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- c# 基础连接已经关闭: 连接被意外关闭,错误的解决
原文:c# 基础连接已经关闭: 连接被意外关闭,错误的解决 调试一个使用HttpWebRequest模拟提交表单的程序的时候频繁出现上述错误提示,google了一下发现了几个解决方案.1.在appli ...
- SQL Server 中的嵌套事务与@@TranCount(转)
在处理事务的时候,一般都用RollBack Transaction来回滚,但是如果在嵌套事务中这样使用的话,就会出现错误. 在SqlServer里,嵌套事务的层次是由@@TranCount全局变量反映 ...
- JPA概要
1 JPA概述 JPA(Java Persistence API,Java持久化API),定义了对象-关系映射(ORM)以及实体对象持久化的标准接口. JPA是JSR-220(EJB3.0)规范的一部 ...
- struts2+jquery+json集成
以下采用struts2+jquery+json模拟一个案例.当点击提交按钮时会把输入的数据提交到后台,然后从后台获取数据在客户端显示. 效果如下: 接下来为struts2+jquery+json集成步 ...