我想在spring中为我的网站添加rss源代码,最后一步是配置CustomRssView的Bean。
但是教程使用xml来配置它。我希望使用@Bean注释。但是,它无法解析视图名称。
教程中的xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.mkyong.common.controller" />
<!-- Map returned view name "rssViewer" to bean id "rssViewer" -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="rssViewer" class="com.mkyong.common.rss.CustomRssViewer" />
我的bean配置
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
@Bean
public RssView rssView() {
return new RssView();
}
}
注册成功,但BeanNameViewResolver无法解析视图名称。
一些调试信息
2015-09-08 16:02:13.103 TRACE 21367 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [getRssFeed] returned [ModelAndView: reference to view with name 'rssView'; model is {rss=[Post(id=0, menuId=0, submenuId=0, date=null, author=admin, title=Spring MVC Tutorial 1, content=Tutorial 1 Content ...), Post(id=0, menuId=0, submenuId=0, date=null, author=system, title=Spring MVC Tutorial 2, content=Tutorial 2 Content ...)]}]
2015-09-08 16:02:13.103 TRACE 21367 --- [nio-8080-exec-1] HandlerMethodReturnValueHandlerComposite : Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler@46a550ec] supports [class org.springframework.web.servlet.ModelAndView]
2015-09-08 16:02:13.111 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2015-09-08 16:02:13.111 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'rssView'
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Invoking afterPropertiesSet() on bean with name 'rssView'
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Invoking afterPropertiesSet() on bean with name 'rssView'
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2015-09-08 16:02:13.113 TRACE 21367 --- [nio-8080-exec-1] o.s.w.s.v.InternalResourceViewResolver : Cached view [rssView]
2015-09-08 16:02:13.114 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'rssView'; URL [rssView]] based on requested media type 'text/html'
2015-09-08 16:02:13.114 DEBUG 21367 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'rssView'; URL [rssView]] in DispatcherServlet with name 'dispatcherServlet'
2015-09-08 16:02:13.114 TRACE 21367 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Rendering view with name 'rssView' with model {rss=[Post(id=0, menuId=0, submenuId=0, date=null, author=admin, title=Spring MVC Tutorial 1, content=Tutorial 1 Content ...), Post(id=0, menuId=0, submenuId=0, date=null, author=system, title=Spring MVC Tutorial 2, content=Tutorial 2 Content ...)]} and static attributes {}
2015-09-08 16:02:13.114 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Added model object 'rss' of type [java.util.ArrayList] to request in view with name 'rssView'
2015-09-08 16:02:13.116 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [rssView] in InternalResourceView 'rssView'
2015-09-08 16:02:13.125 TRACE 21367 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Bound request context to thread: org.apache.catalina.core.ApplicationHttpRequest@41e5804f
2015-09-08 16:02:13.125 DEBUG 21367 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet
如何更改我的代码。