springboot面试题

it2022-05-05  121

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=8081server.servlet.context-path=/sb2

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

#提供jdbc的基本配置:spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/c01?useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=rootspring.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函数)的作用.用到哪些注解.注解的作用.

@SpringBootApplicationpublic 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=zhangsanuser.age=20步骤二: 加载属性到程序中:springboot 1.5版本以及之前采用:br/>@ConfigurationProperties(prefix="user",locations={"classpath:user.propeties"})public class User {private String username;private Integer age;get/set封装省略....}springboot 1.5版本以后采用如下: @PropertySource(value ="classpath:user.properties")@ConfigurationProperties(prefix = "user")br/>@Componentpublic class User {private String username;private Integer age;get/set封装省略....}步骤三:以上配置需要在main启动函数类文件上激活配置: 新版本中默认开启.@EnableConfigurationProperties

需求二:如果加载单个属性:步骤一:省略.如上.步骤二: br/>@Value("${name}")private String name;.

备注:以上外部文件属性加载.切记注意中文乱码问题.

9.springboot支持的默认日志框架有哪些.可以进行哪些设置.

spring-boot: 默认采用Logback作为日志框架.配置即可:logging.file=d:/test/log.loglogging.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包项目

转载于:https://www.cnblogs.com/jpf111/p/11191534.html


最新回复(0)