1. 程式人生 > >2019年3月springboot最新面試題(含詳細答案)

2019年3月springboot最新面試題(含詳細答案)

sources 其他 void jdk sets nic 功能 meta 屬性

springboot項目基礎面試題(一)

1.springboot與spring的區別.

引用自官方說法: java在集成spring等框架需要作出大量的配置,開發效率低,繁瑣.所以官方提出 spring boot的核心思想:習慣優於配置.可以快速創建開發基於spring框架的項目.或者支持可以不用或很少的spring配置即可.

2.springboot的核心功能與使用優點.

核心功能:
1.1: springboot項目為獨立運行的spring項目,java -jar xx.jar即可運行.
1.2: 內嵌servlet容器(可以選擇內嵌: tomcat ,jetty等服務器.).
1.3: 提供了starter的pom 配置 簡化了 maven的配置.

1.4: 自動配置spring容器中的bean.當不滿足實際開發場景,可自定義bean的自動化配置.
1.5: 準生產的應用監控(基於: ssh , http , telnet 對服務器運行的項目進行監控.).
1.6: springboot無需做出xml配置,也不是通過代碼生成來實現(通過條件註解.).
使用優點:
1.快速搭建項目,
2,與主流框架集成無需配置集成.
3.內嵌服務容器.
4.具有應用監控.
5.開發部署方便,後期與雲計算平臺集成方便(docket).

3.springboot中的application.properties配置文件是什麽,有哪些配置.

application.properties為boot項目中的一個系統自帶的全局屬性配置文件. 提供默認屬性重寫的作用. 可包含重寫系統tomcat,spring,springmvc,mybatis等諸多默認配置屬性: 列舉部分如下:

