springboot总结

1.springboot如何开启自动配置

1)、SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration

2)、@EnableAutoConfiguration 作用:

利用EnableAutoConfigurationImportSelector给容器中导入一些组件?

可以查看selectImports()方法的内容;

List configurations = getCandidateConfigurations(annotationMetadata, attributes);

获取候选的配置将 类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中;SpringFactoriesLoader.loadFactoryNames()扫描所有jar包类路径下 META‐INF/spring.factories把扫描到的这些文件的内容包装成properties对象从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中。

2、总结:

Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;

解析:

在SpringBoot的主程序类中必须要有注解@SpringBootApplication,在这个注解中有

开启自动配置注解,继续往下

在@EnableAutoConfiguration注解中导入了一个组件 AutoConfigurationImportSelector

自动配置的AutoConfiguration都是在文件spring.factories中自动配置的。

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值