Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

Summary

197

The birthdayCardSenderAspect is an instance of BirthdayCardSender, which is defined

at the bottom of the file:

class BirthdayCardSender { List peopleSentCards = []

void onBirthday(AdvisedPerson person) { peopleSentCards << person

}

}

Using the config element from the aop namespace, the builder declares an aspect called sendBirthdayCard that references the aspect. After any execution of the birthday method in an advised person, the aspect’s onBirthday method is executed, which adds the person to the peopleSentCards collection. The test then verifies that the aspect did in fact run.

Other tests illustrate other capabilities in BeanBuilder. For example, if the property you’re trying to set requires a hyphen, you put the property in quotes. Some tests show examples like

aop.'scoped-proxy'()

or

jee.'jndi-lookup'(id:"foo", 'jndi-name':"bar")

See the test file for a wide range of examples. The bottom line is that anything you can do in a regular Spring bean configuration file, you can do with the Grails

BeanBuilder.

Lessons learned (Spring with Groovy)

1Spring manages POGOs the same way it manages POJOs, so beans can be implemented in Groovy as easily as in Java.

2Closure coercion eliminates the need for anonymous inner classes.

3By adding a single JAR file, Spock tests work inside the Spring test context.

4Refreshable beans allow you to modify the system without restarting it.

5Inline scripted beans are embedded in configuration files.

6The Grails BeanBuilder gives yet another way to configure Spring.

7.7Summary

This chapter demonstrated all the places where Groovy can work productively with the Spring framework. In addition to writing Spring beans in Groovy, which sometimes results in significant code savings, there are features of Spring unique to beans from dynamic languages. I showed both refreshable beans, in which you deploy the source code and can revise it without stopping the system, and inline scripted beans, in which the beans are defined directly in the configuration file. Groovy beans can also be Spring AOP aspects, as shown. Finally, I reviewed tests from the BeanBuilder

www.it-ebooks.info

198

CHAPTER 7 The Spring framework

class from Grails, which can be used to create Spring bean definitions using the normal Groovy builder syntax, even outside of Grails.

In the next chapter, it’s time to look at database development and manipulation. There, in addition to the cool capabilities of the groovy.sql.Sql class, I’ll also use another contribution from the Grails project, the Grails Object Relational Mapping (GORM) capability.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]