当前位置: 移动技术网 > IT编程>开发语言>Java > 互联网大厂Java面试题集—Spring boot常见面试题(二)

互联网大厂Java面试题集—Spring boot常见面试题(二)

2019年12月06日  | 移动技术网IT编程  | 我要评论

spring boot的核心功能与使用优点?

核心功能:

1)spring boot项目为独立运行的spring项目,java -jar xx.jar即可运行。

2)内嵌servlet容器(可以选择内嵌: tomcat,jetty等服务器)。

3)提供了starter的pom配置简化了maven的配置。

4)自动配置spring容器中的bean。当不满足实际开发场景,可自定义bean的自动化配置。

5)准生产的应用监控(基于:sshhttptelnet对服务器运行的项目进行监控)。

6)spring boot无需做出xml配置,也不是通过代码生成来实现(通过条件注解)。

 

使用优点:

1)快速搭建项目,与主流框架集成无需配置集成。内嵌服务容器,具有应用监控,开发部署方便,后期与云计算平台集成方便(docket)。

2)使用javaconfig有助于避免使用xml。

3)避免大量的maven导入和各种版本冲突。

4)没有单独的web服务器需要。这意味着你不再需要启动tomcatglassfish或其他任何东西。

5)需要更少的配置因为没有web.xml文件。只需添加用@configuration注释的类,然后添加用@bean注释的方法,spring将自动加载对象并像以前一样对其进行管理。您甚至可以将@autowired添加到bean方法中,以使 spring 自动装入需要的依赖关系中。

6)基于环境的配置使用这些属性,您可以将您正在使用的环境传递到应用程序:-dspring.profiles.active = {enviornment}。在加载主应用程序属性文件后,spring将在(application{environment}.properties)中加载后续的应用程序属性文件。

 

什么是spring boot项目中的自动配置与手动配置?如果需要手动配置该如何实现?.

1. 自动配置: spring boot项目默认支持很多框架的集成,添加组件即可。只需要: 针对application.properties做出自动配置的属性重写即可完成,默认开启。关注微信公众号“java精选”(w_z90110)。

举例:

spring boot采用自动配置集成freemarker模板引擎,前提: 构建spring boot项目。 

1)引入模板组件

<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-freemarker</artifactid> 
</dependency>

2)编写controller和freemarker模板,位于resources/templates

 

2. 手动配置: 手动自定义web组件,代替spring boot项目默认支持的组件与配置。

举例:

采用手动配置参数,集成freemarker模板引擎.

1)前提: spring-boot-starter-web引入;

2)编写过程类似于springmvc;

3)额外的springmvc的容器配置:

默认基于spring boot的基本默认配置即可(需要修改: 位于application.properties)。需要手动编写类似于spring容器可以: 

@configuration
public class mvcconfiguration extends webmvcconfigureradapter{ 
    //视图解析器默认地址为: /resources , /static , /templates, /public,/meta
    @bean
    public internalresourceviewresolver defaultresolver(){
        internalresourceviewresolver resourceviewresolver = new internalresourceviewresolver();
        resourceviewresolver.setprefix("classpath:/templates/");
        resourceviewresolver.setsuffix(".html");
        return resourceviewresolver;
    }
    //解析视图时,默认从以上地址中依次寻找视图资源加载,如果自定义例如freemarker模板视图解析器的资源地址,那么:
    @bean
    public freemarkerviewresolver defaultresolver(){
        freemarkerviewresolver freemarkerviewresolver = new freemarkerviewresolver();
        //      freemarkerviewresolver.setprefix("classpath:/views/");
        freemarkerviewresolver.setsuffix(".html");
        freemarkerviewresolver.setcontenttype("text/html;charset=utf-8");
        return freemarkerviewresolver;
    }
    @bean
    public freemarkerconfigurer freemarkerconfigurer(){
        freemarkerconfigurer configurer = new freemarkerconfigurer();
        configurer.settemplateloaderpaths("classpath:/views/");
        configurer.setdefaultencoding("utf-8");
        return configurer;
    }
    //如果不设置静态资源目录,默认: classpath: /static/  ,  classpath: /public/  ,  classpath: /resources/  ,  classpath: /meta-inf/resources/  
    @override
    public void addresourcehandlers(resourcehandlerregistry registry) {
        registry.addresourcehandler("/image/**").addresourcelocations("classpath:/static/image/");
        registry.addresourcehandler("/css/**").addresourcelocations("classpath:/static/css/");
        registry.addresourcehandler("/js/**").addresourcelocations("classpath:/static/js/");
    }
}

 

