How can I bind a particular dependency to a page, and have it injected for components on that page?
I have set up an environment in my wicket application, using the (very small) wicket-ioc and wicket-spring libraries, where I can call InjectorHolder.getInjector().inject(component), in order to inject the component with dependencies. All of my wicket components are injected by Spring via an IComponentInstantiationListener, and this part works (I use it for hibernate access).
I have a serializable object (ComponentGraph) which I want to store as a field on the Page. H开发者_StackOverflowow can I get Spring to figure out which page the component is on, and inject the right ComponentGraph for that page when a ComponentGraph field with @SpringBean is declared?
If someone could point me in the right direction, I would appreciate it. I have a solid grasp of Wicket, but Spring is still a maze of unfamiliar concepts to me, at this point. =)
This does not sound like a job for Spring.
However, I don't know the scope of your ComponentGraph, so I have to make some guesses.
a) If there is one per user, store it in a custom session object.
b) If there is one per page instance, make a base page class from which all your such pages inherit. Let it have a componentGraph
field with a getter and a setter.
c) If there is one per page class, keep a map of type <Class, ComponentGraph>
in a helper bean (accessible through something like helper.getComponentGraphForPageType(this.getClass())
. This could (and should) be injected via Spring
Spring has support for request and session scopes, but does not know what the Wicket page scope is. No worries, though. If you inject a graph factory into the page, you could do something like this:
public class Page extends WebPage {
@SpringBean private GraphFactory factory;
private ObjectGraph graph = null;
public Page() {
...
}
protected ObjectGraph getGraph() {
if (graph == null) graph = factory.createGraph();
return graph;
}
}
After the graph has been created, it will be serialized with your page as expected.
精彩评论