Spring4.0支持Groovy配置

简介: <p>介绍</p> <p>    前一段时间观注了一下Spring4.0的一些特性,其中就有对Groovy配置的支持。由于暂时还没有非常深入的研究,所以举个小例子来说明一下如何支持Groovy配置。</p> <p><br></p> <p></p> <pre class="java" name="code">package shuai.study.spring.bean;publ

介绍

    前一段时间观注了一下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();
	}
}


相关文章
|
6天前
|
存储 Java 数据安全/隐私保护
|
6天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
16 1
|
6天前
|
XML Java 数据格式
Spring高手之路18——从XML配置角度理解Spring AOP
本文是全面解析面向切面编程的实践指南。通过深入讲解切面、连接点、通知等关键概念,以及通过XML配置实现Spring AOP的步骤。
23 6
Spring高手之路18——从XML配置角度理解Spring AOP
|
6天前
|
消息中间件 开发框架 Java
什么是Spring Boot 自动配置?
Spring Boot 是一个流行的 Java 开发框架,它提供了许多便利的功能和工具,帮助开发者快速构建应用程序。其中一个最引人注目的特性是其强大的自动配置功能。
11 0
|
6天前
|
Java Spring
Spring文件配置以及获取
Spring文件配置以及获取
14 0
|
6天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
22 2
|
6天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
22 1
|
6天前
|
Java 开发者 Spring
Spring Boot中的资源文件属性配置
【4月更文挑战第28天】在Spring Boot应用程序中,配置文件是管理应用程序行为的重要组成部分。资源文件属性配置允许开发者在不重新编译代码的情况下,对应用程序进行灵活地配置和调整。本篇博客将介绍Spring Boot中资源文件属性配置的基本概念,并通过实际示例展示如何利用这一功能。
27 1
|
6天前
|
Java Spring 容器
如何用基于 Java 配置的方式配置 Spring?
如何用基于 Java 配置的方式配置 Spring?
|
6天前
|
存储 前端开发 Java
第十一章 Spring Cloud Alibaba nacos配置中心
第十一章 Spring Cloud Alibaba nacos配置中心
30 0