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版本以及之前采用: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/>@Component
public 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.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

https://blog.51cto.com/14185725/2364360?cid=729911

springboot项目基础面试题的更多相关文章

  1. 补习系列(1)-springboot项目基础搭建课

    目录 前言 一.基础结构 二.添加代码 三.应用配置 四.日志配置 五.打包部署 小结 前言 springboot 最近火的不行,目前几乎已经是 spring 家族最耀眼的项目了.抛开微服务.技术社区 ...

  2. SpringBoot学习(一)—— web项目基础搭建

    首先我们在浏览器打开这个网站 https://start.spring.io/ 打开后可以看到以下页面 在这里我们可以快速搭建一个SpringBoot基础项目,填写和选择完相应的信息后,我们点击那个绿 ...

  3. 从SpringBoot到SpringCloudAlibaba简明教程(一):初识SpringBoot及其基础项目构建

    前言 Spring框架的大名想必大家早已如雷贯耳,我们来看一下springboot诞生的初衷,以及它在springframe的基础上解决了哪些痛点. 很久以前,J2EE还是java企业级应用的标准规范 ...

  4. springboot:基础学习一 linux下后台启动springboot项目

    我们知道启动springboot的项目有三种方式: 运行主方法启动 使用命令 mvn spring-boot:run”在命令行启动该应用 运行“mvn package”进行打包时,会打包成一个可以直接 ...

  5. SpringBoot项目创建及入门基础

    一:快速构建springboot项目 进入https://start.spring.io/,选择相应的springboot版本,包名,项目名,依赖 图中选择web,利用tomcat服务器进行开发 sp ...

  6. 这 5 个开源的能挣钱的 SpringBoot 项目,真TMD香!

    不得不佩服 Spring Boot 的生态如此强大,今天我给大家推荐几款 Gitee 上优秀的后台开源版本的管理系统,小伙伴们再也不用从头到尾撸一个项目了,简直就是接私活,挣钱的利器啊. SmartA ...

  7. Java基础面试题(史上最全、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  8. springboot:快速构建一个springboot项目

    前言: springboot作为springcloud的基础,springboot的热度一直很高,所以就有了这个springboot系列,花些时间来了解和学习为自己做技术储备,以备不时之需[手动滑稽] ...

  9. 打造SpringBootTemplate(SpringBoot项目的模版)

    随着框架使用的不断的更新,后面使用SpringBoot会多,这边准备构建一个SpringBoot项目使用的模版. 所谓模版,和之前一样,就是一个最简单的项目,包含所有最简单的空实现. 做模版的时候参考 ...

随机推荐

  1. css改变input输入框placeholder值颜色

    ::-webkit-input-placeholder { /* WebKit browsers */ color: #fff; } :-moz-placeholder { /* Mozilla Fi ...

  2. spring中的Filter使用

    https://blog.csdn.net/bibiwannbe/article/details/81302920

  3. unity学习 5.x解包

    using System.Collections;using System.Collections.Generic;using UnityEngine; public class bundleload ...

  4. 使用GitHub管理Repository

    对已有的Repository进行修改 将已有的项目克隆到本地 git clone https://github.com/username/project-name 或者同步已经下载的项目 git pu ...

  5. <黑马新秀>Spring学习日志

    # 用于梳理Spring知识点 Spring是分层的Java EE应用全栈轻量级开源框架,以IoC(Inverse Of Control反转控制)和AOP(Aspect Oriented Progra ...

  6. 通过javascri实现输入框只能输入数字

    输入框只能输入数字 <input type="text" onkeyup="value=value.replace(/[^\d]/g,'');"> ...

  7. Codeforces 1288B - Yet Another Meme Problem

    题目大意: 令conc(a,b)函数得出的结果为将ab拼接得到的数字. 例如:conc(12,23)=1223 a和b不会包括前导0! 接下来,你已知A和B,问有多少对的(a,b)满足 1≤a≤A , ...

  8. php利用curl发送 post get del put patch 请求

    因为需要在php开发中对接其它接口需要用php  curl去对接其它接口  我把他们封装成函数 希望能对大家有所帮助 这里面是封装好的  会自动把data进行转成json格式   同时解码成php数组 ...

  9. 网络安全与CTF在线学习资源网站

    http://www.sec-wiki.com/skill/ 安全技能(里面渗透逆向编程都有介绍) http://blog.knownsec.com/Knownsec_RD_Checklist/ 知道 ...

  10. Java分层架构的使用规则

    原文章引用地址:http://blog.csdn.net/ygzk123/article/details/7816511 三层结构的程序不是说把项目分成DAL, BLL, WebUI三个模块就叫三层了 ...