我想在Spring MVC項目中使用針對所有瀏覽器的JDK 1.6提供推送通知。我跟着this post,最後決定去Atmosphere。 對於服務器發送的事件,我的服務器控制器(source):整合大氣與Spring MVC
@Controller
public class AtmosphereController {
@RequestMapping(value="/getTime", method=RequestMethod.GET)
@ResponseBody
public void websockets(final AtmosphereResource atmosphereResource) {
final HttpServletRequest request = atmosphereResource.getRequest();
final HttpServletResponse response = atmosphereResource.getResponse();
atmosphereResource.suspend();
final Broadcaster bc = atmosphereResource.getBroadcaster();
bc.scheduleFixedBroadcast(new Callable<String>() {
public String call() throws Exception {
return (new Date()).toString();
}
}, 10, TimeUnit.SECONDS);
}
}
在運行時,我得到了org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.atmosphere.cpr.AtmosphereResource]: Specified class is an interface
。 在解決這個問題時,我得到了this relevant post。我將此添加到我的調度員servlet.xml中:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean id= "atmosphereResource"
class="org.atmosphere.cpr.AtmosphereResourceImpl" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
這樣做,也導致新的錯誤:
[cvc-complex-type.2.1: Element 'mvc:annotation-driven' must have no character or element information item [children], because the type's content type is empty.]
我也試過this。請幫我在Spring Controller中注入AtmosphereResource。我是否還需要更新web.xml或其他一些配置文件才能使其工作或缺少哪些部分。請幫忙! 也請評論其他提供服務器端事件功能的替代方案。提前致謝!
是參數解析程序是否正確? AtmosphereResourceImpl是否實現了一個Spring MethodArgumentResolver? –