Spring 中context:annotation-config、context:component-scan的区别
<context:annotation-config />是为了激活Spring所支持的一系列注解,诸如:Spring自带的@Required和@Autowired,JSR 250提供的 @PostConstruct, @PreDestroy and @Resource注解,JAX-WS提供的@WebServiceRef(如果可用),EJB 3的@EJB注解和JPA提供的@PersistenceContext 、@PersistenceUnit (如果可用)。
<context:component-scan>做了<context:annotation-config>要做的事情,还额外支持@Component,@Repository,@Service,@Controller @RestController,@ControllerAdvice, 和 @Configuration注解。<context:component-scan>扫描base-package指定的包,将标注了上述注解的类自动注册为Spring bean。极大的简化了Spring的配置,这也时Spring 注解配置大行其道的原因所在。Spring boot也在此基础之上建立起来的。
所以配置<context:component-scan>就不需要配置<context:annotation- config/>!

浙公网安备 33010602011771号