#全局配置文件: 重寫視圖解析器的資源地址.
#頁面默認前綴目錄
spring.mvc.view.prefix=/WEB-INF/jsp/
#?響應頁面默認後綴
spring.mvc.view.suffix=.jsp
#靜態資源目錄配置,
spring.mvc.static-path-pattern=/static/**

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

#默認支持的日誌記錄:
#logging.config=classpath:logback.xml 加載單獨的日誌配置文件.
logging.file=d:/test/log.log
logging.level.org.springframework.web=DEBUG

#提供jdbc的基本配置:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/c01?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

4.springboot中常用的starter的組件有哪些.

spring-boot-starter-parent //boot項目繼承的父項目模塊.
spring-boot-starter-web //boot項目集成web開發模塊.
spring-boot-starter-tomcat //boot項目集成tomcat內嵌服務器.
spring-boot-starter-test //boot項目集成測試模塊.
mybatis-spring-boot-starter //boot項目集成mybatis框架.
spring-boot-starter-jdbc //boot項目底層集成jdbc實現數據庫操作支持.
其他諸多組件,可到maven中搜索,或第三方starter組件到github上查詢 .....

5.springboot中的核心啟動主函數(main函數)的作用.用到哪些註解.註解的作用.

@SpringBootApplication
public class SpringBoot1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot1Application.class, args);
}
}
該主函數: 主要啟動springboot框架.用於加載容器和諸多默認組件.
用到核心註解: @SpringBootApplication . 作用:用於標識聲明一個springboot框架容器.

6.springboot中的常用配置入口有哪些?

            bootstrap.properties/bootstrap.yml //用於配置無需重寫的系統常量,例如springcloud項目用到的config配置中心的連接屬性等.加載優先級高於application.properties.
            application.properties/application.yml //用於配置重寫springboot項目的默認框架屬性,例如:重寫tomcat,springmvc,日誌框架等默認屬性.主要提供給spring框架加載使用.
         註: properties後綴名與yml後綴名配置文件二選一即可. 兩種不同格式的配置文件而已.

7.springboot項目需要兼容老項目(spring框架),該如何實現.

集成老項目spring框架的容器配置文件即可:
spring-boot一般提倡零配置.但是如果需要配置,也可增加:
@ImportResource({"classpath:spring1.xml" , "classpath:spring2.xml"})
註意:resources/spring1.xml位置.

8.需要加載外部配置文件中的自定義屬性,該如何實現.

需求一批量加載多個屬性.
步驟一: 首先需要自定義外部配置文件和其中的自定義屬性:
user.properties . 存放在resources目錄下:
內部:
#自定義配置其他屬性:
user.username=zhangsan
user.age=20
步驟二: 加載屬性到程序中:
springboot 1.5版本以及之前采用:@ConfigurationProperties(prefix="user",locations={"classpath:user.propeties"})
br/>@ConfigurationProperties(prefix="user",locations={"classpath:user.propeties"})
private String username;
private Integer age;
get/set封裝省略....
}
springboot 1.5版本以後采用如下:
@PropertySource(value ="classpath:user.properties")
@ConfigurationProperties(prefix = "user")@Component
br/>@Component
private String username;
private Integer age;
get/set封裝省略....
}
步驟三:
以上配置需要在main啟動函數類文件上激活配置: 新版本中默認開啟.
@EnableConfigurationProperties

需求二:如果加載單個屬性:
步驟一:省略.如上.
步驟二: @Value("${name}")
br/>@Value("${name}")

備註:以上外部文件屬性加載.切記註意中文亂碼問題.

9.springboot支持的默認日誌框架有哪些.可以進行哪些設置.

spring-boot: 默認采用Logback作為日誌框架.
配置即可:
logging.file=d:/test/log.log
logging.level.org.springframework.web=DEBUG
#logging.config=classpath:logback.xml 加載單獨的日誌配置文件.
其他配置項......

10.springboot項目的開發環境,生產環境配置該如何實現切換.

        profile配置:
spring-boot默認為了支持不同的配置環境. 
配置步驟: 1.提供環境:
    按照命名模板:application-{profile}.properties(例如: application-pro1.properties/application-pro2.properties)
    2.選擇激活的環境:
    application.properties中設置:spring.profiles.active=pro1

springboot項目WEB模塊開發面試題(二)

11.什麽是springboot項目中的自動配置與手動配置.如果需要手動配置該如何實現.

        自動配置: boot項目默認支持很多框架的集成. 添加組件即可. 只需要: 針對application.properties做出自動配置的屬性重寫即可完成.默認開啟.
        舉例:
         spring boot 采用自動配置集成freemarker模板引擎:(超級簡單)
        前提: 構建spring boot項目. 
        1.引入模板組件.
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId> 
        </dependency>
        2.編寫Controller和freemarker模板.位於resources/templates.

        手動配置: 手動自定義web組件,代替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{ 重寫方法即可. }

12.springboot項目web開發時如何集成web組件:servlet.filter.listener.

                前提: 自定義servlet(實現或繼承HttpServlet),filter(實現或繼承Filter),listener(實現或繼承ServletContextListener).
                方式一: 將以下組件直接提供在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());
                }
                方式二: 啟動類 實現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());
                    }
                }

                方式三:啟動類開啟掃描: @ServletComponentScan
                工具組件采用註解進行加載:
                @WebFilter(filterName = "customFilter", urlPatterns = "/*")
                @WebListener
                @WebServlet(name = "customServlet", urlPatterns = "/custom")

13.springboot+springmvc如何實現集成( 視圖模板: 基於JSP).官方不推薦,但工作有需求.

                1.引入pom.xml組件: 
                <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-tomcat</artifactId>
                </dependency>
                <dependency>
                         <groupId>org.apache.tomcat.embed</groupId>
                         <artifactId>tomcat-embed-jasper</artifactId>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>jstl</artifactId>
                </dependency>

                2. 增加application.properties配置:
                #?頁面默認前綴目錄
                spring.mvc.view.prefix=/WEB-INF/jsp/
                #?響應頁面默認後綴
                spring.mvc.view.suffix=.jsp
                #靜態資源目錄配置,  
                spring.mvc.static-path-pattern=/static/**

                3.編寫controller和jsp: 測試即可.
                (jsp頁面中可直接請求${pageContext.request.contextPath}.)

                補充: src/main/webapp/WEB-INF 創建該目錄.用於存儲頁面資源等.
                右擊項目--->Modules----> web(springboot無需xml. 指定webapp目錄為資源根目錄.) 並create artifacts ---> apply.即可

14.springboot+mybatis如何實現集成.(註意: 官方默認支持的spring-data框架不在此處介紹.)

                        步驟一: 填充pom.xml:
                                <dependency>
                                        <groupId>org.mybatis.spring.boot</groupId>
                                        <artifactId>mybatis-spring-boot-starter</artifactId>
                                        <version>1.3.2</version>
                                </dependency>

                                <dependency>
                                        <groupId>commons-dbcp</groupId>
                                        <artifactId>commons-dbcp</artifactId>
                                        <version>1.4</version>
                                </dependency>

                                <dependency>
                                        <groupId>org.springframework.boot</groupId>
                                        <artifactId>spring-boot-starter-jdbc</artifactId>
                                </dependency>

                                <dependency>
                                        <groupId>mysql</groupId>
                                        <artifactId>mysql-connector-java</artifactId>
                                        <version>5.1.40</version>
                                </dependency>

                                步驟二: application.properties
                                spring.datasource.driver-class-name=com.mysql.jdbc.Driver
                                spring.datasource.url=jdbc:mysql://localhost:3306/c01?useUnicode=true&characterEncoding=utf-8
                                spring.datasource.username=root
                                spring.datasource.password=root
                                spring.datasource.type=org.apache.commons.dbcp.BasicDataSource

                                mybatis.mapper-locations=classpath:mapper/*_mapper.xml
                                步驟三: com.hfxt.demo1.dao.UserDao
                                public interface UserDao {
                                        @ResultMap("BaseResultMap")
                                        @Select("select * from tb_book")
                                        List<User> selectAll(User u1);
                                }
                                步驟四: resources/mapper/User_mapper.xml
                                <?xml version="1.0" encoding="UTF-8"?>
                                <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
                                <mapper namespace="com.hfxt.demo1.dao.UserDao">
                                        <resultMap id="BaseResultMap" type="com.hfxt.demo1.domain.User">
                                                <id column="id" jdbcType="INTEGER" property="id"/>
                                                <result column="book_name" jdbcType="VARCHAR" property="bookName"/>
                                                <result column="book_price" jdbcType="VARCHAR" property="bookPrice"/>
                                        </resultMap>
                                </mapper>
                                步驟五: spring容器掃描接口.
                                @SpringBootApplication
                                @MapperScan("com.hfxt.demo1.dao")
                                public class Demo1Application { }
                                步驟六: 註入dao到service中.
                                //    @Resource
                                        @Autowired
                                        private UserDao userDao;

15.springboot項目的常用啟動部署方式有哪些.

        1.啟動main主函數即可.
        2.采用maven插件的啟動方式:新建maven啟動方式,並明確working directory為項目根目錄. Command Line為 spring-boot:run 即可ok啟動.  該方式必須為標準的tomcat容器.
        3.生產環境下的部署啟動方式:
        實際項目部署: 切換至父項目pom.xml根目錄中, mvn install package 打包. 將target中生成的war包項目部署至tomcat/webapp中,直接啟動tomcat即可. 註意:JAVA_HOME要設置為1.8 因為springboot2.0 必須至少jdk環境為1.8.

補充: 以上springboot項目的公共配置屬性清單地址如下:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

總結:

以上boot項目基礎內容純屬筆者親自手碼, 終於寫完一篇了.如有瑕疵希望與大家指正交流學習. 筆者微信: 15256075342.

2019年3月springboot最新面試題(含詳細答案)