【SpringBoot】springboot的启动初步理解

springboot的启动初步理解

我们会发现开发一个Spring Boot,都会有一个注解@SpringBootApplication和一个类定义SpringApplication.run,点击源码可以查看到如下代码:

复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
....
}

其实这里最重要的是以下三个注解:

@SpringBootConfiguration 该注解点开看里面是引入了@Configuration

@EnableAutoConfiguration 开启springboot的注解功能

@ComponentScan 扫描路径设置

其实我们的程序启动类,可以使用以下三个注解依然可以启动 @Configuration @EnableAutoConfiguration @ComponentScan,但是我们通常都是使用@SpringBootApplication

在启动类里面标注了@Configuration,意味着它其实也是一个 ioc容器的配置类,任何一个标注了@Configuration的Java类定义都是一个JavaConfig配置类。

任何一个标注了@Bean的方法,其返回值将作为一个bean定义注册到Spring的IOC容器,方法名将默认成该bean定义的id。

基于JavaConfig的配置形式是这样的:

@Configuration

public class TestConfiguration{

@Bean

public TestService testService(){

return new TestServiceImpl(tempService());

}

@Bean

public TempService tempService(){

return new TempServiceImpl();

}

}

@ComponentScan

@ComponentScan的功能其实就是自动扫描并加载符合条件的组件(比如@Component和@Repository等)或者bean定义,最终将这些bean定义加载到IoC容器中。

我们可以通过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package进行扫描。

@ComponentScan:

ComponentScan 默认会扫描当前 package 下的的所有加

了@Component 、@Repository、@Service、@Controller的类到 IoC 容器中;

相关推荐
华仔啊3 分钟前
千万级大表如何新增字段?别再直接 ALTER 了
后端·mysql
IT_陈寒7 分钟前
Python开发者必看!10个高效数据处理技巧让你的Pandas代码提速300%
前端·人工智能·后端
量子物理学10 分钟前
Eclipse Mosquitto 在小内存下怎么修改配置文件
java·服务器·eclipse
程序员鱼皮22 分钟前
让老弟做个数据同步,结果踩了 7 个大坑!
java·后端·计算机·程序员·编程·职场
麦兜*31 分钟前
Redis 7.2 新特性实战:Client-Side Caching(客户端缓存)如何大幅降低延迟?
数据库·spring boot·redis·spring·spring cloud·缓存·tomcat
Iris76131 分钟前
MyBatis一对多关系映射方式
java
程序员清风31 分钟前
滴滴二面:MySQL执行计划中,Key有值,还是很慢怎么办?
java·后端·面试
白鲸开源32 分钟前
3.1.8<3.2.0<3.3.1,Apache DolphinScheduler集群升级避坑指南
java·开源·github
huohaiyu1 小时前
synchronized (Java)
java·开发语言·安全·synchronized
梵得儿SHI1 小时前
Java 工具类详解:Arrays、Collections、Objects 一篇通关
java·工具类·collections·arrays·objects