vaadin+spring

 

Introduction-ScopeandPurpose#

This article will show you how to use Spring in a web application relying on Vaadin.

It is assumed that the readers have a basic knowledge of the Spring Framework. For more information about Spring, refer to theofficial web site or to the excellent bookSpring in Action, by Craig Walls.

Springsetup#

Add the Spring framework jar files to your project. If you are using Maven to manage your project dependencies (and you should ;) for more information about how to use integrate Vaadin with Maven2, refer toMaven Integration) add those entries to your'pom.xml':

#!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>

If you are not using Maven2, just add all the relevant jar files to your project.

Edit your 'web.xml' to add the Spring listener:

#!xml

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

And add the relevant information about your application context files :

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

SpringContextHelperclass#

To access your Spring managed beans, you will need a helper class capable of using your Vaadin Application class to get the relevant session information, and thus the Spring context:

#!java
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);
    }    
}

SimpleUsageExample#

A very basic example, where you get a bean managed in Spring in the Vaadin Application:

#!java
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() ));
    }
}

MoreSimpleExamplewithSpring2.5.x#

You could also use annotations. Make sure AspectJ is configured. Then spring aspectAnnotationBeanConfigurerAspect do the job for you.

#!java
@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() ));
    }
}

Spring context xml:

#!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" />

Maven2 pom.xml:

#!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>

That's all now Spring should work nicely with Vaadin. You can test your application for example with mvn tomcat:run or mvn jetty:run command.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值