介绍

前一段时间观注了一下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配置的更多相关文章

  1. 丢弃重口味的xml配置--spring4用groovy配置bean(转)

    spring4之前,bean的配置可以主要分为两种方式,一种是使用基于xml,个人非常讨厌这种方式,因为明明一件很简单的事,放在xml中就会多了不少繁杂的信息.另一种方式,是从spring3.0开始, ...

  2. spring4.0之八:Groovy DSL

    4.0的一个重要特征就是完全支持Groovy,Groovy是Spring主导的一门基于JVM的脚本语言(动态语言).在spring 2.x,脚本语言通过 Java scripting engine在S ...

  3. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  4. [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

    原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...

  5. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...

  6. 项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度

    前言 系列文章:[传送门] 项目需求: http://www.cnblogs.com/Alandre/p/3733249.html 上一博客写的是基本调度,后来这只能用于,像每天定个时间 进行数据库备 ...

  7. 项目ITP(五) spring4.0 整合 Quartz 实现任务调度

    前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...

  8. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口(转)

    转自:[CXF REST标准实战系列] 二.Spring4.0 整合 CXF3.0,实现测试接口 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现W ...

  9. Spring4.0系列9-websocket简单应用

    http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...

随机推荐

  1. 一个中型项目:本地校园App

    好暨: 这个项目的起源于课堂老师作业的要求.老师要求一年下来完成一个构想并实现Demo.思考良久,在要提交构想的那个晚上,想到了校园App,当时团队只有两个人,但我感觉到:就是它了!项目启动时间——2 ...

  2. sed 简明教程

    做个标记 http://coolshell.cn/articles/9104.html sed全名叫stream editor,流编辑器,用程序的方式来编辑文本,相当的hacker啊.sed基本上就是 ...

  3. Smart Client Software Factory安装

    首先要安装 Visual Studio 2010 SDK 不然无法安装 Smart Client Software Factory 2010 然后按顺序安装 GAX 2010 http://visua ...

  4. opencv之图像腐蚀

    最近准备好好学习opencv 这博客就是我的学习笔记. #include <cv.h> #include <highgui.h> using namespace std; vo ...

  5. Rewrite的QSA是什么意思?

    原版的英文: When the replacement URI contains a query string, the default behavior of RewriteRule is to d ...

  6. NSString 练习

        //将“⽂文艺⻘青年”改成“213⻘青年”.   NSString *str = @"文艺青年";   NSString *str1 = [str stringByRepl ...

  7. Tautology

    WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of th ...

  8. Ubuntu 12.04 使用Eclipse搭建C/C++编译环境

    首先是安装Eclipse,方法有两种:       第一种是通过Ubuntu自带的程序安装功能安装Eclipse,应用程序->Ubtuntu软件中心,搜Eclipse安装即可.       第二 ...

  9. (转载)PHP中刷新输出缓冲

    (转载)http://www.cnblogs.com/mutuan/archive/2012/03/18/2404957.html PHP中刷新输出缓冲buffer是一个内存地址空间,Linux系统默 ...

  10. 数据结构(RMQ):POJ 3624 Balanced Lineup

    Balanced Lineup   Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always li ...