spring boot完美集成dwr3.0.2-RELEASE

第一步:注册SpringDwrServlet到spring容器中

 

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * 注册SpringDwrServlet到spring容器中
 * 
 * @author 许畅
 * @since JDK1.7
 * @version 2017年11月28日 许畅 新建
 */
@Configuration
@ImportResource("classpath:dwr-spring-config.xml")
public class SpringBootDwrServletRegister {
    
    /**
     * @param springDwrServlet SpringDwrServlet
     * @return ServletRegistrationBean
     */
    @Bean
    public ServletRegistrationBean registDwrServlet(SpringDwrServlet springDwrServlet) {
        ServletRegistrationBean servletRegister = new ServletRegistrationBean(springDwrServlet, "/dwr/*");
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("debug", "true");
        servletRegister.setInitParameters(initParameters);
        return servletRegister;
    }
    
}

 

 

第二步:重写DwrSpringServlet使dwr代理的实例在spring容器中

 

import javax.servlet.ServletConfig;

import org.directwebremoting.impl.StartupUtil;
import org.directwebremoting.spring.DwrSpringServlet;
import org.directwebremoting.spring.SpringContainer;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * SpringDwrServlet
 * 
 * <pre>
 * 重写DwrSpringServlet使dwr代理的实例在spring容器中
 * </pre>
 * 
 * @author 许畅
 * @since JDK1.7
 * @version 2017年11月28日 许畅 新建
 */
@Component
public class SpringDwrServlet extends DwrSpringServlet {
    
    /**  */
    private static final long serialVersionUID = 1L;
    
    @Override
    protected SpringContainer createContainer(ServletConfig servletConfig) {
        ApplicationContext appContext = getApplicationContext(servletConfig.getServletContext());
        
        SpringDwrContainer springContainer = new SpringDwrContainer();
        springContainer.setBeanFactory(appContext);
        StartupUtil.setupDefaultContainer(springContainer, servletConfig);
        return springContainer;
    }
    
}

 

 

第三步:Spring版本dwr容器

 

import java.util.Map;

import org.directwebremoting.extend.ContainerConfigurationException;
import org.directwebremoting.spring.SpringContainer;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.util.ClassUtils;

/**
 * Spring版本dwr容器
 * 
 * @author 许畅
 * @since JDK1.7
 * @version 2017年11月28日 许畅 新建
 */
public class SpringDwrContainer extends SpringContainer {
    
    /**
     * 
     * @see org.directwebremoting.spring.SpringContainer#addParameter(java.lang.String, java.lang.Object)
     */
    @Override
    public void addParameter(String askFor, Object valueParam) throws ContainerConfigurationException {
        
        try {
            Class<?> clz = ClassUtils.forName(askFor, ClassUtils.getDefaultClassLoader());
            
            @SuppressWarnings("unchecked")
            Map<String, Object> beansOfType = (Map<String, Object>) ((ListableBeanFactory) beanFactory)
                .getBeansOfType(clz);
            
            if (beansOfType.isEmpty()) {
                super.addParameter(askFor, valueParam);
            } else if (beansOfType.size() > 1) {
                String key = StringUtil.uncapitalize(SpringDwrServlet.class.getSimpleName());
                if (beansOfType.containsKey(key)) {
                    beans.put(askFor, beansOfType.get(key));
                } else {
                    throw new ContainerConfigurationException("spring容器中无法找到对应servlet:" + key);
                }
            } else {
                beans.put(askFor, beansOfType.values().iterator().next());
            }
        } catch (ClassNotFoundException ex) {
            super.addParameter(askFor, valueParam);
        }
        
    }
}

 

 

第四步:容器扫描配置文件 dwr-spring-config.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
  
   <dwr:configuration />   
   <dwr:annotation-scan base-package="com.xxx" scanDataTransferObject="true" scanRemoteProxy="true"/>   
</beans>


总结: dwr这个框架后续人员维护的比较少,有些东西照着官网来还行不通,需要自己看源码重写,但是用起来还是比较方便的,

 

不清楚的自己看dwr源码,和查看spring boot官网和dwr官网

 

 

 

 

 

 

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页