Announcement Announcement Module
Collapse
No announcement yet.
Could not instantiate bean class [org.springframework.mobile.device.Device] Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Could not instantiate bean class [org.springframework.mobile.device.Device]

    I am trying to add Spring Mobile to my current Spring MVC Project but I keep getting the following error:

    Code:
        org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.mobile.device.Device]: Specified class is an interface
        	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
        	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        
        root cause
        
        org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.mobile.device.Device]: Specified class is an interface
        	org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
        	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:136)
        	org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
        	org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:102)
        	org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
        	org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
        	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
        	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
        	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
        	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
        	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
        	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    Here is my controller:

    Code:
     @Controller
        @RequestMapping("/ask")
        class IndexController {
        
        	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
        
            @RequestMapping(method = RequestMethod.GET)
            public String displayRequestPage(Device device, SitePreference sitePreference) {
        
                logger.info("SitePreference : " + sitePreference);
                logger.info("Device : " + device);
        
                String page = "index";
        
                if (device.isMobile()) {
                    logger.info("Hello mobile user!");
                    page = page  + "-mobile";
                } else if (device.isTablet()) {
                    logger.info("Hello tablet user!");
                    page = page  + "-tablet";
                } else {
                    logger.info("Hello desktop user!");
                }
                return "index";
             }
        
        
        }

    and here is my WebConfig:

    Code:
     @Configuration
        @EnableWebMvc
        @ComponentScan(basePackages= {"com.johnathanmsmith.mvc.web"})
        public class WebMVCConfig extends WebMvcConfigurerAdapter {
        
        	private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";
        
        	private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);
        
        	@Bean
            public  ViewResolver resolver() {
                UrlBasedViewResolver url = new UrlBasedViewResolver();
                url.setPrefix("views/");
                url.setViewClass(JstlView.class);
                url.setSuffix(".jsp");
                return url;
            }
        
            @Bean
            public LiteDeviceDelegatingViewResolver liteDeviceAwareViewResolver() {
                InternalResourceViewResolver delegate = new InternalResourceViewResolver();
                delegate.setPrefix("views/");
                delegate.setSuffix(".jsp");
                LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(delegate);
                resolver.setMobilePrefix("mobile/");
                resolver.setTabletPrefix("tablet/");
                return resolver;
            }
        
        	@Bean(name = "messageSource")
        	public MessageSource configureMessageSource() {
        		logger.debug("setting up message source");
        		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        		messageSource.setBasename(MESSAGE_SOURCE);
        		messageSource.setCacheSeconds(5);
        		messageSource.setDefaultEncoding("UTF-8");
        		return messageSource;
        	}
        
        	@Bean
        	public LocaleResolver localeResolver() {
        		SessionLocaleResolver lr = new org.springframework.web.servlet.i18n.SessionLocaleResolver();
        		lr.setDefaultLocale(Locale.ENGLISH);
        		return lr;
        	}
        
        	@Override
        	public void addResourceHandlers(ResourceHandlerRegistry registry) {
        		logger.debug("setting up resource handlers");
        		registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
        	}
        
        	@Override
        	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        		logger.debug("configureDefaultServletHandling");
        		configurer.enable();
        	}
        
        	@Override
        	public void addInterceptors(final InterceptorRegistry registry) {
        		registry.addInterceptor(new LocaleChangeInterceptor());
                registry.addInterceptor(new DeviceResolverHandlerInterceptor());
        	}
        
        	@Bean
        	public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        		SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
        
        		Properties mappings = new Properties();
        		mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
        		mappings.put("org.springframework.dao.DataAccessException",	"dataAccessFailure");
        		mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
        		b.setExceptionMappings(mappings);
        		return b;
        	}
        }
    I dont know what it can be.. If you would like to clone the project I have it on github at
    [email protected]:JohnathanSmith/springmvc-mobile.git

  • #2
    try with one viewResolver, add filter and addArgumentResolvers method
    Code:
    @Bean
    	public LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver(){
    		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    		resolver.setPrefix("/WEB-INF/views/");
    		resolver.setSuffix(".jsp");
    
    		//directorios
    		LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = new LiteDeviceDelegatingViewResolver(resolver);
    		liteDeviceDelegatingViewResolver.setMobilePrefix("mobile/");
    		liteDeviceDelegatingViewResolver.setTabletPrefix("tablet/");
    		liteDeviceDelegatingViewResolver.setEnableFallback(true);
    		return liteDeviceDelegatingViewResolver;
    	}
    
    	@Override
    	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    		argumentResolvers.add(new ServletWebArgumentResolverAdapter(new DeviceWebArgumentResolver()));
    	}
    
    filter in web.xml
    
    <filter>
    		<filter-name>deviceResolverRequestFilter</filter-name>
    		<filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
    	</filter>
    https://github.com/jbovet/demo-springmobile.git

    Comment

    Working...
    X