vaadin的spring 整合的例子

 
/*
3 * Copyright (C) 2010 Archie L. Cobbs. All rights reserved.
4 *
5 * $Id: AutowiringApplicationServlet.java 19 2010-02-04 18:09:30Z archie $
6 */
7 
8package com.example;
9 
10import com.vaadin.Application;
11import com.vaadin.terminal.gwt.server.ApplicationServlet;
12 
13import javax.servlet.ServletConfig;
14import javax.servlet.ServletException;
15import javax.servlet.http.HttpServletRequest;
16 
17import org.springframework.beans.BeansException;
18import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
19import org.springframework.web.context.WebApplicationContext;
20import org.springframework.web.context.support.WebApplicationContextUtils;
21 
22/**
23 * {@link ApplicationServlet} that autowires and configures the {@link Application}
24 * objects it creates using the associated Spring {@link WebApplicationContext}.
25 * This allows a Vaadin application to be configured normally as a Spring bean.
26 *
27 * <p>
28 * For example, annotations such as
29 * <code>{@link org.springframework.beans.factory.annotation.Autowired @Autowired}</code>,
30 * <code>{@link org.springframework.beans.factory.annotation.Required @Required}</code>, etc.
31 * and interfaces such as {@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware},
32 * etc. will work on your {@link Application} instances.
33 * </p>
34 *
35 * <p>
36 * An example of "direct" use of this servlet in conjunction with Spring's
37 * {@link org.springframework.web.context.ContextLoaderListener ContextLoaderListener}:
38 * <blockquote><pre>
39 *  &lt;!-- Spring context loader --&gt;
40 *  &lt;listener&gt;
41 *      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
42 *  &lt;/listener&gt;
43 *
44 *  &lt;!-- Vaadin servlet --&gt;
45 *  &lt;servlet&gt;
46 *      &lt;servlet-name&gt;myapp&lt;/servlet-name&gt;
47 *      &lt;servlet-class&gt;com.example.AutowiringApplicationServlet&lt;/servlet-class&gt;
48 *      &lt;init-param&gt;
49 *          &lt;param-name&gt;application&lt;/param-name&gt;
50 *          &lt;param-value&gt;some.spring.configured.Application&lt;/param-value&gt;
51 *      &lt;/init-param&gt;
52 *      &lt;init-param&gt;
53 *          &lt;param-name&gt;productionMode&lt;/param-name&gt;
54 *          &lt;param-value&gt;true&lt;/param-value&gt;
55 *      &lt;/init-param&gt;
56 *  &lt;/servlet&gt;
57 *  &lt;servlet-mapping&gt;
58 *      &lt;servlet-name&gt;myapp&lt;/servlet-name&gt;
59 *      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
60 *  &lt;/servlet-mapping&gt;
61 * </pre></blockquote>
62 * </p>
63 *
64 * <p>
65 * An example that creates a Spring MVC "controller" bean for use with Spring's
66 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}:
67 * <blockquote><pre>
68 *  &lt;!-- Activate Spring annotation support --&gt;
69 *  &lt;context:annotation-config/&gt;
70 *
71 *  &lt;!-- Define controller bean for Vaadin application --&gt;
72 *  &lt;bean id="applicationController" class="org.springframework.web.servlet.mvc.ServletWrappingController"
73 *     p:servletClass="com.example.AutowiringApplicationServlet"&gt;
74 *      &lt;property name="initParameters"&gt;
75 *          &lt;props&gt;
76 *              &lt;prop key="application"&gt;some.spring.configured.Application&lt;/prop&gt;
77 *              &lt;prop key="productionMode"&gt;true&lt;/prop&gt;
78 *          &lt;/props&gt;
79 *      &lt;/property&gt;
80 *  &lt;/bean&gt;
81 * </pre></blockquote>
82 *
83 * @see org.springframework.web.servlet.mvc.ContextLoaderListener
84 * @see org.springframework.web.servlet.DispatcherServlet
85 * @see org.springframework.web.servlet.mvc.ServletWrappingController
86 * @see AutowireCapableBeanFactory
87 */
88public class AutowiringApplicationServlet extends ApplicationServlet {
89 
90    private WebApplicationContext webApplicationContext;
91 
92    /**
93     * Initialize this servlet.
94     *
95     * @throws ServletException if there is no {@link WebApplicationContext} associated with this servlet's context
96     */
97    @Override
98    public void init(ServletConfig config) throws ServletException {
99        super.init(config);
100        try {
101            this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
102        } catch (IllegalStateException e) {
103            throw new ServletException("could not locate containing WebApplicationContext");
104        }
105    }
106 
107    /**
108     * Get the containing Spring {@link WebApplicationContext}.
109     * This only works after the servlet has been initialized (via {@link #init init()}).
110     *
111     * @throws ServletException if the operation fails
112     */
113    protected final WebApplicationContext getWebApplicationContext() throws ServletException {
114        if (this.webApplicationContext == null)
115            throw new ServletException("can't retrieve WebApplicationContext before init() is invoked");
116        return this.webApplicationContext;
117    }
118 
119    /**
120     * Get the {@link AutowireCapableBeanFactory} associated with the containing Spring {@link WebApplicationContext}.
121     * This only works after the servlet has been initialized (via {@link #init init()}).
122     *
123     * @throws ServletException if the operation fails
124     */
125    protected final AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws ServletException {
126        try {
127            return getWebApplicationContext().getAutowireCapableBeanFactory();
128        } catch (IllegalStateException e) {
129            throw new ServletException("containing context " + getWebApplicationContext() + " is not autowire-capable", e);
130        }
131    }
132 
133    /**
134     * Create and configure a new instance of the configured application class.
135     *
136     * <p>
137     * The implementation in {@link AutowiringApplicationServlet} delegates to
138     * {@link #getAutowireCapableBeanFactory getAutowireCapableBeanFactory()}, then invokes
139     * {@link AutowireCapableBeanFactory#createBean AutowireCapableBeanFactory.createBean()}
140     * using the configured {@link Application} class.
141     * </p>
142     *
143     * @param request the triggering {@link HttpServletRequest}
144     * @throws ServletException if creation or autowiring fails
145     */
146    @Override
147    protected Application getNewApplication(HttpServletRequest request) throws ServletException {
148        Class<? extends Application> cl = getApplicationClass();
149        AutowireCapableBeanFactory beanFactory = getAutowireCapableBeanFactory();
150        try {
151            return beanFactory.createBean(cl);
152        } catch (BeansException e) {
153            throw new ServletException("failed to create new instance of " + cl, e);
154        }
155    }
156}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值