在第一讲中显示了如何使用注解配置bean,其实这是Spring3引进的特性,Spring2使用的是XML的方式来配置Bean,那时候漫天的XML文件使得Spring有着配置地狱的称号。Spring也一直在力求改变这一缺陷。Spring3引入的注解方式确实使配置精简不少,而Spring4则引入了Groovy DSL来配置,其语法比XML要简单很多,而且Groovy本身是门语言,其配置文件就相当于代码,可以用来实现复杂的配置。

废话少说,让我们来对Groovy DSL配置来个第一次亲密接触。

首先我们先实现一个XML的bean配置,沿用第一讲中的例子。

configuration.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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="movieService" class="huangbowen.net.service.DefaultMovieService"/> <bean id="cinema" class="huangbowen.net.service.Cinema">
<property name="movieService" ref="movieService"/>
</bean>
</beans>

这个XML文件就不用我多做解释了,很清晰明了。Ok,照例写个测试来测一下。

XmlConfigurationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package huangbowen.net;

import huangbowen.net.service.Cinema;
import huangbowen.net.service.DefaultMovieService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/configuration.xml"})
public class XmlConfigurationTest { @Autowired
private ApplicationContext applicationContext; @Autowired
private Cinema cinema; @Test
public void shouldGetCinemaInstance() {
Cinema cinema = applicationContext.getBean(Cinema.class);
assertNotNull(cinema);
} @Test
public void shouldGetAutowiredCinema() {
assertNotNull(cinema);
} @Test
public void shouldGetMovieServiceInstance() {
assertNotNull(cinema.getMovieService());
assertThat(cinema.getMovieService(), instanceOf(DefaultMovieService.class));
} }

这个测试与第二讲中的测试基本上一样,不过Spring配置的读取是从configuration.xml来的,在@ContextConfiguration中指定了该xml文件为Spring配置文件。

如果想使用Groovy DSL的话第一步需要引入groovy依赖。

pom.xml
1
2
3
4
5
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.2.2</version>
</dependency>

然后就可以新建一个groovy文件来实现配置编写。

Configuration.groovy
1
2
3
4
5
6
7
beans {

   movieService huangbowen.net.service.DefaultMovieService

   cinema huangbowen.net.service.Cinema, movieService : movieService

}

这其实体现不出来Groovy DSL的强大灵活,因为我们的例子太简单了。

beans相当于xml中的beans标签,第一行中是 bean id + class的形式。
第二行是bean id + class + properties map的形式。第二个参数是一个map数组,分别对应property和值。

实现同样的Bean配置有很多种写法。

1
2
3
movieService (huangbowen.net.service.DefaultMovieService)

cinema(huangbowen.net.service.Cinema, {movieService : movieService})

上面这种其实是Groovy语法的一个特性,在调用方法时括号是可选的,既可以加,也可以不加。

1
2
3
4
5
movieService huangbowen.net.service.DefaultMovieService

cinema (huangbowen.net.service.Cinema) {
movieService :ref movieService
}

上面这中使用了另一个设置属性的方法,通过一个闭包将属性设置进去。

1
2
3
4
5
movieService huangbowen.net.service.DefaultMovieService

cinema (huangbowen.net.service.Cinema) {
movieService : movieService
}

这种更好理解了,ref方法也是可选的。

来照旧写个测试来测一下。

GroovyDSLConfigurationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package huangbowen.net;

import huangbowen.net.service.Cinema;
import huangbowen.net.service.DefaultMovieService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AbstractGenericContextLoader; import static huangbowen.net.GroovyDSLConfigurationTest.*;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:Configuration.groovy", loader = GenericGroovyContextLoader.class)
public class GroovyDSLConfigurationTest { public static class GenericGroovyContextLoader extends
AbstractGenericContextLoader { @Override
protected BeanDefinitionReader createBeanDefinitionReader(
GenericApplicationContext context) {
return new GroovyBeanDefinitionReader(context);
} @Override
protected String getResourceSuffix() {
return ".groovy";
} } @Autowired
private ApplicationContext applicationContext; @Autowired
private Cinema cinema; @Test
public void shouldGetCinemaInstance() {
Cinema cinema = applicationContext.getBean(Cinema.class);
assertNotNull(cinema);
} @Test
public void shouldGetAutowiredCinema() {
assertNotNull(cinema);
} @Test
public void shouldGetMovieServiceInstance() {
assertNotNull(cinema.getMovieService());
assertThat(cinema.getMovieService(), instanceOf(DefaultMovieService.class));
} }