以上手动配置总结: 如果想要完全自定义,接管spring boot中的所有web配置,可以:

@configuration: 创建mvc适配器子类的对象并绑定至spring容器中。
@enablewebmvc: 扫描spring容器中的mvc适配器子类对象。
public class mvcconfiguration extends webmvcconfigureradapter{ 重写方法即可. }

 

如何使用maven来构建一个spring boot程序?

就像引入其他库一样,我们可以在maven工程中加入spring boot依赖。然而,最好是从spring-boot-starter-parent项目中继承以及声明依赖到spring boot starters。这样做可以使我们的项目可以重用spring boot的默认配置。

继承spring-boot-starter-parent项目依赖很简 –我们只需要在pom.xml中定义一个parent节点:

<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>2.1.1.release</version>
</parent>

 

我们可以在maven central中央仓库中找到spring-boot-starter-parent的最新版本。

 

使用starter父项目依赖很方便,但是有时候不是可行的。关注微信公众号“java精选”(w_z90110)。如果我们公司都要求项目继承标准 pom,我们就不能依赖spring boot starter了。

这种情况,我们可以通过对pom元素的依赖管理来处理:

<dependencymanagement>
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-dependencies</artifactid>
            <version>2.1.1.release</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencymanagement>

最后可以添加spring boot starter中一些依赖,之后就可以启动项目了。

 

spring boot中如何禁用某些自动配置特性?

1)禁用某些自动配置特性使用@enableautoconfiguration注解的exclude属性来指明。例如,下面的代码段是使datasourceautoconfiguration无效:

// other annotations
@enableautoconfiguration(exclude = datasourceautoconfiguration.class)
public class myconfiguration { }

2)使用@springbootapplication注解,将@enableautoconfiguration作为元注解的项来启用自动化配置,使用相同名字的属性来禁用自动化配置:

// other annotations
@springbootapplication(exclude = datasourceautoconfiguration.class)
public class myconfiguration { }

3)使用spring.autoconfigure.exclude环境属性来禁用自动化配置。

application.properties中的这项配置内容增加如下:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration

 

如何将spring boot web应用程序部署为jar或war文件?

通常,将web应用程序打包成war文件,然后将它部署到另外的服务器上。这样做使得我们能够在相同的服务器上处理多个项目。当cpu和内存有限的情况下,这是一种最好的方法来节省资源。然而,事情发生了转变。现在的计算机硬件相比起来已经比较廉价,并且现在的注意力大多转移到服务器配置上。部署中对服务器配置的一个细小的失误都会导致无可预料的灾难发生。

spring通过提供插件来解决这个问题,也就是spring-boot-maven-plugin来打包web应用程序到一个额外的 jar 文件当中。为了引入这个插件,只需要在pom.xml中添加一个plugin属性:

<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>

有了这个插件,可以在执行package步骤后得到一个jar包。这个jar包包含所需的所有依赖以及一个嵌入的服务器。因此,无需担心去配置一个额外的服务器了。可以通过运行一个普通的jar包来启动应用程序。

需要注意的是,为了打包成jar文件,pom.xml中的packgaing属性必须定义为jar:

<packaging>jar</packaging>

如果不定义这个元素默认值也是jar。

如果想构建一个war文件,将packaging元素修改为war:

<packaging>war</packaging>

并且需要将容器依赖从打包文件中移除:

<dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-tomcat</artifactid>
     <scope>provided</scope>
</dependency>

执行maven的package步骤之后,我们得到一个可部署的war文件。

 

如何使用spring boot实现分页和排序?

使用spring boot实现分页非常简单。使用spring data-jpa可以实现将可分页的传递给存储库方法。

 

spring boot中的监视器是什么?如何监视所有spring boot微服务?

spring boot actuator是spring启动框架中的重要功能之一。spring boot监视器可帮助您访问生产环境中正在运行的应用程序的当前状态。有几个指标必须在生产环境中进行检查和监控。即使一些外部应用程序可能正在使用这些服务来向相关人员触发警报消息。监视器模块公开了一组可直接作为http url访问的rest端点来检查状态。

 

spring boot 提供监视器端点以监控各个微服务的度量。这些端点对于获取有关应用程序的信息(如它们是否已启动)以及它们的组件(如数据库等)是否正常运行很有帮助。但是,使用监视器的一个主要缺点或困难是必须单独打开应用程序的知识点以了解其状态或健康状况。想象一下涉及50个应用程序的微服务,管理员将不得不击中所有50个应用程序的执行终端。为了帮助我们处理这种情况,可以集成开源项目,它建立在spring boot actuator之上,它提供了一个web ui,使我们能够可视化多个应用程序的度量。

 

