springboot web三大组件
分为三组件
1ServletRegistrationBean 注册Servlet组件
2FilterRegistrationBean 注册filter组件
3ServletListenerResgistrationBean 注册监听器
.springboot如何开启自动配置
1),在springboot启动的时候,加载主配置类过程中,开启了自动配置的功能.@EnableAutoConfiguration
SpringBoot是通过核心注解@SpringBootApplication,标识启动器。
而@SpringBootApplication内部则是通过@SpringBootConfiguration注解将启动器作为一个配置类。
然后,使用@ComponentScan注解定义包扫描规则,扫描相关注解 扫描范围 当前入口类所在的包 及 子包 (Spring组件扫描)。
再通过@EnableAutoConfiguration注解开启自动配置,初始化spring环境 springmvc环境 ,该注解只能扫描当前类,因此需要配合@ComponentScan使用
@EnableAutoConfiguration注解则是通过@AutoConfigurationPackage注解定义了spring boot自动导入组件,而该注解则通过@Import({Registrar.class})中的Registrar实现了动态注入Bean
@EnableAutoConfiguration注解又通过@Import({AutoConfigurationImportSelector.class})注解真正开启自动导入配置
而@Import({AutoConfigurationImportSelector.class})通过selectImports() 方法完成自动配置的导入
此方法则通过this.getAutoConfigurationEntry() 执行自动配置操作
此方法中则是通过 List configurations = this.getCandidateConfigurations(annotationMetadata, attributes) 真正进行自动配置操作
而 getCandidateConfigurations() 则是通过 SpringFactoriesLoader.loadFactoryNames() 完成配置的加载。
2.总结springboot如何处理异常的
一、自定义错误页面
SpringBoot默认的处理异常机制:SpringBoot默认的已经提供了一套处理异常的机制。一旦程序出现了异常SpringBoot就像/error的url发送请求。在springboot中提供了一个叫BasicExceptionController来处理/error请求,然后跳转到默认显示异常的页面来展示异常信息。
如果我们需要将所有的异常统一跳转到自定义的错误页面,需要在src/main/resources/templates目录下创建error.html页面。并添加这个标签。
二、@ExceptionHandle注解处理异常
只需要在controller中添加这样的方法:
1 @ExceptionHandler(value={java.lang.ArithmeticException.class})
2 public ModelAndView arithmeticExceptionHandler(Exception e) {
3 ModelAndView mv = new ModelAndView();
4 mv.addObject(“error”, e.toString());
5 mv.setViewName(“error”);
6 return mv;
7 }
三、@ControllerAdvice+@ExceptionHandler注解处理异常
需要创建一个能够处理异常的全局异常类。在该类上需要添加@ControllerAdvice注解。
1 @ControllerAdvice
2 public class GlobalException {
3 @ExceptionHandler(value={java.lang.ArithmeticException.class})
4 public ModelAndView arithmeticExceptionHandler(Exception e) {
5 ModelAndView mv = new ModelAndView();
6 mv.addObject(“error”, e.toString());
7 mv.setViewName(“error”);
8 return mv;
9 }
10 }
四、配置SimpleMappingExceptionResolver处理异常
在全局异常类中添加一个方法完成异常类的统一处理
1 @Configuration
2 public class GlobalException {
3 public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() {
4 SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
5 Properties properties = new Properties();
6 properties.put(“java.lang.ArithmeticException”, “error”);
7 resolver.setExceptionMappings(properties);
8 return resolver;
9 }
10 }
五、自定义HandlerExceptionResolver类处理异常
需要在全局异常类中实现HandlerExceptionResolver接口
复制代码
1 @Configuration
2 public class GlobalException implements HandlerExceptionResolver {
3 @Override
4 public ModelAndView resolverException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) {
5 ModelAndView mv = new ModelAndView();
6 if(exception instanceof ArithmeticException) {
7 mv.setViewName(“error”);
8 }
9 mv.addObject(“error”, exception.toString());
10 return mv;
11 }
12 }
3.在springboot中,如何使用拦截器?
在每个项目中,拦截器都是我们经常会去使用的东西,基本上任一一个项目都缺不了拦截器的使用。
如日志记录、登录验证,session验证等,都需要拦截器来拦截URL请求,那springboot中的拦截器是如何去使用的呢,我们一起试试。
首先,我们去创建一个名为LoginInterceptor的拦截器,来过滤请求,我们创建的拦截器要去实现HandlerInterceptor接口,然后定义我们的方法
public class LoginInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);
/**
进入controller层之前拦截请求
@param httpServletRequest
@param httpServletResponse
@param o
@return
@throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
log.info("---------------------开始进入请求地址拦截----------------------------");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
log.info(“处理请求完成后视图渲染之前的处理操作”);
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
log.info(“视图渲染之后的操作”);
}
}
这里,我们不去写过多的业务逻辑,只需要知道拦截器是否生效即可
接下来我们需要将LoginInterceptor拦截器添加到SpringBoot的配置中,让SpringBoot项目有这么一个拦截器存在,我们新创建一个WebAppConfig,
将拦截器的配置以及拦截路径配置好
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
}
}
这个方法将我们之前写好的登录拦截器添加到了配置中去,接下来,我们去发送请求,观察控制台
发现请求确实被拦截器拦截成功了,然后我们就可以根据业务需要去在拦截器中写我们想要的方法了
4.在springboot中,如何扩展springmvc的功能
SpringBoot中的SpringMVC扩展功能
在之前我们在springmvc中配置视图解析器和拦截器的时候都是需要在xml文件中编写一些配置文件以达到扩展的功能。如下所示添加视图映射。
<mvc:view-controller path="/hello" view-name=“success”/>
mvc:interceptors
mvc:interceptor
<mvc:mapping path="/hello"/>
</mvc:interceptor>
</mvc:interceptors>
而SpringBoot为了简单化这类的功能,在SpringBoot中自动加载了许多以前SpringMVC需要手动配置的东西,例如视图解析器,消息转换器等。
但是我们在有些的时候我们不能用SpringBoot里面的mvc扩展的功能,所以我们就需要在SpringBoot中编写SpringMVC的扩展功能。
在之前的springboot中编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型进行扩展配置SpringMvc中的自定义属性配置。但是当前版本将这个类废弃,根据java8的特性,接口不需要实现类重写接口中的全部方法。所以扩展SpringMvc的属性就可以实现WebMvcConfigurer接口。实现对应的方法就可以了。
代码实现
第一步:编写MVC扩展类
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc 不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求来到 success
registry.addViewController("/atguigu").setViewName(“success”);
}
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册在容器
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName(“login”);
registry.addViewController("/index.html").setViewName(“login”);
}
};
return adapter;
}
}
编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc,如果标注了@EnableWebMvc的类就会全部的接管Mvc的功能,SpringBoot就不会自动的为我们进行自动的配置;
实现WebMvcConfigurer类,用来说明这个类是用来配置MVC的扩展容器。(有些视频上面使用的是继承WebMvcConfigurerAdapter的类,但是我们会发现这个类好像要被淘汰了,所以SpringBoot推荐我们去实现WebMvcConfigurer类,或者继承WebMvcConfigurationSupport)