• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringBoot框架详细

武飞扬头像
IT光
帮助2

目录

1.SpringBoot简介

2. 搭建springboot工程

3. 了解pom.xml以及配置文件

4. springboot的配置文件种类

5. java读取springboot配置文件的内容。

第一种: @Value读取

第二种: @ConfigurationProperties

6.springboot多环境开发配置

7.springboot注册web三大组件

7.1 注册servlet到springboot内置的tomcat中

7.2注册web的filter组件

7.3注册web的监听器Listener组件

8.springboot自动装配原理

8.1springboot自动包扫描

8.2 自动装配原理


1.SpringBoot简介

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

特征:

  • 可以快速独立的创建Spring应用程序
  • 并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs
  • 使用了嵌入式的Servlet容器,无需生成WAR包
  • 提供自动配置的“starter”项目对象模型(POMS) 以简化Maven配置
  • 大量的自动配置,极大地简化了我们的开发
  • 无需XML文件的大量编写,也不会生成代码,底层是利用SpringBoot写好的API来调用实现,开箱即用
  • SpringBoot也有运维监控项目的功能
  • SpringBoot与云计算的集成

springboot:----为了简化spring工程的初始化搭建和开发过程。

2. 搭建springboot工程

(1.)

学新通

 (2.)

学新通

 (3)

学新通

 在主类所在的包下创建controller包并在该包下创建一个HelloController类

  1.  
    @RestController
  2.  
    public class HelloController {
  3.  
     
  4.  
    @GetMapping("hello")
  5.  
    public Map<String, Object> hello() {
  6.  
    // 创建一个用于存储返回结果的Map对象
  7.  
    Map<String, Object> map = new HashMap<>();
  8.  
    // 向Map中添加名为"name"的键值对,值为"张三"
  9.  
    map.put("name", "张三");
  10.  
    // 向Map中添加名为"age"的键值对,值为25
  11.  
    map.put("age", 25);
  12.  
    // 返回存储结果的Map对象
  13.  
    return map;
  14.  
    }
  15.  
    }
学新通

启动主函数:

  1.  
    @SpringBootApplication
  2.  
    public class SpringbootApplication {
  3.  
     
  4.  
    public static void main(String[] args) {
  5.  
    SpringApplication.run(SpringbootApplication.class, args);
  6.  
    }
  7.  
     
  8.  
    }

浏览访问HelloContnroller下的路径资源:

学新通

3. 了解pom.xml以及配置文件

pom.xml

  1.  
    <!--继承springboot父工程:该工程中的主要作用:对jar的版本管理-->
  2.  
    <parent>
  3.  
    <groupId>org.springframework.boot</groupId>
  4.  
    <artifactId>spring-boot-starter-parent</artifactId>
  5.  
    <version>2.7.12</version>
  6.  
    <relativePath/> <!-- lookup parent from repository -->
  7.  
    </parent>

application.properties

application.properties配置文件是用于配置应用程序的各种属性和设置的文件。它是一个常见的外部配置文件,可以在其中指定应用程序的行为,包括数据库连接、端口号、日志级别、国际化设置等。

  1.  
    #Tomcat端口号[] key=value
  2.  
    server.port=7777
  3.  
    # 设置工程的上下文路径
  4.  
    server.servlet.context-path=/aaa

4. springboot的配置文件种类

properties:属性文件类型: 格式 key=value格式

application.properties

  1.  
    #Tomcat端口号[] key=value
  2.  
    server.port=7777
  3.  
    # 设置工程的上下文路径
  4.  
    server.servlet.context-path=/aaa

yml:文件类型: 格式:

application.yml

  1.  
    server:
  2.  
    port: 8081 #Tomcat端口号[]
  3.  
    servlet:
  4.  
    context-path: /bbb #设置工程的上下文路径

如果yml和properties都存在相同的配置,则以properties为主, 如果两个配置文件的内部不同。则合并。#不管是那种格式,他们的名称必须叫:application

5. java读取springboot配置文件的内容。

比如:OSS文件上传。 需要: bucketName 密钥 id. 之前都是写死到类中。

如果改为客户的。需要修改java源码。

---交付的war文件。通过java代码读取配置文件里面的内容

读取配置文件内容的方式:

application.properties配置文件:

  1.  
    student.name=zhangsan
  2.  
    student.age=18
  3.  
    student.address=beijing

第一种: @Value读取

使用@Value读取配置文件

  1.  
    @RestController
  2.  
    public class HelloController {
  3.  
     
  4.  
    @Value("${student.name}") //读取springboot配置文件中student.name对应的内容
  5.  
    private String name;
  6.  
     
  7.  
    @Value("${student.age}")
  8.  
    private String age;
  9.  
     
  10.  
    @Value("${student.address}")
  11.  
    private String address;
  12.  
     
  13.  
    @GetMapping("stu")
  14.  
    public String stu(){
  15.  
    return "姓名:" name ";年龄:" age ";地址:" address;
  16.  
    }
  17.  
    }
