Resources and Quiz Answers
The purpose of this appendix is to help you set up the development environment that you will use to write, compile, and execute the Spring applications specific to this book and to provide detailed responses for the questions in the quiz attached to each chapter.
The appendix for this book is quite small because it was written in such a way that you are guided through the normal succession of steps that you would have to through every time you start development of an application.
At the end of Chapter 1, I presented the tools and instructed you how to install them and how to verify the correct installation. The code samples for the book were written on an Apple Mac computer. One of the strong points of Java is that it is multi-platform, so the code can be run on any operating system. The tools recommended (Gradle, Intellij IDEA, Liferay, and Tomcat) are Java based, and are available at their official sites. The installation instructions are almost identical for any operating system, the only difference is in how an environment variable is set. Information about doing this on different operating systems is widely available on the internet and considering this book is for developers with a little experience, this should not be a problem for you.
The code for this book is split in two projects: personal-records and book-code. The personal-records project follows the evolution of a Spring web application. It was built incrementally and module names were prefixed with a number; if traversed in the ascending order of their prefixes, you will notice that every module contains the code of the previous one and something extra. It simulates the evolution of the configuration, exchanging and adding libraries until the final form of a complete web application is reached, when security and evolved components such as web flows are in place.
Using this study guide you will learn not only how to build Spring web applications, but also how to design a workflow for you and your team and how to design a multi-layered application from scratch.
The code was split in two because some topics such as alternative configurations and alternative view technologies needed to be presented separately without overcrowding the personal-records project. So really small modules covering these were created and wrapped up in a different project called book-code.
For book-code, the name of each module is constructed using the chapter number in which the module is covered and an index number. The modules are referenced in a chapter in ascending order of their indexes.
Both projects are Gradle multi-module projects and are configured in a similar manner. The project structures can be seen side by side in Figure A-1.
Figure A-1. personal-records and book-code project structures in IntelliJ IDEA side by side
Gradle Configuration Explained
project-records and book-code are parent projects that define a set of libraries available for the child modules to use. As the configurations are very similar, from this point on only the configuration of the person-records project will be covered. person-records has the Gradle configuration in a file named build.gradle.
All the modules have the Gradle configuration file named after the module: [module_name].gradle. Also, there’s a closure element in personal-records/settings.gradle that verifies at build time if all modules have their configuration file present.
rootProject.children.each {
project -> project.buildFileName = "${project.name}.gradle"
assert project.projectDir.isDirectory()
assert project.buildFile.exists()
assert project.buildFile.isFile()
}
This was a development choice; the components of a module are also more visible in an editor this way. Plus, if you want to modify the configuration file for a module you can easily find the file in IDEA using a unique name. Imagine the pain if you use the Command+Shift+N to search for a specific build.gradle file and you have 20+ matches.
Another approach for a multi-modular project would have been to have only one build.gradle file for the whole project and use Gradle-specific closures to customize configuration for each module. But in the spirit of good development practices, I decided to keep configurations for the modules as decoupled as possible and in the same location as the module contents.
After you download the source code you need to import the project in the IntelliJ IDEA editor:
Figure A-2. Project import menu options in IntelliJ IDEA
Figure A-3. Project import popup in IntelliJ IDEA
Figure A-4. Selecting the project type in IntelliJ IDEA
Figure A-5. Last popup for project import in IntelliJ IDEA
Before getting to work you should build the project. This can be done from IntelliJ IDEA by clicking the Refresh button, marked with (1) in in Figure A-6. Clicking this button will cause IntelliJ IDEA to do the following: scan the configuration of the project, resolve any dependencies (this includes downloading missing libraries), and do an internal light build of the project, just enough to remove compile-time errors caused by missing dependencies.
The Gradle build task executes a full build of the project. It can be used in the command line:
.../workspace/personal-records $ gradle build
Alternatively, you can use it in IntelliJ IDEA as depicted in Figure A-6, where the task is marked with (2).
Figure A-6. Gradle tasks in IntelliJ IDEA
It will execute the following set of tasks on every module:
:00-pr-dao:compileJava UP-TO-DATE
:00-pr-dao:processResources UP-TO-DATE
:00-pr-dao:classes UP-TO-DATE
:00-pr-dao:jar UP-TO-DATE
:00-pr-dao:assemble UP-TO-DATE
:00-pr-dao:compileTestJava UP-TO-DATE
:00-pr-dao:processTestResources UP-TO-DATE
:00-pr-dao:testClasses UP-TO-DATE
:00-pr-dao:test UP-TO-DATE
:00-pr-dao:check UP-TO-DATE
:00-pr-dao:build UP-TO-DATE
The tasks depicted here are only for the 00-pr-dao module. The Gradle build task will execute all the tasks it depends on. As you can see, it does not run the clean task, so you need to make sure to run this task manually when building a project multiple times, to make sure the most recent versions of the classes are used.
As the project contains incomplete sources that you will have to complete (in modules post fixed with -practice), executing this task will fail. You could just execute tasks clean and compile Java, but there’s a better way. I have created a custom task in the project called allCompile. This task executes the clean and compileJava tasks for all modules. It is marked with (3) in Figure A-6. It is defined in build.gradle and inherited by the child modules, so it can be executed for a module separately.
Every web application in this project is run with the Jetty embedded web server to keep things simple. But there are certain advantages in using an external container such as Apache Tomcat server. Starting the server in debug mode and using breakpoints to debug an application is much easier to do is one advantage. An external container can run multiple applications at a time without the need to stop the server. Plus embedded servers should be used only for testing and educational purposes; in practice application servers are preferred, because of reasons explained in Chapter 8.
Here is what you have to do if you are interested in doing this. First download the latest version of Apache Tomcat 8.x from the official site1 and unpack it somewhere on your system. Then configure an IntelliJ IDEA launcher to start the server and deploy the chosen application. This is quite easy to do, but there are a number of steps to be executed and they are listed below:
Figure A-7. Menu options to create a Tomcat launcher in IntelliJ IDEA
Figure A-8. Popup to create a Tomcat launcher in IntelliJ IDEA
In the previous figure, some items are numbered and their meaning is explained in following list:
Figure A-9. Configure Tomcat instance in IntelliJ IDEA
Figure A-10. Deployable artifact list in IntelliJ IDEA
Figure A-11. Inset a new application context in IntelliJ IDEA
Other application servers can be used in a similar way as long as IntelliJ IDEA provides support for them.
IntelliJ IDEA is really flexible and practical and that’s why it was recommended for practicing the exercises in this study guide: launcher configurations can be duplicated, multiple Tomcat instances can be started at the same time as long as they function on different ports, and so on. The Gradle projects can also be imported in Eclipse and other Java editors that support Gradle.
Quiz Answers
The following sections contain answers to the quiz questions for every chapter. Answers to questions that are simple enough to remember after reading the chapter will not be detailed. Extra details will be provided only for questions that could be considered tricky.
Quiz Solution for Chapter 2
Quiz Solution for Chapter 3
Quiz Solution for Chapter 5
Quiz Solution for Chapter 6
Quiz Solution for Chapter 7
_______________________________
2http://docs.spring.io/spring/docs/4.2.3.RELEASE/spring-framework-reference/htmlsingle/#beans- annotation-config
3http://docs.spring.io/spring/docs/4.2.3.RELEASE/spring-framework-reference/htmlsingle/#mvc- servlet-special-bean-types
4http://docs.spring.io/spring/docs/4.2.3.RELEASE/spring-framework-reference/htmlsingle/#mvc- ann-return-types
3.145.179.35