gpt4 book ai didi

java - 带有 Spring 和多用户的 Vaadin

转载 作者:行者123 更新时间:2023-11-30 11:41:41 24 4
gpt4 key购买 nike

我的新 Vaadin 项目有一个非常奇怪的问题。如果我启动我的应用程序并使用用户登录,然后切换浏览器并再次打开站点,似乎 session 将被共享。我在两个 Windows 中都有相同的内容。

我读了一些关于这个问题的论坛帖子,他们都说不要在同一个浏览器中打开应用程序。好的,我启动了一个虚拟机,但是,同样的问题。或者,从上下文中删除所有静态变量。我已经做到了,现在使用 ThreadLocal-Pattern。

我完全不知道出了什么问题。我相信 Spring 上下文会杀死 Vaadin,有没有人有过使用 Vaadin 和 Spring 的这种经历?

为了将 Spring 和 Vaadin 结合在一起,我使用了 Nicolas Frankel 的 Spring Intgration Addon for Vaadin (https://vaadin.com/directory#addon/spring-integration)

我将发布我的申请代码以供进一步调查。

===================

Vaadin 应用程序类:

public class MainApplication extends Application implements HttpServletRequestListener    {
private static final long serialVersionUID = 2067470624065324231L;

private static ThreadLocal<MainApplication> threadLocal = new ThreadLocal<MainApplication>();

private Window mainWindow;

//Viewmanager
private ViewManager viewManager;

private Professor professor;

//DAO Classes and Bean Setter´s
ProfessorDAO professorDAO;
QuestionDAO questionDAO;
AnswerDAO answerDAO;
AnsweredDAO answeredDAO;
QRCodeDAO qrCodeDAO;

public void setProfessorDAO(ProfessorDAO professorDAO) {
System.out.println("Setting ProfessorDAO!");
System.out.println(professorDAO.toString());
this.professorDAO = professorDAO;
}

public void setQuestionDAO(QuestionDAO questionDAO) {
System.out.println("Setting QuestionDAO!");
System.out.println(questionDAO.toString());
this.questionDAO = questionDAO;
}

public void setAnswerDAO(AnswerDAO answerDAO) {
System.out.println("Setting AnswerDAO!");
System.out.println(answerDAO.toString());
this.answerDAO = answerDAO;
}

public void setAnsweredDAO(AnsweredDAO answeredDAO) {
System.out.println("Setting AnsweredDAO!");
System.out.println(answeredDAO.toString());
this.answeredDAO = answeredDAO;
}

public void setQrCodeDAO(QRCodeDAO qrCodeDAO) {
System.out.println("Setting QRCodeDAO!");
System.out.println(qrCodeDAO.toString());
this.qrCodeDAO = qrCodeDAO;
}

public ProfessorDAO getProfessorDAO() {
return professorDAO;
}

public QuestionDAO getQuestionDAO() {
return questionDAO;
}

public AnswerDAO getAnswerDAO() {
return answerDAO;
}

public AnsweredDAO getAnsweredDAO() {
return answeredDAO;
}

public QRCodeDAO getQrCodeDAO() {
return qrCodeDAO;
}

//Currently logged in Professor.
public Professor getProfessor() {
return professor;
}

public void setProfessor(Professor professor) {
this.professor = professor;
}



public static void setInstance(MainApplication application){
threadLocal.set(application);
}

public ViewManager getViewManager() {
return viewManager;
}

public void setViewManager(ViewManager viewManager) {
this.viewManager = viewManager;
}

public static MainApplication getInstance(){
return threadLocal.get();
}

public MainApplication() {
}

@Override
public void init() {
setInstance(this);

setTheme("crs");

mainWindow = new Window("CRS -- Classroom Response System");
setMainWindow(mainWindow);

viewManager = new ViewManager(mainWindow);

viewManager.switchScreen(LoginScreen.class.getName(), new LoginScreen());


//mainWindow = new MainWindow("CRS -- Classroom Response System");
//setMainWindow(mainWindow);


}


public void onRequestStart(HttpServletRequest request,
HttpServletResponse response) {
MainApplication.setInstance(this);

}

public void onRequestEnd(HttpServletRequest request,
HttpServletResponse response) {
threadLocal.remove();

}

web.xml:

<display-name>CRSServer</display-name>


<context-param>
<description>
Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>


<servlet>
<servlet-name>CRS</servlet-name>
<servlet-class>ch.frankel.vaadin.spring.SpringApplicationServlet</servlet-class>
<init-param>
<description>
Vaadin application class to start</description>
<param-name>applicationBeanName</param-name>
<param-value>cs.hm.edu.kreipl.crs.frontend.MainApplication</param-value>
</init-param>

<init-param>
<description>
Application widgetset</description>
<param-name>widgetset</param-name>
<param-value>cs.hm.edu.kreipl.crs.widgetset.CrsserverWidgetset</param-value>
</init-param>
</servlet>


<servlet-mapping>
<servlet-name>CRS</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-config.xml</param-value>
</context-param>

Spring 语境

<context:component-scan base-package="cs.hm.edu.kreipl.crs" />
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/crs"></property>
<property name="username" value="crs"></property>
<property name="password" value="password"></property>
</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="annotatedClasses">
<list>
<value>cs.hm.edu.kreipl.crs.database.entity.Professor</value>
<value>cs.hm.edu.kreipl.crs.database.entity.Answer</value>
<value>cs.hm.edu.kreipl.crs.database.entity.Answered</value>
<value>cs.hm.edu.kreipl.crs.database.entity.Question</value>
<value>cs.hm.edu.kreipl.crs.database.entity.QuestionQRCode</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">create</prop>
</props>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>

<tx:annotation-driven/>

<!-- VAADIn Startup -->
<bean id="cs.hm.edu.kreipl.crs.frontend.MainApplication" class="cs.hm.edu.kreipl.crs.frontend.MainApplication">
<property name="answerDAO" ref="answerDAO" />
<property name="answeredDAO" ref="answeredDAO" />
<property name="professorDAO" ref="professorDAO" />
<property name="qrCodeDAO" ref="qrCodeDAO" />
<property name="questionDAO" ref="questionDAO" />
</bean>



<bean id="professorDAO" class="cs.hm.edu.kreipl.crs.database.implementation.ProfessorDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Professor</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="questionDAO" class="cs.hm.edu.kreipl.crs.database.implementation.QuestionDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Question</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="qrCodeDAO" class="cs.hm.edu.kreipl.crs.database.implementation.QRCodeDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.QuestionQRCode</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="answerDAO" class="cs.hm.edu.kreipl.crs.database.implementation.AnswerDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Answer</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="answeredDAO" class="cs.hm.edu.kreipl.crs.database.implementation.AnsweredDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Answered</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>
<bean id="deviceDAO" class="cs.hm.edu.kreipl.crs.database.implementation.DeviceDAOImpl">
<constructor-arg>
<value>cs.hm.edu.kreipl.crs.database.entity.Device</value>
</constructor-arg>
<property name="hibernateTemplate" ref="mySessionFactory"/>
</bean>

如果您需要任何其他类(class),请立即联系我。

最佳答案

我正在使用 Vaadin + Spring 但没有使用 Spring Integration Addon。我认为问题在于您没有为请求应用程序的新用户创建新的 Vaadin applicationBean。

在我的应用程序中,我在 web.xml 中传递了 applicationBean。我的 Vaadin 应用程序注释为:

应用类

@Component(value = "appName")
@Scope(value = "session")
class AppName extends Application {
....
}

WEB.XML

    <init-param>
<param-name>applicationBean</param-name>
<param-value>appName</param-value>
</init-param>

对于每个新 session ,Servlet 都会返回新的应用程序。在您的 web.xml 中,您像这样传递您的 vaadin 应用程序:

您的 WEB.XML

   <init-param>
<description>
Vaadin application class to start</description>
<param-name>applicationBeanName</param-name>
<param-value>cs.hm.edu.kreipl.crs.frontend.MainApplication</param-value>
</init-param>

因此它每次为所有用户返回相同的应用程序。这就是我认为导致问题的原因。

关于java - 带有 Spring 和多用户的 Vaadin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12168632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com