学新通

第二种: @ConfigurationProperties

@ConfigurationProperties(prefix="前缀")

---封装一个Student类

  1.  
    @ConfigurationProperties(prefix = "student") //配置文件中以student开头
  2.  
    @Data
  3.  
    @Component //由spring容器帮你创建该类的对象,并把读取到配置文件中以student开头的内容配置给相应的属性
  4.  
    public class Student {
  5.  
    private String name;
  6.  
    private Integer age;
  7.  
    private String address; //属性名要和配置文件中的名称相同
  8.  
     
  9.  
    }
  1.  
    @RestController
  2.  
    public class HelloController {
  3.  
     
  4.  
    @Autowired
  5.  
    private Student student;
  6.  
     
  7.  
    @GetMapping("main")
  8.  
    public Student main(){
  9.  
    return student;
  10.  
    }
  11.  
    }

注意:第一种方式只能读取(基本类型和字符串类型), 第二种方式可以读取任意类型

例:

application.properties配置文件:(有集合,map就只能使用第二种,第一种会报错)

  1.  
    student.name=zhangsan
  2.  
    student.age=18
  3.  
    student.address=beijing
  4.  
    student.hobby[0]=swing0
  5.  
    student.hobby[1]=swing1
  6.  
    student.hobby[2]=swing2
  7.  
    student.map.k1=v1
  8.  
    student.map.k2=v2
  9.  
    student.map.k3=v3

封装一个Student类

  1.  
    @ConfigurationProperties(prefix = "student") //配置文件中以student开头
  2.  
    @Data
  3.  
    @Component //由spring容器帮你创建该类的对象,并把读取到配置文件中以student开头的内容配置给相应的属性
  4.  
    public class Student {
  5.  
    private String name;
  6.  
    private Integer age;
  7.  
    private String address; //属性名要和配置文件中的名称相同
  8.  
    private List<String> hobby;
  9.  
    private Map<String , Object>map;
  10.  
     
  11.  
    }
  1.  
    @RestController
  2.  
    public class HelloController {
  3.  
     
  4.  
    @Autowired
  5.  
    private Student student;
  6.  
     
  7.  
    @GetMapping("main")
  8.  
    public Student main(){
  9.  
    return student;
  10.  
    }
  11.  
    }

上面application.properties配置文件  内容转换到  application.yml配置文件

  1.  
    student:
  2.  
    address: beijing
  3.  
    age: 18
  4.  
    hobby:
  5.  
    - swing0
  6.  
    - swing1
  7.  
    - swing2
  8.  
    map:
  9.  
    k1: v1
  10.  
    k2: v2
  11.  
    k3: v3
  12.  
    name: zhangsan

在线yaml转properties-在线properties转yaml-ToYaml.com

6.springboot多环境开发配置

环境: (1)开发环境 (2)测试环境 (3)生产环境【线上环境】

由于环境的不同,可能它们的配置也会不同

例:不同环境下的端口号不同

(1)application-dev.properties

  1.  
    #针对关于开发的配置内容
  2.  
    server.port=8001

(2) application-test.properties

  1.  
    #针对关于测试环境的配置内容
  2.  
    server.port=8002

(3)application-online.properties

  1.  
    #针对关于线上环境的配置内容---
  2.  
    server.port=8003

application.properties

  1.  
    # 不同环境公共配置可以写在application
  2.  
    student.name=zhangsan
  3.  
    student.age=18
  4.  
    student.address=beijing
  5.  
    student.hobby[0]=swing
  6.  
    student.hobby[1]=eating
  7.  
    student.hobby[2]=sleeping
  8.  
    student.map.k1=v1
  9.  
    student.map.k2=v2
  10.  
    student.map.k3=v3
  11.  
    #激活相应的环境配置---每个环境的文件配置名必须:application-xxx.properties
  12.  
    spring.profiles.active=dev

7.springboot注册web三大组件

web中的三大组件:

  •   servlet
  •  filter过滤器
  •  Listener监听器

由于springboot没有web.xml文件了,有时我们需要把第三方的servlet注册到springboot中。讲解shiro安全框架

7.1 注册servlet到springboot内置的tomcat中

(1)创建一个servlet类

  1.  
    public class MyServlet extends HttpServlet {
  2.  
     
  3.  
    @Override
  4.  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  5.  
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  6.  
    }
  7.  
     
  8.  
    @Override
  9.  
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  10.  
     
  11.  
    System.out.println("************************************************");
  12.  
    }
  13.  
    }

