零基础快速入门SpringBoot2.0教程 (二)

一、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
 	
 	不被热部署的文件
 		1、/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates
 		2、指定文件不进行热部署 spring.devtools.restart.exclude=static/**,public/**
 		3、手工触发重启 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是不会进行热部署的
 
 
二、SpringBoot2.x配置文件讲解
	简介:SpringBoot2.x常见的配置文件 xml、yml、properties的区别和使用
 
		xml、properties、json、yaml
		1、常见的配置文件 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 
 
 
		2、默认示例文件仅作为指导。 不要将整个内容复制并粘贴到您的应用程序中,只挑选您需要的属性。
		
		3、参考:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#common-application-properties
 
		如果需要修改,直接复制对应的配置文件,加到application.properties里面
 
 
 
三、SpringBoot注解配置文件自动映射到属性和实体类实战
	简介:讲解使用@value注解配置文件自动映射到属性和实体类
			
	1、配置文件加载
		方式一
			1、Controller上面配置
			   @PropertySource({"classpath:resource.properties"})
			2、增加属性
				 @Value("${test.name}")
		 		 private String name;
 
		方式二:实体类配置文件
		步骤:
			1、添加 @Component 注解;
			2、使用 @PropertySource 注解指定配置文件位置;
			3、使用 @ConfigurationProperties 注解,设置相关属性;
 
			4、必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。
				@Autowired
	    		private ServerSettings serverSettings;
 
	    		例子:
	    			@Configuration
					@ConfigurationProperties(prefix="test")
					@PropertySource(value="classpath:resource.properties")
					public class ServerConstant {
 
 
			常见问题:
				1、配置文件注入失败,Could not resolve placeholder
					解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解, 
					默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入,
					因此启动类最好放在根路径下面,或者指定扫描包范围
					spring-boot扫描启动类对应的目录和子目录
				2、注入bean的方式,属性名称和配置文件里面的key一一对应,就用加@Value 这个注解
					如果不一样,就要加@value("${XXX}")
 
 
 
四、SpringBoot个性化启动banner设置和debug日志
	简介:自定义应用启动的趣味性日志图标和查看调试日志
 
		1、启动获取更多信息 java -jar xxx.jar --debug
		
		2、修改启动的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
 
 
五、SpringBoot2.x配置全局异常实战
	讲解:服务端异常讲解和SpringBoot配置全局异常实战
 
		1、默认异常测试  int i = 1/0,不友好
		
		2、异常注解介绍
			@ControllerAdvice 如果是返回json数据 则用 RestControllerAdvice,就可以不加 @ResponseBody
			
			//捕获全局异常,处理所有不可知的异常
			@ExceptionHandler(value=Exception.class)
 
六、SpringBoot2.x配置全局异常返回自定义页面
	简介:使用SpringBoot自定义异常和错误页面跳转实战
 
	1、返回自定义异常界面,需要引入thymeleaf依赖
		<dependency>
		   <groupId>org.springframework.boot</groupId>
		   <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	2、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
 
 
七、SpringBoot启动方式讲解和部署war项目到tomcat9
简介:SpringBoot常见启动方式讲解和部署war项目Tomcat
 
	1、ide启动
	2、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
 
	3、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
 
	4、启动容器介绍和第三方测试数据讲解
	
	使用Jmter测试工具测试性能,QPS,TPS,RT
 
	https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-of-spring-boot-embedded-servlet-containers/
 
 
八、深入SpringBoot过滤器和Servlet3.0配置过滤器实战
	简介:讲解SpringBoot里面Filter讲解和使用Servlet3.0配置自定义Filter实战
		
		filter简单理解:人--->检票员(filter)---> 景点
 
		1、SpringBoot启动默认加载的Filter 
			characterEncodingFilter
			hiddenHttpMethodFilter
			httpPutFormContentFilter
			requestContextFilter
				
		2、Filter优先级
 
			Ordered.HIGHEST_PRECEDENCE
			Ordered.LOWEST_PRECEDENCE
 
			低位值意味着更高的优先级 Higher values are interpreted as lower priority
			自定义Filter,避免和默认的Filter优先级一样,不然会冲突
 
			注册Filter的bean FilterRegistrationBean
			同模块里面有相关默认Filter
				web->servlet->filter
 
 
		3、自定义Filter
			1)使用Servlet3.0的注解进行配置
			2)启动类里面增加 @ServletComponentScan,进行扫描
			3)新建一个Filter类,implements Filter,并实现对应的接口
			4) @WebFilter 标记一个类为filter,被spring进行扫描 
				urlPatterns:拦截规则,支持正则
 
			6)控制chain.doFilter的方法的调用,来实现是否通过放行
			   不放行,web应用resp.sendRedirect("/index.html");
				场景:权限控制、用户登录(非前端后端分离场景)等
			
 
1、	官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-embedded-container-servlets-filters-listeners
 
 
九、Servlet3.0的注解原生Servlet实战
	讲解:使用 Servlet3.0的注解自定义原生Servlet和Listener
		1、自定义原生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);
			     }
			}
 
十、Servlet3.0的注解原生Listener监听器实战
		简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战
 
		1、自定义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========");
				
			}
 
十一、SpringBoot2.X拦截器实战及新旧配置对比
	简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解
	
	1、@Configuration
		继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本)
 
		SpringBoot2.X 新版本配置拦截器 implements WebMvcConfigurer
 
	2、自定义拦截器 HandlerInterceptor
		preHandle:调用Controller某个方法之前
		postHandle:Controller之后调用,视图渲染之前,如果控制器Controller出现了异常,则不会执行此方法
		afterCompletion:不管有没有异常,这个afterCompletion都会被调用,用于资源清理
	
	3、按照注册顺序进行拦截,先注册,先被拦截
 
	拦截器不生效常见问题:
		1)是否有加@Configuration
		2)拦截路径是否有问题 **  和 * 
		3)拦截器最后路径一定要 “/**”, 如果是目录的话则是 /*/
	Filter
		是基于函数回调 doFilter(),而Interceptor则是基于AOP思想
		Filter在只在Servlet前后起作用,而Interceptor够深入到方法前后、异常抛出前后等
		依赖于Servlet容器即web应用中,而Interceptor不依赖于Servlet容器所以可以运行在多种环境。
	
		在接口调用的生命周期里,Interceptor可以被多次调用,而Filter只能在容器初始化时调用一次。
		
		Filter和Interceptor的执行顺序
     	
    	过滤前->拦截前->action执行->拦截后->过滤后

可结合之前的博文 2018最新SpringBoot2.0教程(零基础入门)观看理解

更多学习资料可参考:https://xdclass.net/html/course_catalogue.html?video_id=4

https://ke.qq.com/course/299498 

猜你喜欢

转载自www.cnblogs.com/xdclass/p/9569584.html