Spring lovin’… from a Swing perspective.
I’m learning Spring.
WOW.
All I can humbly request of my readers is please don’t embark on a new Java project without checking out Spring (if you haven’t already) first.
For those who haven’t heard of it, here’s my practical executive summary of the least Spring can offer your project. I’m sure there’s much more but I’m still learning here… I’ll be using Spring on a Swing-based app, so keep in mind that is my perspective as I’m learning.
- Dependency Injection or Inversion of Control (IoC) - This sounds very complicated, but the basic idea (and even basic usage, IMHO) is alarmingly simple — why haven’t some clever people made this popular before Spring came along? In a nutshell, Spring allows you to declaratively (via XML or other methods) map dependencies between objects, eliminating the need for most look ups or Registry objects.
For example… in previous Swing projects, I have often implemented a Registry class based on Martin Fowler’s pattern from Patterns of Enterprise Application Architecture. This class (either through static methods or a singleton instance) would manage what amounts to registration and retrieval of global object references. If a particular Swing gui component needed a reference to another, disparate component, the Registry would come in handy. This is a fairly clean solution — certainly better than passing object references through multiple gui component constructors — but still reqires a lot of coding. Also, when each new component is created, it must register itself so others may find it.
Generally speaking, this just clutters up the code and becomes quickly annoying.
Now I’m embarking on a new Swing project and will be using Spring (although not Spring RCP) as a container. This will allow me to declaratively use simple setter methods or constructor args to inject the object references from a config file rather than registering and looking up myself. This is so much cleaner!
- Aspect-Oriented Programming — Again, still learning here… I’ll skip a lame attempt at explaining this as it’s very popular already, but here’s how I plan to use it in my Swing app.
My app will need to generate reports of different types (email,SMS,XML and HTML) at regular intervals. Although this is important functionality to the app, it’s not central to the logic of the domain model. Additionally, this same functionality will be needed in many places. Spring allows you to separate these “cross-cutting” concerns using AOP… separating this reporting (logging, basically) from the core business logic completely for ultra-clear code. I think of it as a decorator pattern on steroids.
10 Comments
Jump to comment form | comments rss [?] | trackback uri [?]