UI instance is already initialized on logout with Shiro/Spring/UIProvider

I have Vaadin 7.2.6 with a UIProvider for Spring so I can use Shiro authentication with WebSockets. Everythings works until I log out. The I get this IllegalStateException. It keeps trying to create a new UI and it’s not going through the UIProvider per the stack trace.

Aug 11, 2014 4:52:32 PM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
java.lang.IllegalStateException: This UI instance is already initialized (as UI id 0) and can therefore not be initialized again (as UI id 0). Please make sure you are not accidentally reusing an old UI instance.
at com.vaadin.ui.UI.doInit(UI.java:630)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1401)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:237)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)

This is the only thing in the UIProvider class:

/* (non-Javadoc)
 * @see com.vaadin.server.UIProvider#createInstance(com.vaadin.server.UICreateEvent)
 */
@Override
public UI createInstance(UICreateEvent event) {
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(VaadinServlet.getCurrent().getServletContext());
    return ctx.getBean(event.getUIClass());
}

After I log the Subject out I call:
ui.getSession().close();
ui.getRoot().removeAllComponents();
ui.getPage().setLocation(“”);

Any ideas?

Maybe your UI is a singleton, should be prototype.

I have these annotations:

@Component
@Scope(“prototype”)

I figured it out. The problem was I mixing the annotations and using xml based configuration. When I put scope=“prototype” on the bean definition in the xml and removed the annotations it resolved.