使用@SpringBean将ApplicationContext注入Wicket组件失败

Jac*_*ack 4 java spring wicket applicationcontext

我有一个带Wicket的Spring项目.我可以使用@SpringBean注释在Wicket组件中成功注入服务.

现在,我想访问Spring Application Context.所以我已经声明了一个ApplicationContext类型的成员变量,并使用@SpringBean注释它,就像其他服务一样:

尝试使用@SpringBean注入Application

public class MyPanel extends Panel {

    @SpringBean
    private ApplicationContext applicationContext;

    ...
}
Run Code Online (Sandbox Code Playgroud)

但是,在运行时,这会给出错误

bean of type [org.springframework.context.ApplicationContext] not found
Run Code Online (Sandbox Code Playgroud)

是否无法将ApplicationContext注入Wicket组件?如果是这样,那么访问ApplicationContext的合适方式是什么?

mag*_*omi 6

ApplicationContext应该可以在您的应用程序类中访问.

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Run Code Online (Sandbox Code Playgroud)

在应用程序类中创建getApplicationContext方法.

public class MyApplication extends WebApplication {

    public ApplicationContext getAppCtx() {
        return WebApplicationContextUtils.getWebApplicationContext(servletContext);
    }

}
Run Code Online (Sandbox Code Playgroud)

可以从任何wicket组件访问应用程序对象.

public class MyPanel extends Panel {

    public MyPanel(String id) {
        ...
        ApplicationContext appCtx = ((MyApplication) getApplication()).getAppCtx();
        ...
    } 
}   
Run Code Online (Sandbox Code Playgroud)