在集成测试中如果加载xml配置文件,Spring提供了GenericXmlContextLoader类,如果加载注解方式的配置类,Spring提供了AnnotationConfigContextLoader类。但是对于Groovy配置文件Spring testContext框架还未提供相应的Loader,所以在本测试方法中需要自己实现一个Loader,其实也简单,只要实现两个方法即可。

本例中的源码请在我的GitHub上自行下载。

Spring-Context之三:使用XML和Groovy DSL配置Bean的更多相关文章

  1. Spring(十五):通过注解配置 Bean

    在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...

  2. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

  3. Spring(十四):使用FactoryBean配置Bean

    FactoryBean简介: 1)Spring中Bean包含两种一种是普通Bean,另外一种是FactoryBean.它们都受IOC容器管理,但是也有不同之处. 2)普通Bean与FactoryBea ...

  4. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  5. 使用web.xml方式加载Spring时,获取Spring context的两种方式

    使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...

  6. 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

    spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...

  7. Spring实战——无需一行xml配置实现自动化注入

    已经想不起来上一次买技术相关的书是什么时候了,一直以来都习惯性的下载一份电子档看看.显然,如果不是基于强烈的需求或强大的动力鞭策下,大部分的书籍也都只是蜻蜓点水,浮光掠影. 就像有位同事说的一样,有些 ...

  8. Spring装配Bean---使用xml配置

    声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...

  9. spring4.0之八:Groovy DSL

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

随机推荐

  1. STL库的内存配置器(allocator)

    正在学习中,如果有错,还请多多指教,根据不断的理解,会进行更改,更改之前的样子都会保留下来,记录错误是最大的进步,嗯嗯! 具有次配置力的SGI空间配置器(SGI是STL的一种版本,也有其他的版本) 这 ...

  2. Win10专业版激活方法可查版本

    Win10专业版激活步骤 ------安装Win10专业版,请win+R,键入winver回车,可查看版本------ 1.点击左下角windows按钮,找到设置并打开,依次点击"更新和安全 ...

  3. px4flow通过iic读取具体寄存器数据程序

    底层通信用了昨天写好的iic,今天结合官方资料成功读出所有指定寄存器的数据附上源码 include.h主要包括了一些stm32 IO控制的宏定义,具体参考正点原子所有例程中都有的sys.h头文件 in ...

  4. springboot maven install 找不到符号

    好多朋友在网上找maven install 找不到符号,我今天也遇到了同样的问题,我项目结构如下: 在multicreate-web这个项目引用了multicreate-service的jar包,在i ...

  5. svn不能添加.a文件的解决方法

    上次说用svn add命令添加.a文件,下面是另外的一种解决办法: 修改~/.subversion/config文件,增加一条 # global-ignores = *.o *.lo *.la *.a ...

  6. iis最大工作进程数

    IIS 6.0允许将应用程序池配置成一个Web园(Web Garden).要理解Web园的概念,可以设想这样一种情形:假设有一个IIS 5.0服务器和三个Web网站,每一个Web网站运行着相同的应用程 ...

  7. TTL 生存时间介绍 (转)

    TTL: (Time To Live)生存时间,是IP协议包中的一个值,它告诉网络路由器包在网络中的时间是否太长而应被丢弃.有很多原因使包在一定时间内不能被传递到目的地.例如,不正确的路由表可能导致包 ...

  8. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  9. 位图图像处理控件ImageCapture Suite更新至v9.1

    概述:Dynamsoft公司旗下非常出名的位图图像处理控件ImageCapture Suite更新至了v9.1,这次新版本为Mac版本和IE 9新增了不少功能,同时还对其他组件的性能进行了质的提高! ...

  10. EDA技术与ASIC设计和FPGA开发有什么关系?FPGA在ASIC设计中有什么用途?

    利用EDA技术进行电子系统设计的最后目标是完成专用集成电路ASIC的设计和实现:FPGA和CPLD是实现这一途径的主流器件.FPGA和CPLD通常也被称为可编程专用IC,或可编程ASIC.FPGA和C ...