1

使用示例时出现以下异常

嵌套异常是 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 source官方网站上找到。它说设备是一个接口,但我只使用了它的实现类。基本上我认为它是dispatcher servlet中的一个错误。 xml

4

4 回答 4

4

尝试使用设备过滤器:

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

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

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

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

于 2013-05-28T14:42:44.283 回答
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>
于 2015-07-27T12:37:04.170 回答
0

您必须为此添加参数解析器。按照这个

如果您想将当前设备作为参数传递给您的 @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());
}
于 2018-08-28T16:40:38.940 回答
0

除了 leogps 答案,以下是 Java Config

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

     @Override
     public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add(deviceHandlerMethodArgumentResolver());
        }
于 2017-11-28T05:11:22.523 回答