Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting ApplicationContext into Wicket Component with @SpringBean fails

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?

like image 840
Jack Avatar asked Jan 17 '23 03:01

Jack


1 Answers

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();
        ...
    } 
}   
like image 95
magomi Avatar answered Jan 27 '23 06:01

magomi