Swipe to navigate through the chapters of this book
In this chapter, we’ll expand on our sample application and learn about the lifecycle options in Spring. We’ll introduce how to invoke methods when Spring beans are created or destroyed and how to do so via multiple configurable options either using the Spring XML file, annotations, or the programmatic configurations, all of which were used in Chapter 3.
Please log in to get access to this content
Finalization has long been known to be a good way to wreck your application’s performance, but sometimes it was the only tool available that allowed classes to know when to force-remove allocated resources. Since Java 9, though, there’s an alternative: the java.lang.ref.Cleaner class, which isn’t entirely trivial but is far more safe to use.
The builder pattern is a pattern described by Design Patterns: Elements of Reusable Object-Oriented Software, by Gamma, Helm, Johnson, and Vlissides. It’s a pattern that refers to how objects are created consistently; see https://en.wikipedia.org/wiki/Builder_pattern for more.
Java’s source rules specify that a file must be named the same as a single public class contained in that file. In this case, we have no public classes, which limits their use to inside the package in which they exist.
This is sophistry, really. A method specified with init-method would have to be manually called, as would a method specified with destroy-method, to make sense; these methods exist primarily in the context of Spring lifecycles, and which one you choose is really up to you, although you’re probably best off using annotations – covered later in this chapter – to accomplish similar effects.
This assumes you’re not using source code downloaded from https://apress.com for this book; if you’re using the source code from the book’s web site, all of these files will obviously be in the right places already.
Joseph B. Ottinger
- Sequence number
- Chapter number
- Chapter 4