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

Summary

45

Lessons learned (Groovy Baseball)

1POGOs have private attributes and public methods by default. Public getters and setters are auto-generated for each attribute.

2POGOs include a map-based constructor that can be used to set any or all of the attributes in any combination.

3Closures and methods in Groovy return their last evaluated expressions automatically.

4The XmlSlurper class makes parsing XML simple and returns the root of the resulting DOM tree. Values can be extracted by walking the tree.

5The MarkupBuilder class produces XML.

6The groovy.sql.Sql class is a simple façade for dealing with relational databases.

7Groovlets are simple Groovy scripts that respond to HTTP requests.

8All Groovy exceptions are unchecked.

The rest of the system is just HTML and JavaScript, so it’s beyond the scope of a Groovy discussion. The complete source code for the application is contained in the GitHub repository for the book.

2.4Summary

This chapter is a tutorial on Groovy for Java developers, using example applications rather than a series of features. What’s remarkable is how much Groovy simplifies the code. POGOs are a minimal yet more flexible version of POJOs. The groovy.sql.Sql class makes JDBC practical for reasonably small applications. The Groovy JDK adds many convenience methods, like toURL and getText, which make existing Java classes easier to use. The combination of maps, closures, and the join method makes it simple to build URLs for web services. Finally, the difference between working with XML in Java and working with XML in Groovy is staggering. Whenever I have to work with XML in any form I always look for a way to add a Groovy module to handle the details.

In the next chapter we’ll examine mechanisms to integrate Java and Groovy together in more detail.

www.it-ebooks.info

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