1、SpringBoot2.x使用Dev-tool热部署 简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用 官方地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools 核心依赖包: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> 添加依赖后,在ide里面重启应用,后续修改后立刻能够生效 classloader 不被热部署的文件 一、/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates 二、指定文件不进行热部署 spring.devtools.restart.exclude=static/**,public/** 三、手工触发重启 spring.devtools.restart.trigger-file=trigger.txt 改代码不重启,经过一个文本去控制 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools-restart-exclude 注意点:生产环境不要开启这个功能,若是用java -jar启动,springBoot是不会进行热部署的 2、SpringBoot2.x配置文件讲解 简介:SpringBoot2.x常见的配置文件 xml、yml、properties的区别和使用 xml、properties、json、yaml 一、常见的配置文件 xx.yml, xx.properties, 1)YAML(Yet Another Markup Language) 写 YAML 要比写 XML 快得多(无需关注标签或引号) 使用空格 Space 缩进表示分层,不一样层次之间的缩进可使用不一样的空格数目 注意:key后面的冒号,后面必定要跟一个空格,树状结构 application.properties示例 server.port=8090 server.session-timeout=30 server.tomcat.max-threads=0 server.tomcat.uri-encoding=UTF-8 application.yml示例 server: port: 8090 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8 二、默认示例文件仅做为指导。 不要将整个内容复制并粘贴到您的应用程序中,只挑选您须要的属性。 三、参考:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#common-application-properties 若是须要修改,直接复制对应的配置文件,加到application.properties里面 3、SpringBoot注解配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 一、配置文件加载 方式一 一、Controller上面配置 @PropertySource({"classpath:resource.properties"}) 二、增长属性 @Value("${test.name}") private String name; 方式二:实体类配置文件 步骤: 一、添加 @Component 注解; 二、使用 @PropertySource 注解指定配置文件位置; 三、使用 @ConfigurationProperties 注解,设置相关属性; 四、必须 经过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。 @Autowired private ServerSettings serverSettings; 例子: @Configuration @ConfigurationProperties(prefix="test") @PropertySource(value="classpath:resource.properties") public class ServerConstant { 常见问题: 一、配置文件注入失败,Could not resolve placeholder 解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解, 默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入, 所以启动类最好放在根路径下面,或者指定扫描包范围 spring-boot扫描启动类对应的目录和子目录 二、注入bean的方式,属性名称和配置文件里面的key一一对应,就用加@Value 这个注解 若是不同,就要加@value("${XXX}") 4、SpringBoot个性化启动banner设置和debug日志 简介:自定义应用启动的趣味性日志图标和查看调试日志 一、启动获取更多信息 java -jar xxx.jar --debug 二、修改启动的banner信息 1)在类路径下增长一个banner.txt,里面是启动要输出的信息 2)在applicatoin.properties增长banner文件的路径地址 spring.banner.location=banner.txt 3)官网地址 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-banners 5、SpringBoot2.x配置全局异常实战 讲解:服务端异常讲解和SpringBoot配置全局异常实战 一、默认异常测试 int i = 1/0,不友好 二、异常注解介绍 @ControllerAdvice 若是是返回json数据 则用 RestControllerAdvice,就能够不加 @ResponseBody //捕获全局异常,处理全部不可知的异常 @ExceptionHandler(value=Exception.class) 6、SpringBoot2.x配置全局异常返回自定义页面 简介:使用SpringBoot自定义异常和错误页面跳转实战 一、返回自定义异常界面,须要引入thymeleaf依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 二、resource目录下新建templates,并新建error.html ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("error.html"); modelAndView.addObject("msg", e.getMessage()); return modelAndView; https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-error-handling 7、SpringBoot启动方式讲解和部署war项目到tomcat9 简介:SpringBoot常见启动方式讲解和部署war项目Tomcat 一、ide启动 二、jar包方式启动 maven插件: <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> 若是没有加,则执行jar包 ,报错以下 java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar no main manifest attribute, in spring-boot-demo-0.0.1-SNAPSHOT.jar 若是有安装maven 用 mvn spring-boot:run 项目结构 example.jar | +-META-INF | +-MANIFEST.MF +-org | +-springframework | +-boot | +-loader | +-<spring boot loader classes> +-BOOT-INF +-classes | +-mycompany | +-project | +-YourClasses.class +-lib +-dependency1.jar +-dependency2.jar 目录结构讲解 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#executable-jar-jar-file-structure 三、war包方式启动 1)在pom.xml中将打包形式 jar 修改成war <packaging>war</packaging> 构建项目名称 <finalName>xdclass_springboot</finalName> 2)tocmat下载 https://tomcat.apache.org/download-90.cgi 3)修改启动类 public class XdclassApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(XdclassApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(XdclassApplication.class, args); } } 4)打包项目,启动tomcat 四、启动容器介绍和第三方测试数据讲解 使用Jmter测试工具测试性能,QPS,TPS,RT https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-of-spring-boot-embedded-servlet-containers/ 8、深刻SpringBoot过滤器和Servlet3.0配置过滤器实战 简介:讲解SpringBoot里面Filter讲解和使用Servlet3.0配置自定义Filter实战 filter简单理解:人--->检票员(filter)---> 景点 一、SpringBoot启动默认加载的Filter characterEncodingFilter hiddenHttpMethodFilter httpPutFormContentFilter requestContextFilter 二、Filter优先级 Ordered.HIGHEST_PRECEDENCE Ordered.LOWEST_PRECEDENCE 低位值意味着更高的优先级 Higher values are interpreted as lower priority 自定义Filter,避免和默认的Filter优先级同样,否则会冲突 注册Filter的bean FilterRegistrationBean 同模块里面有相关默认Filter web->servlet->filter 三、自定义Filter 1)使用Servlet3.0的注解进行配置 2)启动类里面增长 @ServletComponentScan,进行扫描 3)新建一个Filter类,implements Filter,并实现对应的接口 4) @WebFilter 标记一个类为filter,被spring进行扫描 urlPatterns:拦截规则,支持正则 6)控制chain.doFilter的方法的调用,来实现是否经过放行 不放行,web应用resp.sendRedirect("/index.html"); 场景:权限控制、用户登陆(非前端后端分离场景)等 一、 官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners 9、Servlet3.0的注解原生Servlet实战 讲解:使用 Servlet3.0的注解自定义原生Servlet和Listener 一、自定义原生Servlet @WebServlet(name = "userServlet",urlPatterns = "/test/customs") public class UserServlet extends HttpServlet{ @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("custom sevlet"); resp.getWriter().flush(); resp.getWriter().close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } } 10、Servlet3.0的注解原生Listener监听器实战 简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战 一、自定义Listener(经常使用的监听器 servletContextListener、httpSessionListener、servletRequestListener) @WebListener public class RequestListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub System.out.println("======requestDestroyed========"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("======requestInitialized========"); } 11、SpringBoot2.X拦截器实战及新旧配置对比 简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解 一、@Configuration 继承WebMvcConfigurationAdapter(SpringBoot2.X以前旧版本) SpringBoot2.X 新版本配置拦截器 implements WebMvcConfigurer 二、自定义拦截器 HandlerInterceptor preHandle:调用Controller某个方法以前 postHandle:Controller以后调用,视图渲染以前,若是控制器Controller出现了异常,则不会执行此方法 afterCompletion:无论有没有异常,这个afterCompletion都会被调用,用于资源清理 三、按照注册顺序进行拦截,先注册,先被拦截 拦截器不生效常见问题: 1)是否有加@Configuration 2)拦截路径是否有问题 ** 和 * 3)拦截器最后路径必定要 “/**”, 若是是目录的话则是 /*/ Filter 是基于函数回调 doFilter(),而Interceptor则是基于AOP思想 Filter在只在Servlet先后起做用,而Interceptor够深刻到方法先后、异常抛出先后等 依赖于Servlet容器即web应用中,而Interceptor不依赖于Servlet容器因此能够运行在多种环境。 在接口调用的生命周期里,Interceptor能够被屡次调用,而Filter只能在容器初始化时调用一次。 Filter和Interceptor的执行顺序 过滤前->拦截前->action执行->拦截后->过滤后
可结合以前的博文 2018最新SpringBoot2.0教程(零基础入门)观看理解html
更多学习资料可参考:https://xdclass.net/html/course_catalogue.html?video_id=4前端