annotation-config与annotation-driven与component-scan

 1、<context:annotation-config/><mvc:annotation-driven/>

<context:annotation-config/>这个配置其实是Spring里的配置,此配置可以扫描的注解为: 

Spring@Required@Autowired

JSR 250@PostConstruct@PreDestroy@Resource

JAX-WS@WebServiceRef

EJB3@EJB

JPA@PersistenceContext@PersistenceUnit

此配置不包含Spring@TransactionalEJB3@TransactionAttribute,如果有需要请使用

<tx:annotation-driven>

<mvc:annotation-driven/>这个配置是Spring-mvc里的配置,作用是向Spring容器注册HandlerAdapter实现类。它是处理请求的适配器。这个配置是作用于Controller的。<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。


2.<context:annotation-config> <context:component-scan>

<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解,是一个注解处理工具。

<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 

<context:annotation-config />仅能够在已经在已经注册过的bean上面起作用。对于没有在spring容器中注册的bean,它并不能执行任何操作。<context:component-scan>除了具有<context:annotation-config />的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。context:annotation-config /> <context:component-scan>同时存在的时候,前者会被忽略。也就是那些@autowire@resource等注入注解只会被注入一次

猜你喜欢

转载自blog.csdn.net/u013789656/article/details/80927978