
7.SpringBoot面试题1. SpringBoot自动配置原理是什么2.开启SpringBoot特性有哪几种方式3.SpringBoot如何解决跨域问题4.SpringBoot如何整合Mybatis5.SpringBoot怎么实现多环境配置6.SpringBoot的核心注解有哪些7.SpringBoot中静态首页或者静态资源默认位置可以放在哪里8.SpringBoot的核心配置文件有哪几个他们的区别是什么9.SpringBoot的优点有哪些1. SpringBoot自动配置原理是什么SpringBoot的项目启动注解是SpringBootApplication,其实就是由下面三个注解组成的SpringBootConfigurationEnableAutoConfigurationComponentScan其中EnableAutoConfiguration是实现自动配置的入口该注解又通过Import注解导入了AutoConfigurationImportSelector,该类会加载META-INF/spring.factories的配置信息。然后根据Condition条件来筛选出有效的自动配置类。每一个自动配置类结合对应的xxxProperties.java读取配置文件进行自动配置功能。2.开启SpringBoot特性有哪几种方式有以下两种方式继承spring-boot-starter-parent项目parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.4/version/parent导入spring-boot-dependencies项目依赖dependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.2.1.RELEASE/versiontypepom/typescopeimport/scope/dependencydependencies/dependencyManagement3.SpringBoot如何解决跨域问题方法一:使用CrossOrigin注解在Spring Boot 中给我们提供了一个注解 CrossOrigin 来实现跨域这个注解可以实现方法级别的细粒度的跨域控制。我们可以在类或者方法添加该注解如果在类上添加该注解该类下的所有接口都可以通过跨域访问如果在方法上添加注解那么仅仅只限于加注解的方法可以访问。RestControllerCrossOriginpublicclassCategoryController{AutowiredprivateCategoryMappercategoryMapper;RequestMapping(/category)publicListCategoryfindAll(){returncategoryMapper.selectAll();}}方法二:实现WebMvcConfigurer这种解决方案并非 Spring Boot 特有的在传统的 SSM 框架中就可以通过 CORS 来解决跨域问题只不过之前我们是在 XML 文件中配置 CORS 现在可以通过实现WebMvcConfigurer接口然后重写addCorsMappings方法解决跨域问题。ConfigurationpublicclassMyWebConfigurerimplementsWebMvcConfigurer{OverridepublicvoidaddCorsMappings(CorsRegistryregistry){registry.addMapping(/**).allowedMethods(*)//支持所有请求方法跨域.allowedHeaders(*)//支持所有请求头跨域.allowCredentials(true)//跨域请求可以包含cookie.allowedOriginPatterns(*)//允许任何域名使用,低版本Springboot用allowedOrigins()方法.maxAge(3600*24);}}addMapping配置可以被跨域的路径可以任意配置可以具体到直接请求路径。allowedOriginPatterns允许所有的请求域名访问我们的跨域资源可以固定单条或者多条内容如“http://www.baidu.com”只有百度可以访问我们的跨域资源。低版本springboot需要使用allowedOriginsallowCredentials 跨域请求是否允许包含Cookie。allowedMethods允许输入参数的请求方法访问该跨域资源服务器如POST、GET、PUT、OPTIONS、DELETE等。allowedHeaders允许所有的请求header访问可以自定义设置任意请求头信息如“X-YAUTH-TOKEN”。maxAge配置预检请求的有效时间以秒为单位。默认设置为1800秒30分钟。方法三:使用CorsFilter过滤器ConfigurationpublicclassCorsConfig{BeanpublicCorsFiltercorsFilter(){CorsConfigurationcorsConfigurationnewCorsConfiguration();corsConfiguration.addAllowedHeader(*);corsConfiguration.addAllowedMethod(*);corsConfiguration.addAllowedOriginPattern(*);corsConfiguration.setAllowCredentials(true);UrlBasedCorsConfigurationSourcesourcenewUrlBasedCorsConfigurationSource();source.registerCorsConfiguration(/**,corsConfiguration);returnnewCorsFilter(source);}}4.SpringBoot如何整合Mybatis导入Mybatis和数据库驱动的依赖dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.0/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencyapplication.properties配置数据源和mapper.xml文件的映射路径spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driverspring.datasource.urljdbc:mysql://8.140.19.47:3306/gulimall_pms?useSSLfalseserverTimezoneGMT%2B8 spring.datasource.usernameroot spring.datasource.passwordroot mybatis.mapper-locationsclasspath:mapper/*.xml主启动类添加MapperScan注解添加mapper接口的扫描路径SpringBootApplicationMapperScan(com.springboot.springbootdemo.dao)publicclassSpringbootdemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringbootdemoApplication.class,args);}}5.SpringBoot怎么实现多环境配置profile是用来完成不同环境下配置动态切换功能的多profile文件方式提供多个配置文件每个代表一种环境。application-dev.properties/yml 开发环境application-test.properties/yml 测试环境application-pro.properties/yml 生产环境profile激活方式配置文件spring.profiles.activedev6.SpringBoot的核心注解有哪些启动类上面的注解SpringBootApplication是SpringBoot的核心注解主要包含3个注解(1)SpringBootConfiguration(里面还包含一个Configuration),表示当前类为SpringBoot配置类(2)EnableAutoConfiguration:开启自动配置的功能可以单独关闭某些资源的自动配置(3)ComponentScan:扫描组件的功能默认是扫描主启动类所在包以及子包的所有组件。7.SpringBoot中静态首页或者静态资源默认位置可以放在哪里classpath: resources/index.htmlclasspath: static/index.htmlclasspath: public/index.htmlclasspath: META-INF/resources/index.html优先级 META-INF/resources/resources/static/public/8.SpringBoot的核心配置文件有哪几个他们的区别是什么SpringBoot的核心配置文件是application和bootstrap配置文件。application配置文件这个容易理解主要用于Spring Boot项目的自动化配置。bootstrap里面的属性会优先加载它们默认也不能被本地相同配置覆盖。bootstrap配置文件有以下几个应用场景使用Spring Cloud Config配置中心时这时需要在bootstrap配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息一些固定的不能被覆盖的属性一些加密/解密的场景9.SpringBoot的优点有哪些SpringBoot 主要有这几个优点第一约定大于配置自动装配根据 classpath 下的 jar 自动装配 Bean省去 Spring 大量 XML 配置。第二提供 Starter 启动器做依赖版本管理减少版本冲突引入组件更加方便。第三内置 web 容器可以直接打成可执行 jarjava‑jar 直接运行部署简单适配容器化 CI/CD。第四支持多环境 profile 配置配置外部化方便区分开发、测试、生产环境。第五提供 Actuator 监控组件方便线上运维监控同时完全兼容整个 Spring 生态能够很好对接 SpringCloud、Dubbo 微服务。