宝塔山 发表于 2023-8-29 18:39:53

SpringBoot

1.回顾

spring的AOP:
(1)什么是AOP? 面向切面编程,它是对OOP的一种补充技术。把业务代码和非业务代码分离。在不改变业务代码的前提下,可以对业务代码进行增强。
(2)应用场景: (1)日志 (2)权限校验 (3)事务处理。
(3) 核心的概念:
​      【1】切面:
​       切点:
​       通知: 前置后置后置返回异常环绕

[*]事务: 概念。 如何实现事务? tx aspect依赖   配置事务切面类开启事务驱动注解
2. 正文

1. 什么是springboot?
2. 为什么使用springboot?
3. 如何创建springboot工程?
4. java中如何读取springboot配置文件的内容?
5. springboot注册web组件
6. springboot自动装配原理?
7. springboot整合数据源?
8. springboot整合mybatis.3.什么是springboot?

SpringBoot基于Spring4.0设计,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决。
4. springboot的优点

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

【1】第一种快速搭建-----必须联网
【2】使用maven来搭建。
5.1 第一种快速搭建-----必须联网

https://www.cnblogs.com/assets/1692254774508.png
https://www.cnblogs.com/assets/1692254901474.png
https://www.cnblogs.com/assets/1692255071086.png
https://www.cnblogs.com/assets/1692255485978.png
编写controller代码
https://www.cnblogs.com/assets/1692255969578.png
@RestController
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("/index") //java对象--转换为json必须引入jackson
    public Map<String,Object> index(){
      Map<String,Object> map=new HashMap<>();
      map.put("name","成毅");
      map.put("age",15);
      return map;
    }
}注意: controller包必须在主启动类所在包下。 默认扫描的包为主启动类所在的包。
5.2 聊聊springboot中的配置

https://www.cnblogs.com/assets/1692257055586.png
https://www.cnblogs.com/assets/1692257530390.png
5.3 第二种搭建springboot----了解

(1)创建一个maven工程
(2) pom中添加相关的依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
   
    <parent>
         <artifactId>spring-boot-starter-parent</artifactId>
      <groupId>org.springframework.boot</groupId>
      <version>2.7.5</version>
    </parent>
    <groupId>com.ykq</groupId>
    <artifactId>qy168-springboot02</artifactId>
    <version>1.0-SNAPSHOT</version>

   
    <dependencies>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
    </dependencies>

</project>(3)创建一个springboot配置文件--application
(4)创建主启动类
@SpringBootApplication
public class SpringBootApp {
    public static void main(String[] args) {
      SpringApplication.run(SpringBootApp.class,args);
    }
}6. java读取springboot配置文件中的内容

6.1 springboot配置文件的种类

springboot提供了两种格式的配置文件.   内容格式不同。
第一种:属性文件 后缀: properties
https://www.cnblogs.com/assets/1692259722608.png
第二种:yaml文件后缀 yml
https://www.cnblogs.com/assets/1692259718240.png
如果上面两个配置文件的内容相同: 以properties配置为主。如果上面配置文件的内容不同:都能用。
6.2 如何读取springboot配置文件中的内容

比如我们使用OSS完成文件上传时,之前把OSS相关的内容密钥,bucket等信息都写在java代码,在实际开发中肯定不允许。因为属于硬编码。以后项目上线后密钥和bucket等信息都需要改为客户。都需要修改了源码。 以后写都应该提取到配置文件中。我们掌握如何java代码读取配置文件中的内容。
在spring中提供了两个方式读取:
第一种: @Value
https://www.cnblogs.com/assets/1692260522925.png
思考: 它只能一个一个的读取。能否封装一个实体类,把读取的内容放入实体类属性中。
第二种: @ConfigurationProperties
https://www.cnblogs.com/assets/1692261019717.png
第一种@Value只能读取配置文件中基本类型和字符串类型。而第二种可以读取任意类型的值
https://www.cnblogs.com/assets/1692262192443.png
7.springboot注册web组件

servlet:-----(1)定义一个Servlet类 (2)重写doGet和doPost方法 (3)把自定义的serlvet注册到web.xml文件。
filter: ----(1)定义一个filter类(2)dofilter方法(3)把自定义的过滤器注册到web.xml
思考: 我们现在的springboot工程没有web.xml文件了。如何把自定义或者第三方的过滤器注册到tomcat容器中。
7.1 注册自定义的Servlet

(1)自定义一个Servlet并重写doGet和doPost方法
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      System.out.println("执行了doget方法");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      System.out.println("执行了doPost方法~~~~~~~~~~~~~~~~~~~~~~~");
    }
}(2)把自定义的Servlet注册到内置tomcat中。
@Configuration //等价于spring配置文件.
public class MyConfiguration {

    @Bean //相当于spring配置文件中 <bean >把该方法返回的对象交于spring容器来管理了
    public ServletRegistrationBean<Servlet> registrationBean(){
      ServletRegistrationBean<Servlet> registrationBean=new ServletRegistrationBean<>();
      registrationBean.setServlet(new MyServlet());
      registrationBean.setName("my");
      registrationBean.addUrlMappings("/my");
      return registrationBean;
    }
}(3)测试
https://www.cnblogs.com/assets/1692263370304.png
7.2 注册自定义过滤器

(1)创建一个过滤器类并重写相应的方法。
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      System.out.println("经过了过滤器~~~~~~~~~~~~~~~~~~~~~~~~");
    }
}(2)把自定义的过滤器注册到内置的tomcat中
@Bean
    public FilterRegistrationBean<Filter> registrationBean02(){
      FilterRegistrationBean<Filter> registrationBean=new FilterRegistrationBean<>();
      registrationBean.setFilter(new MyFilter());
      registrationBean.setName("myFilter");
      registrationBean.addUrlPatterns("/*");
      return registrationBean;
    }(3)测试
8. springboot自动包扫描的原理

(1)查看@SpringBootApplication注解---它是一个复合注解
https://www.cnblogs.com/assets/1692264136856.png
(1)第一层含义自动包扫描第二层含义自动装配
https://www.cnblogs.com/assets/1692264203665.png
https://www.cnblogs.com/assets/1692264235614.png
https://www.cnblogs.com/assets/1692264395306.png
指定包扫描路径
https://www.cnblogs.com/assets/1692264650067.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: SpringBoot