我有一个使用Guice [v3]的Wicket [v6]应用程序 - 我目前在仓库操作中使用依赖注入,现在我想将其扩展到使用每个用户会话的服务(每个用户的会话一个服务)。我已经阅读了官方文档、各种博客文章和问题,但我不确定我是否使用了正确的方法。
我的两个问题: 1. 我是否使用了正确的方式? 2. 我需要为依赖于SessionScoped注入的类运行TestNG测试,还需要什么特殊设置吗?
我的设置: web.xml:
在一个示例页面中,我有以下内容:
在进行单元测试的userService.doSomething期间,我遇到了Guice OutOfScopeException错误,指向我的ServletModule中的绑定: 在自定义提供者中出现错误,com.google.inject.OutOfScopeException?:无法访问作用域对象。我们当前不在HTTP Servlet请求内,或者您可能已经忘记为此请求应用com.google.inject.servlet.GuiceFilter作为servlet过滤器。 我的配置是否正确,需要以不同的方式运行单元测试(我只是使用WicketTester启动我的应用程序),还是我的设计有问题?
我的两个问题: 1. 我是否使用了正确的方式? 2. 我需要为依赖于SessionScoped注入的类运行TestNG测试,还需要什么特殊设置吗?
我的设置: web.xml:
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.xxx.CustomServletConfig</listener-class>
我的应用程序初始化:
@Override
protected void init()
{
super.init();
getResourceSettings().setResourcePollFrequency(null);
getMarkupSettings().setStripWicketTags(true);
getDebugSettings().setDevelopmentUtilitiesEnabled(true);
GuiceComponentInjector injector = new GuiceComponentInjector(this, new WebModule(), new GuiceModule());;
}
CustomServletConfig:
public class CustomServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new GuiceModule(), new WebModule());
}
Web模块:
public static class WebModule extends ServletModule {
@Override
protected void configureServlets() {
bind(WebApplication.class).toProvider(WicketGuiceAppProvider.class).asEagerSingleton();
bind(IUserService.class).to(UserService.class).in(ServletScopes.SESSION);
Map<String, String> params = new HashMap<String, String>();
params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
filter("/*").through(WicketGuiceFilter.class, params);
}
}
在一个示例页面中,我有以下内容:
@Inject
IUserService userService
...
userService.doSomething
在进行单元测试的userService.doSomething期间,我遇到了Guice OutOfScopeException错误,指向我的ServletModule中的绑定: 在自定义提供者中出现错误,com.google.inject.OutOfScopeException?:无法访问作用域对象。我们当前不在HTTP Servlet请求内,或者您可能已经忘记为此请求应用com.google.inject.servlet.GuiceFilter作为servlet过滤器。 我的配置是否正确,需要以不同的方式运行单元测试(我只是使用WicketTester启动我的应用程序),还是我的设计有问题?