Spring4.0支持Groovy配置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring4.0支持Groovy配置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1881字,纯文字阅读大概需要3分钟。
内容图文
介绍
前一段时间观注了一下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(); } }
原文:http://blog.csdn.net/three_man/article/details/38097283
内容总结
以上是互联网集市为您收集整理的Spring4.0支持Groovy配置全部内容,希望文章能够帮你解决Spring4.0支持Groovy配置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。