Springboot学习笔记(一)

本文是Springboot学习的第一部分,主要介绍了Springboot的基础概念,包括起步依赖、自动配置、内嵌Servlet容器的使用,以及如何创建第一个Springboot应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring boot学习笔记(一)

maven环境搭建
首先需要搭建一个父model,所有的版本以父model版本为准
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

如果不继承这个parent,需要使用dependencyManagement,这样不需要再继承,避免了单继承的一些麻烦
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.4.0.RELEASE</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>



SpringBoot入口方式
SpringApplication.run(Application.class, args) 返回一个ConfigurableApplicationContext上下文对象,可以通过这个上下文对象来管理bean

在入口上使用 @SpringBootApplication这个注解,将自动扫描所有component,非常方便。
这里是这个注解的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration//当前类是配置类
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes =
TypeExcludeFilter.class))//自动扫描包
public @interface SpringBootApplication {
    
    Class<?>[] exclude() default {};//排除指定的bean


    String[] excludeName() default {};//排除指定的bean

    @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
    String[] scanBasePackages() default {};//从当前包开始扫描

    @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
    Class<?>[] scanBasePackageClasses() default {};

}


在springboot中,入口的源(run的第一个参数)自动当做一个配置类。
除了使用静态方法外,还可以创建一个SpringApplication对象来管理多个源

SpringApplication app = new SpringApplication();
Set<Object> set = new HashSet<>();
set.add(Application1.class);
set.add(Application2.class);
app.setSource(set);
ConfigurableApplicationContext context = app.run(args);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值