将 spring 与 vaadin/gwt 集成时使用了哪些设计模式?
与上面提供的内容略有不同。它对我有用,而且非常简单,所以我把它贴在这里。
我在 web.xml 中添加了以下内容,并使用了对依赖项的 spring-context 的精确类路径引用(更改路径以满足您的需要):
<!-- Spring context loader -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/company/appserv/spring/spring-context.xml</param-value>
</context-param>
以上假设您已将 spring-web 添加到您的 pom.xml,因此请务必这样做。您还需要 servlet-api。因此,您将在 pom 中添加以下内容,调整版本以满足您的需求:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
现在,创建以下类:
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SpringContextHelper {
private ApplicationContext context;
private static Logger logger = LoggerFactory.getLogger(SpringContextHelper.class);
public SpringContextHelper(Application application) {
ServletContext servletContext = ((WebApplicationContext) application.getContext()).getHttpSession().getServletContext();
context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
logger.debug("Number of beans: {}",context.getBeanDefinitionCount());
}
public Object getBean(final String beanRef) {
return context.getBean(beanRef);
}
}
现在创建您的 DAO 类(如之前的帖子中所建议的那样):
import com.company.appserv.dbo.DatabaseHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.Application;
public class DAO {
private static DatabaseHelper databaseHelper;
private static Logger logger = LoggerFactory.getLogger(DAO.class);
public static final void initialize(Application application) {
logger.debug("DAO initializing...");
if (databaseHelper == null) {
logger.debug("Creating databaseHelper...");
SpringContextHelper helper = new SpringContextHelper(application);
databaseHelper = (DatabaseHelper)helper.getBean("databaseHelper");
}
}
public static DatabaseHelper getDatabaseHelper() {
return databaseHelper;
}
}
最后,请务必初始化您现在将在各种视图类中使用的 DAO。这个初始化将发生在你扩展Application的任何类中,如下所示:
import com.vaadin.Application;
import com.vaadin.ui.Window;
/**
* The Application's "main" class
*
* Vaadin framework associates requests with sessions so that
* an application class instance is really a session object.
*/
@SuppressWarnings("serial")
public class VoucherTool extends Application
{
private Window mainWindow;
@Override
public void init() {
mainWindow = new Window("Application");
setMainWindow(mainWindow);
// Needed because composites are full size
mainWindow.getContent().setSizeFull();
LoginView myComposite = new LoginView();
mainWindow.addComponent(myComposite);
setTheme("applicationtheme");
DAO.initialize(this);
}
}