I have a Spring Project with Wicket. I can successfully inject Services in Wicket components with @SpringBean annotation.
Now, I want to access the Spring Application Context. So I've declared a member variable of type ApplicationContext and annotated it with @SpringBean, just like other services as well:
trying to use @SpringBean to inject the Application
public class MyPanel extends Panel {
@SpringBean
private ApplicationContext applicationContext;
...
}
However, at runtime, this gives the error
bean of type [org.springframework.context.ApplicationContext] not found
Is it not possible to inject ApplicationContext into Wicket Components? If so, what would be a suitable way to get access to the ApplicationContext?
The ApplicationContext should be accessible in your application class.
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Create a getApplicationContext method in your application class.
public class MyApplication extends WebApplication {
public ApplicationContext getAppCtx() {
return WebApplicationContextUtils.getWebApplicationContext(servletContext);
}
}
The application object can be accessed from any wicket component.
public class MyPanel extends Panel {
public MyPanel(String id) {
...
ApplicationContext appCtx = ((MyApplication) getApplication()).getAppCtx();
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With