美文网首页
Spring Boot基础面试题

Spring Boot基础面试题

作者: 执着的逗比 | 来源:发表于2020-04-09 22:37 被阅读0次

    1、什么是Spring Boot?使用Spring Boot有什么好处?

    Spring Boot是基于Spring框架的快速开发整合包。

    1)编码变得简单
    快速搭建项目,无需编写繁琐的配置文件;

    2)配置变得简单
    遵循"习惯优于配置"原则,使用Spirng Boot只需很少的配置,大部分时候可以使用默认配置;

    3)部署变得简单
    内嵌Tomcat容器,项目部署可以直接用jar包运行(java -jar);

    4)监控变得简单
    Spring Boot 提供基于 http、ssh、telnet 对运行时的项目进行监控。

    2、SpringBoot中默认配置文件有几种格式,以及有哪些区别?

    有.yaml和.properties两种文件格式。

    1).yaml是key:value的形式,.properties是key=value的形式,.yaml更加简洁明了,查看更加形象;
    2).yaml中加载的属性是有顺序的,properties中的数据是无序的。在一些需要路径匹配的的配置中,顺序就显得尤为重要,此时我们一般采用.yaml。

    3、SpringBoot读取配置文件的方式有哪几种?

    1)使用注解@Value进行获取

    先在application.properties中配置属性

    book.author=石红英
    

    然后在controller类,调用属性,通过@Value修饰的变量会和book.author映射起来

    @Controller
    public class HelloController {
        @Value("${book.author}")
        private String name;
     
        @ResponseBody
        @RequestMapping("testConfigM1")
        public String testConfigM1(ModelMap modelMap) {     
            return name;
        }
    }
    

    2)使用bean封装

    先在application.properties配置属性

    student.name=你真漂亮 
    student.age=25
    

    定义一个bean,@ConfigurationProperties(prefix = "student")将配置文件的值映射到类上使用,prefix = "student"表示映射前缀是student

    @Component
    @ConfigurationProperties(prefix = "student")
    public class StudentProperties {
        private String name;
        private Integer age;
        // 省略get,set方法
    }
    

    然后在controller类,注入bean

       @Controller
       public class HelloController {
        @Autowired
        private StudentProperties studentProperties;
        @ResponseBody
        @RequestMapping("testConfigM2")
        public String testConfigM2(){
            System.out.println(studentProperties.getName()+"-----"+studentProperties.getAge());
            return  studentProperties.getName();
        }
    }
    

    3)使用Environment进行获取

    先在application.properties配置属性

    name2=白骨精
    

    然后在controller类,注入Environment类

    @Controller
    public class HelloController {
        @Autowired
        private Environment environment;
     
        @ResponseBody
        @RequestMapping("testConfigM3")
        public String testConfigM3(){
            return  environment.getProperty("name2");
        }
    }
    
    
    4、SpringBoot如何配置多套不同的拦截配置?

    SpringBoot的拦截器实现和SpringMVC一致,通过配置把拦截器注入到Spring容器中去; 但配置方式不一样:

    1)新建两个拦截器类

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    @Component
    public class MyInterceptor implements HandlerInterceptor {
     
        private static final Logger LOG= LoggerFactory.getLogger(MyInterceptor.class);
     
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            LOG.info("执行拦截器的preHandle方法");
            return true;
        }
     
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            LOG.info("执行拦截器的postHandle方法");
        }
     
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            LOG.info("执行拦截器的afterCompletion方法");
        }
    }
    
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    @Component
    public class MyInterceptorTwo implements HandlerInterceptor {
     
        private static final Logger LOG= LoggerFactory.getLogger(MyInterceptor.class);
     
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            LOG.info("执行拦截器的preHandle方法");
            return true;
        }
     
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
            LOG.info("执行拦截器的postHandle方法");
        }
     
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
            LOG.info("执行拦截器的afterCompletion方法");
        }
    }
    

    2)拦截器配置类

    
    import com.jd.m.tg.interceptor.MyInterceptor;
    import com.jd.m.tg.interceptor.MyInterceptorTwo;
    import lombok.AllArgsConstructor;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
     
    @Configuration
    @AllArgsConstructor
    public class InterceptorConfig extends WebMvcConfigurerAdapter{
     
        private MyInterceptor myInterceptor;
     
        private MyInterceptorTwo myInterceptorTwo;
     
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(myInterceptorTwo).addPathPatterns("/test/**");
            registry.addInterceptor(myInterceptor).addPathPatterns("/test/**");
        }
    }
    

    拦截器的添加顺序就是执行顺序,拦截器执行顺序第一个是myInterceptorTwo,第二个是myInterceptor。

    5、如何启动一个SpringBoot项目以及需要添加什么注解?

    直接像运行Java程序一样运行启动类就可以了。

    启动类就相当于我们Java程序的main方法, 而启动类中会用到一个关键的注解--@SpringBootApplication,@SpringBootApplication标注这个类是属于SpringBoot的启动类,从源代码中可以获悉,这个注解被@Configuration、@EnableAutoConfiguration、@ComponentScan 注解所修饰,换言之 Springboot 提供了统一的注解来替代以上三个注解。

    6、SpringBoot是否需要一个独立的web容器来运行?

    不需要,Spring Boot有内嵌的Tomcat服务器,不过要注意的是,当你打war包部署在服务器上的Tomcat的时候,要记得去除Spring Boot自带的Tomcat服务器。

    未完待续

    相关文章

      网友评论

          本文标题:Spring Boot基础面试题

          本文链接:https://www.haomeiwen.com/subject/mjfkmhtx.html