@ComponentScan 和 @Configuration

来源:互联网 发布:巨潮资讯 数据库接口 编辑:程序博客网 时间:2024/05/23 01:20

@ComponentScan 如果不设置basePackage的话 默认会扫描包的所有类,所以最好还是写上basePackage ,减少加载时间。默认扫描**/*.class路径 比如这个注解在com.wuhulala 下面 ,那么会扫描这个包下的所有类还有子包的所有类,比如com.wuhulala.service包的应用

@Configuration 表示这个类中定义了Bean,会把这个类中bean加载到spring容器中

@EnableAutoConfiguration springboot的注解 会在你开启某些功能的时候自动配置
,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用,并对Spring进行相应地设置。


我们通常建议您在其他类的根包中找到主应用程序类。@EnableAutoConfiguration 注解通常会放在 main class上, 这为特定的东西隐式的定义了一个基础的“search package”. 比如, 如果你在写一个 JPA application, 有使用了@EnableAutoConfiguration 注解的class的包 回去查找 @Entity 的items.


We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @EnableAutoConfiguration annotated class will be used to search for @Entity items.


@SpringBootApplication 相当于 @Configuration、@EnableAutoConfiguration 、 @ComponentScan 三个的作用

@ComponentScan@Configurationpublic class AppConfig {    @Bean    public Foo foo() {        return new Foo(bar());    }    @Bean    public Bar bar() {        return new Bar();    }}
2 0