Working on my first major SpringMVC app at the moment and having a ball. But this whole ApplicationContext
thing was really bugging me. I was looking up beans all over the place, and in order to get a handle to the appContext to do the lookup I was making heaps of stuff ApplicationContextAware
. How do people feel comfortable doing this in their apps?
But the penny dropped when looking through some of the Lingo spring examples…
Then in your Java code, just lookup the client in Spring (or preferably have it dependency injected into your code by Spring)
Of course! Why am I looking this stuff up via an appContext?!?. Geez. It’s an IoC container - it’s designed to inject stuff.
Couple of hours later and I’d pretty much removed all my ApplicationContextAware beans and almost all my getBean()
calls and injected the real objects straight in them. I now have a much cleaner codebase that isn’t even tied to Spring as an IoC container.
So I think I just discovered a very basic Spring Antipattern: If you have tons of code doing getBean()
lookups, and a dozen ApplicationContextAware
there’s a code smell going on. It’s time to invert…