spring boot的基础入门

1. 什么是springboot

springboot也是spring公司开发的一款框架。为了简化spring项目的初始化搭建的。

spring项目搭建的缺点: [1]配置麻烦 [2]依赖 [3] tomcat启动慢

2. springboot的特点

1) 自动配置

Spring Boot的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是SpringBoot自动完成的。

2) 起步依赖

起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。

简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能。

3) 辅助功能

提供了一些大型项目中常见的非功能性特性,如嵌入式服务器tomcat、安全、指标,健康检测、外部配置等。

3. 搭建springboot的项目

请添加图片描述

请添加图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <!--springboot3.0以上必须使用jdk17-->
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>

    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <!--web依赖jar-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--单元测试jar-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <!--如果下方出现红色可以删除-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4. springboot常用的配置文件种类

springboot提供了两种配置文件。第一种properties 第二种:yml文件。不管是哪种他们的前缀都是application

yaml格式

server:
  port: 8088
  servlet:
    context-path: /bbb

properties格式:

#key=value
server.port=8080
server.servlet.context-path=/aaa

如果两个文件同时存在,properties文件的优先级更高。

5. springboot中java如何读取配置文件中的内容【重点】

我们习惯把一些自己的信息放入配置文件中,便于修改。比如OSS. 支付。 我们还希望通过java代码能够读取到配置文件中自己的信息。

springboot提供了两种方式用于读取springboot配置文件中信息的方式。

第一种: @ConfigurationProperties

使用在类上 @ConfigurationProperties(prefix=“前缀”)

请添加图片描述

StudentController

@RestController
public class StudentController {

    @Autowired
    private Student student;

    @GetMapping("student")
    public Student getStudent(){
        return student;
    }
}

第二种: @Value

它只能读取基本类型和字符串类型

请添加图片描述

6. profile多环境配置 [重点]

我们在开发Spring Boot应用时,通常同一套程序会被安装到不同环境,比如:开发、测试、生产等。其中数据库地址、服务器端口等等配置都不同,如果每次打包时,都要修改配置文件,那么非常麻烦。profile功能就是来进行动态配置切换的。

1) profile配置方式

  • 多profile文件方式
  • yml多文档方式

2) profile激活方式

  • 配置文件
  • 命令行参数

我们需要针对不同的环境来创建不同的配置文件。使用profile来激活对应的配置文件

比如:

application-dev.properties  [开发环境的配置文件]
application-test.properties [测试环境的配置文件]
application-pro.properties [生产环境的配置文件]
-----------------------相同配置依然还是放在application.properties中----------------

请添加图片描述

如何激活对于的配置文件。激活的方式有两种:

第一种: 之间在application配置文件中

#激活对应环境的配置文件
spring.profiles.active=pro

第二种: 部署时如何激活对应环境的配置[cmd]

命令行参数:java –jar xxx.jar --spring.profiles.active=dev

7. springboot注册web组件

7.1 注册servlet

web组件表示的就是servlet,filter组件。

servlet的步骤.

[1]创建一个类并继承HttpServlet 重写service方法

[2]注册到web.xml文件中

<servlet>
 <servlet-name></servlet-name>
 <servlet-class>自己的servlet或第三方的servlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name></servlet-name>
  <url-partern>/my</url-partern>
</servlet-mapping>

思考: 在springboot中还有没有web.xml文件。[没有] 它如何注册servlet? 提供了一个配置类。

创建一个Servlet

public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }
}

创建一个配置类

@Configuration //表示该类为配置类,等价于之前的xml配置文件
public class MyConfig {

    @Bean //等价于<bean标签.
    public ServletRegistrationBean  myServlet(){
        ServletRegistrationBean bean=new ServletRegistrationBean();
        bean.setServlet(new MyServlet());
        bean.setName("my");
        bean.setLoadOnStartup(1);
        bean.addUrlMappings("/my");
        return bean;
    }
}

7.2 注册过滤器

创建一个配置类

@Configuration //表示该类为配置类,等价于之前的xml配置文件
public class MyConfig {
	@Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean bean=new FilterRegistrationBean();
        bean.setFilter(new MyFilter());
        bean.setName("my");
        bean.addUrlPatterns("/*");
        return bean;
    }
}

8. springboot包扫描的原理

springboot自带了包扫描的功能。核心在主类上@SpringBootApplication上,它是一个复合注解,里面包含@EnableAutoConfiguration开启自动配置,里面包含@AutoConfigurationPackage。@Import({AutoConfigurationPackages.Registrar.class})需要导入一个自动配置包的类。加载主类所在的包,并按照该包进行扫描。

我们如果不想让他扫描主类所在的包,我们可以使用@CompentScan(basePackages={})来指定自己的包

请添加图片描述

9. springboot的自动装配原理【了解】

我们原来ssm项目,都需要加载前端控制器DispatcherServlet. 而现在的springboot并没有加载DispatcherServlet。 springboot具备自动装配的功能。

springboot启动时,加载了使用@SpringbootApplication注解的类,该注解是一个符合注解,包含@EnableAutoConfiguration该注解开启了自动装配功能,该注解也是一个符合注解里面包含@Import({AutoConfigurationImportSelector.class}),导入AutoConfigurationImportSelector该类自动装配选择器类,该类会自动加载很多自动装配。每个自动装配会完成对于的自动装配功能

请添加图片描述

DispatcherServletAutoConfiguration类

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值