自学SpringBoot一之启动类

本文解析SpringBoot项目的主程序类作为入口的作用,深入探讨@SpringBootApplication注解的组成及功能,包括@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan的具体含义。同时,简要介绍如何在Idea中创建SpringBoot项目及资源文件夹的目录结构。

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

一、主程序类,SpringBoot项目的入口

eg:

/**
 * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class hello {
    // Spring应用启动起来,run方法的参数只能是@SpringBootApplication注解标注的类
    public static void main(String[] args) throws Exception{
        SpringApplication.run(hello.class,args);
    }
}

二、关于 @SpringBootApplication

@SpringBootApplication 是一个组合注解,详细见下文代码:

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

1.注解@SpringBootConfiguration:表示SpringBoot的配置类,实际上也是一个组合类,底层是由Spring框架的配置注解@Configuration等注解组合而成。标注有@Configuration的类就属于配置类,可替代配置文件,而实际上配置类也是容器中的一个组件,底层标注了@Component。

2.注解@EnableAutoConfiguration:表示开启自动配置功能;即以前需要我们程序员自己配置的功能,均可交给SpringBoot帮我们配置。它下面的底层注解@AutoConfigurationPackage(自动配置包)可以将主配置类,即启动类所在的包之下的所有的子包及其所有的组件全部扫描到Spring容器中。也因此,一般情况我们都将启动类放在项目中最外层的包下。

3.注解@ComponentScan:会自动扫描包路径下面的所有@Controller@Service@Repository@Component 的类。

综上所诉@SpringBootApplication=@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan

三、使用idea快速生成一个SpringBoot项目(必须是联网状态下)

网上有很多教程,此处不详述。此处大致说一下resources文件夹中目录结构。

--resources:主要放置配置文件

  --static:保存所有的静态资源; js css  images;

  --templates:保存所有的模板,页面(html);(SpringBoot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);

  --application.properties:SpringBoot应用的配置文件;可以修改一些默认设置;

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值