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/>
報錯:
