<context:annotation-config/>和<mvc:annotation-driven/>及解决No mapping found for HTTP request with URI [/role/getRole] in DispatcherServlet with name 'springmvc-config'

1:什么时候使用<context:annotation-config>

当你使用@Autowired,@Required,@Resource,@PostConstruct,@PreDestroy等注解时会选择使用,

但也可以手动配置Bean来使用这些注解,但是会使spring的配置文件比较笨拙

一般都会使用<context:annotation-config>,由spring提供,隐式的向容器注册了AutowiredAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor这四个bean。

但是使用注解的时候,我们都会用<xontext:component-scan/>来将bean扫描进容器,此时,可以将<context:annotation-config/>省去

2:什么时候使用<mvc:annotation-drive/n>

当使用了@Controller时

而且是必须使用,如果不使用的话,org.Springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器

<mvc:annotation-driven/>注册了DefaultAnnotationHandleMapping和AnnotationMethodHandleAdapter两个bean(该两个bean解决了@Controller注解的使用前提配置)

我之前是一个了@Controller注解而没有使用<mvc:annotation-driven/>

报错:

 No mapping found for HTTP request with URI [/role/getRole] in DispatcherServlet with name 'springmvc-config'
解决:添加<mvc:annotation-driven/>且必须放在配置文件的第一行
原文地址:https://www.cnblogs.com/Hdaydayup/p/6622812.html