现在的位置: 首页 > 综合 > 正文

Spring集成Vaadin

2013年10月30日 ⁄ 综合 ⁄ 共 4206字 ⁄ 字号 评论关闭

引言—— 范围与目的

    这篇文章将会向你展示SpringWeb应用程序中如何使用Vaadin

    在这里我们将会假定读者对SpringFramework有一个基本的了解和认识的。对于想了解更多Spring相关信息的,请参考官方网站或者Springin
Action
这本由CraigWalls编写的
优秀书籍。

Spring中设置

    先添加SpringFrameworkjar文件到你的项目里。如果你使用Maven来管理你的项目的依赖关系(而且是你必须使用它)。对于更多如何使用Maven2来集成Vaadin的相关信息,请参考Maven集成,添加那些条目到你的“pom.xml”里:

<dependencies>
  ...
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>2.0.3</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>2.0.3</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>2.0.3</version>
  </dependency>
</dependencies>

    如果你没有使用Maven2,只需要把所有相关jar文件添加到你的项目。

    编辑你的“web.xml”文件,添加Springlistener

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

    添加相关配置项到你的应用程序上下文(web.xml)文件里:

<web-app>
  ..
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
</web-app>

SpringContext Helper

    为了能访问由Spring管理的beans,你需要一个能胜任用以在Vaadin应用程序类中获取相关会话信息和Spring上下文的助手类:

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;

public class SpringContextHelper {

    private ApplicationContext context;

    public SpringContextHelper(Application application) {
        ServletContext servletContext = ((WebApplicationContext) application.getContext()).getHttpSession().getServletContext();
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    }

    public Object getBean(final String beanRef) {
        return context.getBean(beanRef);
    }    
}

使用它的简单例子

    一个十分基本的的例子,展现了你该在Vaadin应用程序中哪里去编写获得一个受管理的bean的代码

public class SpringHelloWorld extends com.vaadin.Application {

    public void init() {
        final Window main = new Window("Hello window");
        setMainWindow(main);
        
        SpringContextHelper helper = new SpringContextHelper(this);
        MyBeanInterface bean = (MyBeanInterface) helper.getBean("myBean");
        main.addComponent(new Label( bean.myMethod() ));
    }
}

另一个运用Spring2.5.x 使用的简单例子

    你也可以使用注解。确保AspectJ已经配置好。然后Spring切面AnnotationBeanConfigurerAspect将会帮助你完成所需的工作。

@Configurable(preConstruction = true)
public class SpringHelloWorld extends com.vaadin.Application {
    
    @Autowired
    private MyBeanInterface bean;

    public void init() {
        final Window main = new Window("Hello window");
        setMainWindow(main);        
        main.addComponent(new Label( bean.myMethod() ));
    }
}

Sprigcontext xml:

<!-- Turn on AspectJ @Configurable support -->
<context:spring-configured />

<context:component-scan base-package="foo" />

<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

Maven2pom.xml:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>2.5.5</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>2.5.5</version>
</dependency>

...

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <version>1.0</version>
  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.3</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjtools</artifactId>
      <version>1.6.3</version>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
    <goal>test-compile</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <aspectLibraries>
    <aspectLibrary>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      </aspectLibrary>
    </aspectLibraries>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
</plugin>

就这样现在Spring应该可以很完美地和Vaadin一起运作起来了。你可以使用像“mvntomcat:run”或者“mvnjetty:run”这样的指令来测试你的应用程序了。

其它例子

参见这个展示使用AutowiringApplicationServlet助手类论坛帖子

译者声明

翻译水平有限,敬请原谅。

若转载请注明原文译者yuanzhuohang与原文链接http://blog.csdn.net/yuanzhuohang/article/details/7277876

【上篇】
【下篇】

抱歉!评论已关闭.