随笔分类 - SpringBoot
摘要:SpringBoot提供了监测与度量的功能,本篇记录一下使用过程 1、添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId>
阅读全文
摘要:本篇记录如何使用SpringBoot的默认日志和其他日志 一、使用默认日志logback 方法1:通过配置application.properties(不推荐) 日志的默认级别是info,且默认只打印在控制台 测试如下,控制台只打印出 info、warn、error,不打印debug package
阅读全文
摘要:SpringBoot能快速开发之一是因为只要引入某个starter就可获取其服务。现自己实现starter-redis,并且总结步骤。 一、创建SpringBoot工程starter-redis 1、添加依赖 <dependency> <groupId>org.springframework.boo
阅读全文
摘要:AOP即面向切面的编程,将业务逻辑代码和琐碎逻辑代码分开,达到重用或者解耦的目的 Spring中的AOP有两种实现,一种是基于jdk的,一种是基于cglib的,AopAutoConfiguration中相关代码如下: 一、基于jdk的动态代理实现 1、引入依赖 <dependency> <group
阅读全文
摘要:1、引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 2、代码示例 @Repository pub
阅读全文
摘要:一、SpringBoot中默认的数据源 SpringBoot中默认的数据源是HikariDataSource 下面测试一下(pom.xml无需额外添加依赖,application.yml中简单配置如下,不配置会报错) spring: datasource: url: jdbc:mysql://loc
阅读全文
摘要:方式一、修改application.properties文件中属性 server.port=8081 server.address=127.0.0.1 server.tomcat.accesslog.enabled=true server.tomcat.accesslog.directory=d\:
阅读全文
摘要:本篇介绍一下SpringBoot中两种处理异常的方式 一、实现ErrorPageRegistrar接口,指定跳转页面 1、实现ErrorPageRegistrar接口 @Component public class CommonErrorPageRegistrar implements ErrorP
阅读全文
摘要:使用步骤: 1、实现HandlerInterceptor接口,该接口有三个方法preHandle 、postHandle 、afterCompletion (1)preHandle在controller执行之前调用 (2)postHandle在controller执行之后,页面渲染之前调用 (3)a
阅读全文
摘要:有两种方法实现Servlet、Filter、Listener,在启动类上添加ServletComponentScan注解,或者分别创建RegistrationBean。 一、使用在启动类上添加ServletComponentScan注解的方式 1、创建Servlet、Filter、Listener,
阅读全文
摘要:一、在springboot项目中,有5个地方的静态资源可以直接访问: 1、webapp目录下 2、classpath:/static/目录下 3、classpath:/public/目录下 4、classpath:/resources/目录下 5、classpath:/META-INF/resour
阅读全文
摘要:现在一般都是前后端分离项目,很少用到freemarker作为视图层,这里简单记录一下在SpringBoot中使用freemarker的过程 1、添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri
阅读全文
摘要:SpringBoot通常在配置文件application.properties中设置属性,其实也可以在代码中写死属性,如下: @SpringBootApplication public class App { @Value("${server.host:localhost}") private St
阅读全文
摘要:1)SpringBootApplication默认扫描启动类所在包以及子包的所有类 2)如果要扫描启动类所在包的上一级可以通过scanBasePackages指定扫描路径 @SpringBootApplication(scanBasePackages={"com.edu.spring"}) publ
阅读全文
摘要:Spring Boot启动后有三种回调方式,即实现ApplicationContextInitializer、ApplicationRunner或CommandLineRunner接口。 一、实现ApplicationContextInitializer接口 1、方法1:调用SpringApplic
阅读全文
摘要:一、监听事件包含四个步骤 (1)自定义事件 (2)自定义监听器 (3)使Spring容器获取到监听器(4种方式) (4)发布事件 二、演示四种实现方式 1、启动方法中添加监听 1)自定义事件 public class MyApplicationEvent extends ApplicationEve
阅读全文
摘要:Spring Boot使用EnableAutoConfiguration和spring.factories解决了在一个模块中把其它模块的类装配成bean的问题 一、怎样实现 1、配置文件中enableautoconfiguration设置为true(默认为true) 2、引入其它模块的依赖 3、在其
阅读全文
摘要:一、Import注解可以装配Bean 1、单独使用Import注解,普通类作为参数 public class User {} public class Role {} public class MyConfig { @Bean public Runnable createRunnable(){ re
阅读全文
摘要:一、使用Conditional注解,自己定义Condition接口 1、实现接口 public class GBKCondition implements Condition { @Override public boolean matches(ConditionContext conditionC
阅读全文
摘要:一、读取默认配置文件中变量 即读取application.properties或者application.yaml中的变量 配置文件如下: local.ip=192.168.23.111 local.port=8125 local.address=${local.ip}:${local.port}
阅读全文