初识springboot【手把手教你搭建springboot项目】+springboot日志详解【超详细】

简介: 初识springboot【手把手教你搭建springboot项目】+springboot日志详解【超详细】

目录

一.springboot的概念

1.什么是springboot?

二.使用springboot进行开发的优势

springboot的设计原则是什么,使用springboot进行开发具有怎样的优势?(M)

三.手把手搭建一个springboot项目

①创建项目并选择依赖

②设置热部署(部分代码改动不需要手动重新run即可生效)

四.springboot配置文件

1.前提须知

2.两种配置文件及对比

3.springboot默认扫描配置文件的位置

4.详解properties文件

1.语法规则

2.优先级

3.自定义配置以及配置信息的读取

五.springboot日志详解

1.日志的概念(包括框架)

2.日志的作用

3.日志的级别

4.日志的创建及打印

5.lombok补充

6.linux打印日志

一.springboot的概念

1.什么是springboot?

springboot与spring相同,都是一种矿建,但是springboot是基于spring框架进行开发的脚手架,对spring框架进行部分优化,从而提高项目开发的效率。

二.使用springboot进行开发的优势

springboot的设计原则是什么,使用springboot进行开发具有怎样的优势?(M)

springboot的设计原则:约定大于配置

其优越的设计原则使其具有超越springframework的优点:

①相比于springframework必须通过配置大量的xml文件来运行程序,springboot内部约定好了很多默认的配置,并提供不同的配置文件允许用户定制自定义的配置项

②springboot内部约定好了众多第三方框架的配置路径(主要包括各种框架的配置信息),可以在项目启动时加载各种配置信息,实现第三方框架的快速配置

③springboot内部集成了了web容器,无需配置其他的web容器

④springboot具有更多的监控指标,能更好的了解项目运行的情况

三.手把手搭建一个springboot项目

①创建项目并选择依赖

bb60543efb4c4566a69e635b72ca0f43.png

aeaa2f264ce041368e9b03799045a86c.png

3719fa1c7a044d81a906c96d18b264ff.png

②设置热部署(部分代码改动不需要手动重新run即可生效)

c124f9468d4540aba6427799ba8b8bcd.png

2a6a30863c784af48ec3e899fabe7cf6.png

③禁用JMX:因为如果不对其进行排除会导致在项目启动时报错,虽然这个报错不影响我们项目的实现,但是规范化起见,我们还是加上

203dcc7bdecd4f298c17a5ad3fc013cb.png

④禁用tomcat,取而代之undertow(非必须选项,换是因为undertow的效率略高于tomcat)

36f1a6daa15c43d8bc3f7b9f38307503.png

⑤修改编码集

bbe3c6b82aea45c38b0531cbb197a48b.png

b0d041ada28848fe99585fe867da377d.png

四.springboot配置文件

1.前提须知

受益于springboot的设计原则:springboot会约定好(配置好)许多默认的配置,在springboot项目启动时加载,也就是说即使用户不进行任何的配置,该springboot项目也会默认加载一些配置项,同时springboot提供给用户配置文件,允许用户进行定制化的项目配置

2.两种配置文件及对比

SpringBoot 默认使用以下 2 种全局的配置文件,其文件名是固定的

  • application.properties
  • application.yml

其中,application.yml 是一种使用 YAML 语言编写的文件,它与 application.properties 一样,可以在 Spring Boot 启动时被自动读取,修改 Spring Boot 自动配置的默认值。二者功能类似,都能完成Spring Boot配置(例如指定Tomcat端口,配置mybatis等),但是Properties的优先级要高于YAML。

3.springboot默认扫描配置文件的位置

