Spring移动中的Bean创建异常

时间:2013-05-28 07:54:07

标签: spring spring-mobile

使用sample

时,我收到以下异常

嵌套异常是 具有根本原因的org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.mobile.device.Device]: Specified class is an interface]

我的控制器如下:

package com.srccodes.spring.controller;

import java.util.logging.Logger;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.site.SitePreference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Abhijit Ghosh
 * @version 1.0
 */
@Controller
public class SpringMobileHelloController {
    private static final Logger logger = Logger.getLogger(SpringMobileHelloController.class.getName());

    @RequestMapping("/")
    public String sayHello(SitePreference sitePreference, Device device, Model model) {
        logger.info("SitePreference : " + sitePreference);
        logger.info("Device : " + device);

        model.addAttribute("message", "Hello World!");

        return "helloWorld";
    }
}


My dispatcher-servlet.xml is:

 (some parts omitted)

    <context:component-scan base-package="com.srccodes.spring.controller" />

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean            class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />            
            <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <mvc:interceptors>
        <!-- Resolve the device which has generated the request -->
        <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />

        <!-- User's site preference -->
        <bean class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />

        <!-- Redirects users to the device specific site -->
        <bean class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor" factory-method="urlPath">
            <constructor-arg value="/m" />
            <constructor-arg value="/t" />
            <constructor-arg value="/SpringMobileHelloWorld" />
        </bean>
    </mvc:interceptors>


    <!-- Device aware view resolving -->
    <bean id="liteDeviceDelegatingViewResolver" class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
        <constructor-arg>
            <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/pages/" />
                <property name="suffix" value=".jsp" />
            </bean>
        </constructor-arg>

        <property name="mobilePrefix" value="m/" />
        <property name="tabletPrefix" value="t/" />
        <property name="enableFallback" value="true" />
    </bean>

</beans>

请帮助我,因为我是spring mobile的初学者。这个例子可以在spring源官方网站上找到。它说设备是一个接口,但我只使用它的实现类。基本上我认为它是一个错误调度程序servlet.xml

3 个答案:

答案 0 :(得分:4)

尝试使用设备过滤器:

    <filter>
        <filter-name>deviceResolverRequestFilter</filter-name>
        <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
    </filter>

无论如何,您可以使用DeviceUtilsHttpServletRequest检索设备,就像这个处理程序示例一样..:

public boolean preHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2) throws Exception {
        Device device = DeviceUtils.getCurrentDevice(request);
...
}

我有同样的问题,用第二种方法解决它。

答案 1 :(得分:2)

检查其他地方是否未定义<mvc:annotation-driven/>。 只需添加:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />

        <bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

答案 2 :(得分:0)

您必须为此添加参数解析器。 跟随this

如果您想将当前设备作为参数传递给@Controller方法之一,请配置DeviceWebArgumentResolver:

<annotation-driven>
  <argument-resolvers>
    <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
  </argument-resolvers>
</annotation-driven>

您也可以使用基于Java的配置来配置DeviceHandlerMethodArgumentResolver:

@Bean
public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() {
    return new DeviceHandlerMethodArgumentResolver();
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    argumentResolvers.add(deviceHandlerMethodArgumentResolver());
}