使用注解将 Vaadin 6.7.3 和 Spring 3.0.5 整合 (一)

Vaadin、Spring 整合。

1.Spring配置

    (1).web.xml

	<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/spring-config.xml</paramvalue>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	 <!-- spring管理session作用域的bean -->
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>

 

2.将Vaadin 的 Application 纳入到 Spring 的 Context中进行管理;

   (1).web.xml

        <!--Vaadin 应用程序Servlet继承vaadin的Appliction-->
        <servlet>
		<servlet-name>Vaadinmanager Application</servlet-name>
		<servlet-class>com.iyspace.manager.SpringApplicationServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Vaadinmanager Application</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

    (2).com.iyspace.manager.SpringApplicationServlet 类继承自Vaadin的application类,过滤所有的请求并从Spring的ApplicationContext中取得应用的Application对象

    SpringApplicationServlet 源码

package com.iyspace.manager;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
@SuppressWarnings("serial")
public class SpringApplicationServlet extends AbstractApplicationServlet {

    /** Default application bean name in Spring application context. */
    private static final String DEFAULT_APP_BEAN_NAME = "application";

    /** Application bean name in Spring application context. */
    private String name;

    @Override
    public void init(ServletConfig config) throws ServletException {

	super.init(config);

	String name = config.getInitParameter("applicationBeanName");

	this.name = name == null ? DEFAULT_APP_BEAN_NAME : name;
    }

    @Override
    protected Application getNewApplication(HttpServletRequest request) throws ServletException {

	WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

	if (wac == null) {

	    throw new ServletException("Cannot get an handle on Spring's context. Is Spring running?"
		    + "Check there's an org.springframework.web.context.ContextLoaderListener configured.");
	}

	Object bean = wac.getBean(Application.class);

	if (!(bean instanceof Application)) {

	    throw new ServletException("Bean " + name + " is not of expected class Application");
	}

	return (Application) bean;
    }
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {

	WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

	if (wac == null) {

	    throw new ClassNotFoundException("Cannot get an handle on Spring's context. Is Spring running? "
		    + "Check there's an org.springframework.web.context.ContextLoaderListener configured.");
	}

	Object bean = wac.getBean(name);

	if (bean == null) {

	    throw new ClassNotFoundException("No application bean found under name " + name);
	}

	return (Class) bean.getClass();
    }
}

   (3)应用的Application类

    VaadinmanagerApplication 

@Component("application")
@Scope("session")
public class VaadinmanagerApplication extends Application{
       @Autowired
	public MainWindow mainWindow;  // Spring中管理存在在session作用域中
	
	public VaadinmanagerApplication(){
		System.out.println("application create");
	}
	
	@Override
	public void init() {
		this.mainWindow.showLoginPage();
		setMainWindow(this.mainWindow);
	}
}

现在就Vaadin和Spring就简单的整合到一起了,当有请求到来时SpringApplicationServlet 会判断如果是第一次请求(Session中没有VaadinmanagerApplication 对象)spring会创建该对象,若有该对象将直接使用。

 

以上的内容是google加自己摸索整理出来的如果有错误的地方欢迎指正

 

先整到这里,明天再写 Vaadin页面怎么使用Spring中的bean来加载数据。

 

 

   

 

你可能感兴趣的:(spring,3)