ComponentScan 和 Configuration

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

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

@Configuration 表示这个类是一个spring 配置类,一般这里面会定义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();    }}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/trigfj/article/details/84195731
今日推荐