Spring Boot中隐藏的<context:annotation-config>在哪里?

时间:2019-01-24 08:18:57

标签: spring spring-boot configuration annotations

要在Spring中通过注释工作,需要定义:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    xmlns:context="http://www.springframework.org/schema/context">

    <context:annotation-config/>
</beans>

  

annotation-config.xml

但是我创建了一个最简单的Spring boot Application(假设我在initialazr中选择了lust Web)

它适用于注释,但是那里没有任何comment-config.xml,也没有提及, 隐藏在哪里?

1 个答案:

答案 0 :(得分:3)

使用不支持即用注释的ApplicationContext实现时,您仅需要添加<context:annotation-config /><context:component-scan />(这意味着注释驱动的配置)。

仅使用基于XML的配置时,您还可以使用一种启用XML的ApplicationContext实现,通常是XmlWebApplicationContext。有了这些,您将需要指示ApplicationContext启用注释处理。

使用基于Java的配置时,通常使用基于注释的ApplicationContext,默认值为AnnotationConfigWebApplicationContext。由于其处理Java配置类的性质,因此默认情况下启用了注释处理。

Spring Boot使用后者(实际上,它为此使用了专门的子类)。因此,您无需显式启用它。

相关问题