spring boot的actuator是做什么的?

本质上,actuator通过启用production-ready功能使得spring boot应用程序变得更有生命力。这些功能允许我们对生产环境中的应用程序进行监视和管理。

集成spring boot actuator到项目中需要做的只是将spring-boot-starter-actuator starter引入到pom.xml文件当中:

<dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-actuator</artifactid>
</dependency>

 

spring boot actuaor可以使用http或者jmx endpoints来浏览操作信息。大多数应用程序都是用http,作为endpoint的标识以及使用/actuator前缀作为 url路径。

这里有一些常用的内置endpoints actuator:

 

spring boot项目web开发时如何集成web组件:servlet.filter.listener? 

自定义servlet(实现或继承httpservlet),filter(实现或继承filter),listener(实现或继承servletcontextlistener)。

1)将以下组件直接提供在main()启动类中,用于加载:

@bean
public servletregistrationbean servletregistrationbean() {
    return new servletregistrationbean(new customservlet(), "/custom");
}
@bean
public filterregistrationbean filterregistrationbean() {
    return new filterregistrationbean(new customfilter(), servletregistrationbean());
}
@bean
public servletlistenerregistrationbean<customlistener> servletlistenerregistrationbean() {
    return new servletlistenerregistrationbean<customlistener>(new customlistener());
}

2)启动类实现servletcontextinitializer,重写onstartup():

@springbootapplication
public class springbootdemo102application implements servletcontextinitializer {
    @override
    public void onstartup(servletcontext servletcontext) throws servletexception {
        servletcontext.addservlet("customservlet", new customservlet()).addmapping("/custom");
        servletcontext.addfilter("customfilter", new customfilter())
        .addmappingforservletnames(enumset.of(dispatchertype.request), true, "customservlet");
        servletcontext.addlistener(new customlistener());
    }
}

3)启动类开启扫描: @servletcomponentscan

工具组件采用注解进行加载:

@webfilter(filtername = "customfilter", urlpatterns = "/*")
@weblistener
@webservlet(name = "customservlet", urlpatterns = "/custom")

  

spring boot中的application.properties配置文件是什么,有哪些配置?

application.propertiesspring boot项目中的一个系统自带的全局属性配置文件,提供默认属性重写的作用。可包含重写系统tomcatspringspringmvcmybatis等诸多默认配置属性。列举部分如下:

#全局配置文件: 重写视图解析器的资源地址
#页面默认前缀目录
spring.mvc.view.prefix=/web-inf/jsp/

#响应页面默认后缀
spring.mvc.view.suffix=.jsp

#静态资源目录配置,
spring.mvc.static-path-pattern=/static/**

#默认支持的日志记录:
#logging.config=classpath:logback.xml 加载单独的日志配置文件
logging.file=d:/test/log.log
logging.level.org.springframework.web=debug

#tomcat服务器的配置:
server.port=8081
server.servlet.context-path=/yoodb

#提供jdbc的基本配置:
spring.datasource.driver-class-name=com.mysql.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/yoodb01?useunicode=true&characterencoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=org.apache.commons.dbcp.basicdatasource

#提供mybatis的属性配置: 扫描
mybatis.mapper-locations=classpath:mapper/*_mapper.xml

 

如何在spring boot中添加通用的js代码?

在源文件夹下创建一个名为static的文件夹。之后把静态的内容放在这里面。

例如,yoodb.js的路径是resources\static\js\yoodb.js,可以参考它在jsp中的使用方法:

错误:hal browser gives me unauthorized error - full authenticaition is required to access this resource.,该如何来修复这个错误呢?

两种方式:

1)关闭安全验证,打开application.properties文件增加内容如下:

management.security.enabled:false

2)在日志中搜索密码并传递至请求头中

 

spring和spring boot有什么不同?

spring框架提供多种特性使得web应用开发变得更简便,包括依赖注入、数据绑定、切面编程、数据存取等。

但随着时间推移,spring生态变得越来越复杂了,并且应用程序所必须的配置文件也令人觉得可怕。这就是spirng boot派上用场的地方了–它使得spring的配置变得更轻而易举。

实际上,spring是unopinionated(予以配置项多,倾向性弱) 的,spring boot在平台和库的做法中更opinionated,使得我们更容易上手。

简单总结两条spring boot带来的好处:

1)根据classpath中的artifacts的自动化配置应用程序;

2)提供非功能性特性例如安全和健康检查给到生产环境中的应用程序。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网