(2)创建一个配置类

  1.  
    @Configuration //等价于xml配置文件
  2.  
    public class MyConfig {
  3.  
     
  4.  
    @Bean //等价于xml配置文件<bean/> ServletRegistrationBean该类用于注册servlet到tomcat容器中
  5.  
    public ServletRegistrationBean<HttpServlet> registrationBean(){
  6.  
    ServletRegistrationBean<HttpServlet> registrationBean=new ServletRegistrationBean<>();
  7.  
    registrationBean.setName("my"); // 设置Servlet的名称为"my"
  8.  
    registrationBean.setServlet(new MyServlet()); // 设置Servlet的实例为自定义的MyServlet对象
  9.  
    registrationBean.addUrlMappings("/my"); // 设置Servlet的URL映射为"/my"
  10.  
    return registrationBean; // 返回ServletRegistrationBean对象
  11.  
    }
  12.  
     
  13.  
    }

(3)测试

学新通

7.2注册web的filter组件

(1)创建一个过滤器类

  1.  
    public class MyFilter implements Filter {
  2.  
    @Override
  3.  
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  4.  
    //校验
  5.  
    System.out.println("经过了过滤器~~~~~~~");
  6.  
    filterChain.doFilter(servletRequest,servletResponse);//放行到下一个过滤器 或者 到达请求地址的controller
  7.  
    }
  8.  
    }

(2)过滤器的配置类

  1.  
    @Configuration //等价于xml配置文件
  2.  
    public class MyConfig {
  3.  
     
  4.  
     
  5.  
    //过滤器的配置类
  6.  
    @Bean
  7.  
    public FilterRegistrationBean<Filter> filterRegistrationBean() {
  8.  
    FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
  9.  
    filterRegistrationBean.setName("myFilter"); // 设置过滤器的名称为 "myFilter"
  10.  
    filterRegistrationBean.setFilter(new MyFilter()); // 设置过滤器实例为 MyFilter 的实例
  11.  
    filterRegistrationBean.addUrlPatterns("/*"); // 设置过滤器的 URL 匹配模式为 "/*",即对所有请求进行过滤
  12.  
    return filterRegistrationBean; // 返回过滤器注册的对象
  13.  
    }
  14.  
    }

7.3注册web的监听器Listener组件

(1)创建一个监听器类

  1.  
    @WebListener
  2.  
    public class MyListener implements ServletContextListener {
  3.  
     
  4.  
    @Override
  5.  
    public void contextInitialized(ServletContextEvent servletContextEvent) {
  6.  
    System.out.println("ServletContext 初始化");
  7.  
    }
  8.  
     
  9.  
    @Override
  10.  
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
  11.  
    System.out.println("ServletContext 销毁");
  12.  
    }
  13.  
    }

(2)监听器的配置类

  1.  
    @Configuration //等价于xml配置文件
  2.  
    public class MyConfig {
  3.  
     
  4.  
    //监听器
  5.  
    @Bean
  6.  
    public ServletListenerRegistrationBean<MyListener> listenerRegistrationBean() {
  7.  
    // 创建一个 ServletListenerRegistrationBean 实例
  8.  
    ServletListenerRegistrationBean<MyListener> listenerRegistrationBean = new ServletListenerRegistrationBean<>();
  9.  
    // 设置监听器为 MyListener 的实例
  10.  
    listenerRegistrationBean.setListener(new MyListener());
  11.  
    // 返回配置好的 ServletListenerRegistrationBean 实例
  12.  
    return listenerRegistrationBean;
  13.  
    }
  14.  
     
  15.  
    }
学新通

8.springboot自动装配原理

Spring Boot 的自动装配原理通过条件注解和条件判断来确定是否需要创建相应的组件,自动配置类定义了创建和配置组件的方法,启动器则简化了依赖管理。这样可以让开发者更加专注于业务开发,减少了手动配置的工作量。

8.1springboot自动包扫描

springboot默认扫描的包是哪些?

主类所在的包以及子包。

---为什么是这样? 能不能认为改动?

包扫描的核心一定在主类上的@SpringBootApplication上

@SpringBootApplication源码:

学新通

@EnableAutoConfiguration

学新通

@AutoConfigurationPackage
Registrar.class

学新通

 可以在主类上使用@ComponentScan来修改默认的包扫描

学新通

8.2 自动装配原理

springboot帮你完成自动装配

学新通

AutoConfigurationImportSelector.class

学新通

 学新通

当主函数运行时会加载一个使用@SpringBootApplication注释的类,@SpringbootApplication它是一个复合组件。其中 @EnableAutoConfiguration它是开启自动配置的核心注解,该注解也是一个复合组件,其中@Import({AutoConfigurationImportSelector.class}) ,该注解需要导入一个AutoConfigurationImportSelector类,该类会加载你需要的自动装配类,而这些自动装配类会完成响应的自动装配功能。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibhjbh
系列文章
更多 icon
同类精品
更多 icon
继续加载