springboot项目在运行时,会默认扫描以下路径,查找配置文件并对项目进行定制化的配置

  • file:./config/
  • file:./config/*/
  • file:./
  • classpath:/config/
  • classpath:/
  • 注:file: 指当前项目根目录;classpath: 指当前项目的类路径,即 resources 目录。

以上所有位置的配置文件都会被加载,且它们优先级依次降低,序号越小优先级越高。其次,位于相同位置的 application.properties 的优先级高于 application.yml。

所有位置的文件都会被加载,高优先级配置会覆盖低优先级配置,形成互补配置,即:

存在相同的配置内容时,高优先级的内容会覆盖低优先级的内容;
存在不同的配置内容时,高优先级和低优先级的配置内容取并集。

4.详解properties文件

1.语法规则

key=value ,一般value不加双引号或者单引号

2.优先级

不同的文件位置,配置文件的优先级也有所不同

  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/
  • 由上到下优先级逐渐降低

3.自定义配置以及配置信息的读取

通过@Value读取配置文件中的信息

配置文件中:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8

spring.datasource.name=root

spring.datasource.password=abc123

@Value("${spring.datasource.url}")

   private String url;

   @Value("${spring.datasource.name}")

   private String name;

   @Value("${spring.datasource.password}")

   private String password;

通过@ConfigurationProperties(prefix = "”)获取配置类中自定义的对象

配置文件中:

# 创建自定义对象

user.username=zhangsan

user.userId=1

user.list[0]=1

user.list[1]=2

user.map.1=hhh

类中:

@ConfigurationProperties(prefix = "user")

public class User {

   private String username;

   private int userId;

   private List<Integer>list;

   private Hashtable<Integer,String>map;

   //使用init方法判断

   @PostConstruct

   public void init(){

       System.out.println(username+userId+list+map);

   }

五.springboot日志详解

1.日志的概念(包括框架)

日志在程序运行时打印在控制台上的信息:

bad59efd40c14994b37ebce8346bf696.png

日志框架

常见的日志框架如:log4j、logback、slf4j、jdk-logging、commons-logging......但是这些框架在使用中一旦涉及日志框架的转化,十分不方便,因此slf4j应运而生,slf4j用于日志框架的桥接,引入slf4j之后,日志的使用也比较简单了:只需要引入某个具体日志框架的依赖包+slf4j的依赖包,统一使用slf4j的配置类和方法。

2.日志的作用

  1. 发现和定位问题
  2. 记录用户的登录信息,进行大数据分析
  3. 记录系统的操作信息,方便数据的恢复和定位操作者
  4. 记录程序的执行时间,方便以后优化程序

3.日志的级别

所有项目默认的日志打印级别是info(只打印info即其以上的日志级别)

日志的级别由上到下级别逐渐变高,我们可以通过设置配置文件来修改项目的日志打印级别

# 当前项目日志的打印级别是debug

logging.level.root=debug

# 设置具体某个包下的日志打印级别

logging.level.com.ljl.springmvc_adv.controller.loginController=info

c3210f475d0142d68b78f7c1ca21321f.png

4.日志的创建及打印

使用方式如下:

package com.example.demo.Controller;

 

@Controller

@ResponseBody

public class LoggerController {

 

   // 1. 得到日志对象

   private Logger logger = LoggerFactory.getLogger(LoggerController.class);

 

   // 2. 打印日志

   @RequestMapping("/logger")

   public String logger(){

       logger.trace("日志级别: trace");

       logger.debug("日志级别: degue");

       logger.info("日志级别: info");

       logger.warn("日志级别: warn");

       logger.error("日志级别: error");

       return "logger";

   }

}

在默认的配置下,一般只打印info及以上级别的日志

a5b0f876f4b44e9fb570fbd260324a60.png

@Slf4j

@Component

public class LoggerTest {

   public static void main(String[] args) {

       log.debug("这是debug级别的日志......");

       log.info("这是info级别的日志......");

       log.warn("这是warn级别的日志......");

       log.error("这是error级别的日志......");

   }

}

cb1365c500fe4adb8aea29a8db7b8fcf.png

5.lombok补充

大家思考一个问题:为什么加入@Data注解之后,为什么很多方法为什么即使不写也会自动生成呢?

我们不妨对比一下编译前后的文件类

我们在所写的文件中的文件类是这样的:

@Data

@Repository//注入到容器

@ConfigurationProperties(prefix = "user")

public class User {

   private String username;

   private int userId;

   private List<Integer>list;

   private Hashtable<Integer,String>map;

   //使用init方法判断

   @PostConstruct

   public void init(){

       System.out.println(username+userId+list+map);

   }

 

}

编译后:

package com.example.springboot_study.test;

 

import java.util.Hashtable;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Repository;

 

@Repository

@ConfigurationProperties(

   prefix = "user"

)

public class User {

   private String username;

   private int userId;

   private List<Integer> list;

   private Hashtable<Integer, String> map;

 

   @PostConstruct

   public void init() {

       System.out.println(this.username + this.userId + this.list + this.map);

   }

 

   public User() {

   }

 

   public String getUsername() {

       return this.username;

   }

 

   public int getUserId() {

       return this.userId;

   }

 

   public List<Integer> getList() {

       return this.list;

   }

 

   public Hashtable<Integer, String> getMap() {

       return this.map;

   }

 

   public void setUsername(final String username) {

       this.username = username;

   }

 

   public void setUserId(final int userId) {

       this.userId = userId;

   }

 

   public void setList(final List<Integer> list) {

       this.list = list;

   }

 

   public void setMap(final Hashtable<Integer, String> map) {

       this.map = map;

   }

 

   public boolean equals(final Object o) {

       if (o == this) {

           return true;

       } else if (!(o instanceof User)) {

           return false;

       } else {

           User other = (User)o;

           if (!other.canEqual(this)) {

               return false;

           } else if (this.getUserId() != other.getUserId()) {

               return false;

           } else {

               label49: {

                   Object this$username = this.getUsername();

                   Object other$username = other.getUsername();

                   if (this$username == null) {

                       if (other$username == null) {

                           break label49;

                       }

                   } else if (this$username.equals(other$username)) {

                       break label49;

                   }

 

                   return false;

               }

 

               Object this$list = this.getList();

               Object other$list = other.getList();

               if (this$list == null) {

                   if (other$list != null) {

                       return false;

                   }

               } else if (!this$list.equals(other$list)) {

                   return false;

               }

 

               Object this$map = this.getMap();

               Object other$map = other.getMap();

               if (this$map == null) {

                   if (other$map != null) {

                       return false;

                   }

               } else if (!this$map.equals(other$map)) {

                   return false;

               }

 

               return true;

           }

       }

   }

 

   protected boolean canEqual(final Object other) {

       return other instanceof User;

   }

 

   public int hashCode() {

       int PRIME = true;

       int result = 1;

       result = result * 59 + this.getUserId();

       Object $username = this.getUsername();

       result = result * 59 + ($username == null ? 43 : $username.hashCode());

       Object $list = this.getList();

       result = result * 59 + ($list == null ? 43 : $list.hashCode());

       Object $map = this.getMap();

       result = result * 59 + ($map == null ? 43 : $map.hashCode());

       return result;

   }

 

   public String toString() {

       return "User(username=" + this.getUsername() + ", userId=" + this.getUserId() + ", list=" + this.getList() + ", map=" + this.getMap() + ")";

   }

}

通过这些对比,我们可以得出以下的结论:lombok会在编译期将自己注解作用的代码加入类中,从而使注解生效。

6.linux打印日志

1ba512f20cc74551b85be60a18b96f66.png


相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
3月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
625 5
|
8月前
|
前端开发 安全 Java
Spring Boot 便利店销售系统项目分包设计解析
本文深入解析了基于Spring Boot的便利店销售系统分包设计,通过清晰的分层架构(表现层、业务逻辑层、数据访问层等)和模块化设计,提升了代码的可维护性、复用性和扩展性。具体分包结构包括`controller`、`service`、`repository`、`entity`、`dto`、`config`和`util`等模块,职责分明,便于团队协作与功能迭代。该设计为复杂企业级应用开发提供了实践参考。
320 0
|
5月前
|
机器学习/深度学习 XML Java
【spring boot logback】日志logback格式解析
在 Spring Boot 中,Logback 是默认的日志框架,它支持灵活的日志格式配置。通过配置 logback.xml 文件,可以定义日志的输出格式、日志级别、日志文件路径等。
921 5
|
5月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
300 3
|
5月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
484 3
|
5月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
625 2
|
5月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
335 2
|
5月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
256 0
|
5月前
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
458 0
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
112 0