spring +vaadin spring ioc依赖注入

package com.xq.vaadin;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class SimpleAddressBook extends AbstractApplicationServlet  {
	private Class<? extends Application> clazz;
	@Override
	public void init(ServletConfig config) throws ServletException{
		super.init(config);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(
		config.getServletContext());
		ResourceBundleMessageSource messageSource=(ResourceBundleMessageSource)wac.getBean(ResourceBundleMessageSource.class);
		System.out.println(messageSource);
		com.xq.controller.HelloWorld application = (com.xq.controller.HelloWorld) wac.getBean(com.xq.controller.HelloWorld.class);
		
		clazz = application.getClass();
	}
	@Override
	protected com.xq.controller.HelloWorld getNewApplication(HttpServletRequest request)
			throws ServletException {
		// TODO Auto-generated method stub
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		return (com.xq.controller.HelloWorld) wac.getBean(com.xq.controller.HelloWorld.class);
	}
	@Override
	protected Class<? extends Application> getApplicationClass()
			throws ClassNotFoundException {
		// TODO Auto-generated method stub
		return clazz;
	}
	
}

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());return (com.xq.controller.HelloWorld) wac.getBean(com.xq.controller.HelloWorld.class);

这个是通过类去找bean.

在web。xml中

    <servlet>
    <servlet-name>HelloWorde</servlet-name>
    <servlet-class>com.xq.vaadin.SimpleAddressBook</servlet-class>
    <init-param>
      <param-name>application</param-name>
      <param-value>com.xq.vaadin.HelloWorld</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorde</servlet-name>
    <url-pattern>/VAADIN2/*</url-pattern>
  </servlet-mapping>

spring 中国际化,还有vaadin的页面配置:

	<bean id="messageSource2" name="messageSource2"  class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="message-info"></property>
		<property name="useCodeAsDefaultMessage" value="true"></property>
	</bean>
	<bean id="test" name="test" class="com.xq.controller.HelloWorld"></bean>

package com.xq.util;
import javax.servlet.ServletContext;

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

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
public class SpringContext {

	    private ApplicationContext context;

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

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


 

package com.xq.util;
import javax.servlet.ServletContext;

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

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
public class SpringContext {

	    private ApplicationContext context;

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

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


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值