spring+vaadin的一个记录

public class SimpleAddressBook extends AbstractApplicationServlet  {
	@Resource(name="messageSource")
	private ResourceBundleMessageSource messageSource;
	private Class<? extends Application> clazz;
	@Override
	public void init(ServletConfig config) throws ServletException{
		super.init(config);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(
		config.getServletContext());
		Application application = (Application) wac.getBean("application", Application.class);
		clazz = application.getClass();
	}
	@Override
	protected Application getNewApplication(HttpServletRequest request)
			throws ServletException {
		// TODO Auto-generated method stub
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		return (Application) wac.getBean("application", Application.class);
	}
	@Override
	protected Class<? extends Application> getApplicationClass()
			throws ClassNotFoundException {
		// TODO Auto-generated method stub
		return clazz;
	}
	
}
<?xml version="1.0" encoding="UTF-8"?> 
<web-app...> 
... 
  <servlet> 
      <servlet-name>Vaadin servlet</servlet-name> 
      <servlet-class>ch.frankel.blog.vaadin.SpringVaadinServlet </servlet-class> 
      <init-param> 
       <param-name>application</param-name> 
      <param-value>ch.frankel.blog.vaadin.Application</param-value> 
      </init-param> 
  </servlet> 
  <servlet-mapping> 
      <servlet-name>Vaadin servlet</servlet-name> 
      <url-pattern>/*</url-pattern> 
  </servlet-mapping> 
</web-app>


 

 

转帖过来:


 

I lately became interested in Vaadin, another web framework but where everything is done on the server side: no need for developers to learn HTML, CSS nor JavaScript. Since Vaadin adress my remarks about web applications being to expensive because of a constant need of well-rounded developers, I dug a little deeper: it will probably be the subject of another post.

Anyway, i became a little disappointed when I wanted to use my favourite Dependency Injection framework, namely Spring, in Vaadin. After a little Google research, I found the Vaadin Wiki and more preciselythe page talking about Vaadin Spring Integration. It exposes two ways to integrate Spring in Vaadin.

The first one uses the Helper “pattern”, a class with static method that has access to the Spring application context. IMHO, those Helper classes should be forgotten now we have DI since they completely defeat its purpose. If you need to explicitly call the Helper static method in order to get the bean, where’s the Inversion of Control?

The second solution uses Spring proprietary annotation @Autowired in order to use DI. Since IoC is all about decoupling, I’m vehemently opposed to coupling my code to the Spring framework.

Since neither option seemed viable to me, let me present you the one I imagined: it is very simple and consists of subclassing the Vaadin’sAbstractApplicationServlet and using it instead of the classicalApplicationServlet.

  
  
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class SpringVaadinServlet extends AbstractApplicationServlet {
/** Class serial version unique identifier. */
private static final long serialVersionUID = 1L;
private Class clazz;
@Override
public void init(ServletConfig config) throws ServletException {
super .init(config);
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(
config.getServletContext());
Application application = (Application) wac.getBean( "application" , Application. class );
clazz = application.getClass();
}
/**
* Gets the application from the Spring context.
*
* @return The Spring bean named 'application'
*/
@Override
protected Application getNewApplication(HttpServletRequest request)
throws ServletException {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(
request.getSession().getServletContext());
return (Application) wac.getBean( "application" , Application. class );
}
/**
* @see com.vaadin.terminal.gwt.server.AbstractApplicationServlet#getApplicationClass()
*/
@Override
protected Class getApplicationClass()
throws ClassNotFoundException {
return clazz;
}
}

This solution is concise and elegant (according to me). Its only drawback is that it couples the servlet to Spring. But a class-localized coupling is of no consequesence and perfectly acceptable. Morevoer, this lets you use Spring autowiring mechanism, JSR 250 autowiring or plain old XML explicit wiring.

http://blog.frankel.ch/vaadin-spring-integration

此高手的博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值