SpringBoot-自动装载

1,SpringBoot里面有内置的tomcat容器。

2,SpringBoot是基于已有的东西创建的新的东西。核心:(AutoConfiguration)自动装载,Starter,Actuator,SpringBoot CLI

3,SpringBoot本质上还是使用IOC。

4,剖析启动注解(SpringBootApplication)

从第二张图的可以看出SpringBootApplication是一个复合注解,里面有:

(1)ComponentScan注解:

  定义扫描@Controller,@Service,@Reponsitory,@Component的范围。

  在早期:<context:component-scan />使得带有上述注解的类被加载到spring容器中

(2)EnableAutoConfiguration注解:

  在以前,需要在applicationContext.xml中配置bean的信息

  在Spring3开始,他支持了JavaConfig(Java jdk5支持了注解):所谓的JavaConfig就是基于注解实现IOC容器的配置(实现Bean的管理)

  下面是配置bean托管的三种方式

  1)xml的方式    

  

  2)@Configuration注解的方式        

  

  3)@ComponentScan注解的方式(装载当前包下面使用了注解(@Controller,@Service,@Dao)的类)

   1.可以加载到同一包下加注解的类

  

  

   2.不同包下加载不到

  

  

  3.可以通过指定扫描的包解决上述问题

  @ComponentScan(basePackages = "com.example.demo")

   

   (4)@EnableAutoConfiguration

    @AutoConfigurationPackage
   @Import({AutoConfigurationImportSelector.class})
    1)@import注解
      如下图:在Demo2Main中获取容器中的bean会发现加载不到tempp包下的bean,想要加载指定的其他的bean,就可以使用import来实现

      

      @import 导入指定的bean

       

  (5)动态注入

  

5,RestController注解

这个注解也是一个复合注解:

 

 这也是为什么使用了RestController这个注解之后就不需要再写ResponseBody注解,Controller注解。

原文地址:https://www.cnblogs.com/excellencesy/p/11953561.html