【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 容器中;

相关推荐
摇滚侠2 小时前
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 使用 JdbcTemplate 阅读笔记 32
spring boot·笔记·后端
phltxy2 小时前
C语言操作符详解
java·c语言·算法
步行cgn3 小时前
@Configuration 详解:Spring 配置类的核心注解
java·后端·spring
sunshine22 girl3 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
嘿嘿-665 小时前
Windows 一键使用 GPT-6 Astra:Codex CLI 配置教程
java·人工智能·windows·gpt·chatgpt·web
桦说编程5 小时前
【AtomicAgent系列1】变异测试——过去做不起,现在 agent 做得起
后端·ai编程·vibecoding
考虑考虑6 小时前
nginx打印请求日志
运维·后端·nginx
策案案案6 小时前
YOLO: 将AI Agents嵌入到IntelliJ IDEA
java·大数据·人工智能·笔记·yolo·microsoft·intellij-idea
IT_陈寒6 小时前
Python的切片赋值把我坑惨了,这不是bug是特性
前端·人工智能·后端
CoderLiu6 小时前
程序化工具调用(PTC)与动态工作流引擎:深入大模型工具调用的架构演进与实践
前端·人工智能·后端