I have an application that has multiple screens and each screen is selected via a button. Each screen contains pretty heavy-weight components so it's important that only the activate screen is in memory - all others should be available for garbage collection.
The app uses Spring for the glue and currently it switches screens using getBean():
//event handler for a specific button
public void actionPerformed(Event e) {
setScreen( (Screen) applicationContext.getBean("screen1"));
}
"screen1" is a prototype bean so a new instance of the screen is created when the button is pressed. Also, setScreen() is the only place where a reference to Screen is maintained in the app so the previously active screen will be available for garbage collection. I haven't tested this yet but I expect it will work fine - no rocket science here!
The problem is - after reading this page about why getBean() is considered bad - I'm wondering whether there is a more idiomatic way to acheive the same results while removing the dependency on getBean().
I have looked at method injection and it looks to me to introduce complexity with little benefit. It's another concept to learn, more magic, adds dependency on CGLIB, etc. If I really want to remove the dependency on Spring, I can just introduce an interface that exposes the getBean() method.
Is getBean() and method injection the only options in my case or have I missed something?
And if so, is getBean() really so bad?