SpringBoot架构

1.什么是Springboot?

springboot可以帮你简化spring的搭建,并且快速创建一个spring的应用程序。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置

2.Springboot特点有哪些?

(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
(2)内嵌Tomcat或Jetty等Servlet容器;
(3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
(4)尽可能自动配置Spring容器;
(5)提供准备好的特性,如指标、健康检查和外部化配置;
(6)绝对没有代码生成,不需要XML配置。 

3.创建springboot工程

前提:

1.JDK必须为1.8以上

2.spring的jar必须5.0以上

3.maven必须3.3以上

SpringBoot架构_第1张图片

SpringBoot架构_第2张图片 SpringBoot架构_第3张图片

 SpringBoot架构_第4张图片

创建一个controller类

SpringBoot架构_第5张图片

 启动springboot工程并浏览器访问:

SpringBoot架构_第6张图片

3.1 介绍springboot中pom文件



    4.0.0
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.12.RELEASE
         
    
    com.ykq
    qy151-springboot
    0.0.1-SNAPSHOT
    qy151-springboot
    Demo project for Spring Boot
    
        1.8
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


注意:

SpringBoot架构_第7张图片

 默认springboot扫描的包为主启动类所在的包以及子包。

3.2 介绍springboot的配置文件

有两种格式的配置文件:

第一种: properties属性文件

# 修改springboot中tomcat端口号.
server.port=8888

第二种: yml文件

#注意有空格和四个空格(Tab)
server:
    port: 6666

 不管是哪种,他们的名字必须以application开始

如果两个配置文件同时存在,而且有些内容一样。按照properties的优先级高如果有些不一样,两个配置文件不一样的会合并在一起。

4.读取springboot配置文件中的内容

案例:

        OSS文件上传

密钥和bucket名称等---密钥和bucket都写死在java代码中。如果后期修改密钥和bucket的值,你必须修改源码代码。 我们要写在配置文件。然后通过代码在读取配置文件中的密钥和bucket.

如何读取springboot配置文件的内容呢?

通过@ConfigurationProperties或者@Value注解。

@ConfigurationProperties该注解使用在类上。

#自定义的配置信息
student.name=ldh
student.age=15
student.hobby[0]=sing
student.hobby[1]=swing
@Data
@Component //该类对象的创建和销毁都有spring容器来管理
@ConfigurationProperties(prefix = "student")    //读取springboot中的配置内容
public class Student {
    private String name;
    private Integer age;
    private String[] hobby;
}
@Autowired  //spring容器帮你注入该对象
    private Student student;
    @GetMapping("/index")
    public Student index(){
        return student;
    }

@Value 只能放在我们的类属性上。而且它只能读取基本类型和字符串类型。


#自定义的配置信息
student.name=ldh
student.ages=15
student.hobby[0]=sing
student.hobby[1]=swing
student.map.clazz=qy151
student.map.stuno=110

SpringBoot架构_第8张图片

5. profiles文件的介绍

思考: 我们在实际开发中,环境有哪些?

开发环境---->测试环境---->线上环境 由于环境的不同,那么就会有不同的配置内容。

难道我们不断的修改配置内容。----不会

实际工作中,针对不同的环境配置不同的配置文件,然后再总的配置文件中激活相应的配置文件

 SpringBoot架构_第9张图片

SpringBoot架构_第10张图片

6. Springboot注册web三大组件

什么是web的三个组件?

Servlet和Filter以及Linstener监听器。

为什么要注册这三个组件呢?

因为后面springboot有可能要集成第三方框架,而第三方框架的底层可能就依赖于过滤器或者servlet.

如何注册呢?

思考: 早期:

<1>Servlet类

<2>注册到Tomcat容器web.xml


 
 Servlet类


  
  /

现在:都没有web.xml

创建一个配置类:

@Configuration //该类为配置类 xml文件
public class MyConfig {

 @Bean  //理解为配置文件中
 public ServletRegistrationBean registrationBean(){
     //创建一个Servlet注册器.
      ServletRegistrationBean registrationBean=new ServletRegistrationBean<>();
      registrationBean.setName("my");
      registrationBean.setServlet(new MyServlet());
      registrationBean.addUrlMappings("/my");
      return registrationBean;
 }

}
package com.lqh.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Li Qinghua
 * @Create 2022/7/21 19:53
 */
public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("这是自己定义的servlet");
    }
}

以前如何注册过滤器: web.xml

 

现在:

package com.lqh.config;

import com.lqh.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @Author Li Qinghua
 * @Create 2022/7/21 20:05
 */
@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean testFilter(){
        FilterRegistrationBean filter=new FilterRegistrationBean<>();
        filter.setName("myFilter");
        filter.setFilter(new MyFilter());
        filter.addUrlPatterns("/*");
        return filter;
    }
}
package com.lqh.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @Author Li Qinghua
 * @Create 2022/7/21 20:04
 */
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("这是自己定义的filter");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

7.springboot自动装配原理[面试]

为什么包扫描时,扫描的为主类所在的包以及子包?

默认扫描启动类所有的包及其子包都可以自动扫描

7.1如何自动扫描

@SpringBootApplication

SpringBoot架构_第11张图片

public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
    AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
}

7.2如何加载自动配置类

@EnableAutoConfiguration

SpringBoot架构_第12张图片

@Import(AutoConfigurationImportSelector.class)

发现当Springboot启动时默认加载127【2.2.2】个自动配置类

然后再排除不生效的配置类

SpringBoot架构_第13张图片

 思考: 这127自动配置类从哪来的

SpringBoot架构_第14张图片

SpringBoot架构_第15张图片

你可能感兴趣的:(spring,boot,架构,spring)