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);
	    }
	}


 

你可能感兴趣的:(spring +vaadin spring ioc依赖注入)