2012-07-13 73 views
0

我可以在XML中輕鬆配置,但不能在@Configuration中配置。如何將屬性文件中的spring 3.1 classNameString轉換爲類?

屬性

spring.view.class=org.springframework.web.servlet.view.JstlView 
spring.view.prefix=/WEB-INF/jsp/ 
spring.view.suffix=.jsp 

代碼

@Bean 
public UrlBasedViewResolver viewResolver() throws ClassNotFoundException { 
    //ViewResolver Setting 
    Class viewResolverClass = env.getPropertyAsClass("spring.view.class", Class.class); 
    String viewResolverPrefix = env.getProperty("spring.view.prefix"); 
    String viewResolverSuffix = env.getProperty("spring.view.suffix"); 
    UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); 
    viewResolver.setViewClass(viewResolverClass); 
    viewResolver.setPrefix(viewResolverPrefix); 
    viewResolver.setSuffix(viewResolverSuffix); 
    return viewResolver; 
} 

錯誤

throws java.lang.ClassNotFoundException] threw exception; nested exception is java.lang.IllegalArgumentException: Cannot convert value [org.springframework.web.servlet.view.JstlView] from source type [String] to target type [Class] 

在此先感謝。

回答

0

錯誤消息太明顯了,並說所有:

無法轉換值...從源類型[字符串]爲目標類型[等級]

所以,你應該手動轉換StringClass是這樣的:

viewResolver.setViewClass(Class.forName(viewResolverClass)); 
相關問題