Spring Boot面试问题

Spring Boot面试前20名问答列表

  1. What are the benefits of using Spring boot? How it is different from the Spring framework?Spring Boot makes it very easy to create a standalone application by providing auto configurations. It provides production-ready features like Actuator.

特征

自动配置 创建麻烦最少的独立应用程序 提供嵌入式tomcat,Jetty服务器 无需部署战争文件 无需维护应用程序/ Web服务器 提供可用于生产的功能,例如指标,JMX,运行状况检查和审核。 提供各种入门工具以简化依赖关系管理 与第三方库轻松集成 外部化配置 配置文件特定的属性文件 Spring Boot是Spring框架的附加组件。 Spring Boot通过自动配置来简化应用程序的创建。

  1. 很少使用的Spring Boot注释是什么? @SpringBootApplication : mark the main class of a Spring Boot application @SpringBootTest : used for Integration testing @Configuration : used to provide bean configrations @Conditional @ConditionalOnBean @Bean @DataJpaTest @WebMVCTest @Value @ControllerAdvice在Spring Boot应用程序启动之前如何自定义环境或ApplicationContext? A SpringApplication class has ApplicationListeners and ApplicationContextInitializers that are used to apply customizations to the context or environment. Spring Boot loads a number of such customizations. It internally uses the META-INF/spring.factories.

有许多方法可以注册其他自定义项:

I.以编程方式通过在运行SpringApplication之前调用addListeners和addInitializers方法。 二。 通过在application.properties中设置context.initializer.classes或context.listener.classes进行声明式设置。 三, 通过添加META-INF / spring.factories并添加供应用程序用作库的jar文件进行声明。

  1. How to create custom Spring Boot Auto Configuration?Auto-Configuration is the way Spring Boot configure the application automatically with minimal configurations. Auto-configuration classes can be bundled in external jars and picked-up by Spring Boot application. Auto-configuration can be associated to a “starter” dependencies that provide the auto-configuration code as well as the typical libraries that you would use with it.

在后台,自动配置是通过标准@Configuration类实现的。 其他@Conditional批注用于约束何时应应用自动配置。 通常,自动配置类使用@ConditionalOnClass和@ConditionalOnMissingBean批注。 这样可以确保仅当找到相关的类并且没有声明自己的@Configuration时,才应用自动配置。

  1. How to disable the specific Auto-Configuration bean in Spring Boot?To exclude/disable the auto-configuration bean from being loaded to an application, we can add the @EnableAutoConfiguration annotation with exclude or excludeName attribute to a configuration class.

禁用特定自动配置的另一个选项是通过从应用程序属性文件中设置spring.autoconfigure.exclude属性。

  1. What is the Spring Boot Actuator? How to create custom Actuator?Actuators enable production-ready features to a Spring Boot application, without having to actually implement these things yourself. Actuator endpoints are secured by default.Here are some of the common endpoints of Spring Boot

/ health-显示应用程序运行状况信息。 监视软件通常使用它在生产系统出现故障时向某人发出警报。 / info-显示任意应用程序信息 / metrics-显示当前应用程序的“指标”信息; / trace —显示跟踪信息 / mappings-显示所有@RequestMapping路径的整理列表。 默认情况下,除关机外的所有端点均处于启用状态。 为了启用/禁用执行器端点,我们可以使用其management.endpoint..enabled属性。 例如

management.endpoint.shutdown.enabled = true Spring Boot使用Jersey,Spring MVC或Spring WebFlux通过HTTP自动使用@ Endpoint,@ WebEndpoint或@WebEndpointExtension公开终结点。 我们可以添加带有@Endpoint注释的@Bean,带有@ ReadOperation,@ WriteOperation或@DeleteOperation注释的任何方法都将通过JMX以及Web应用程序通过HTTP自动公开。

  1. How to configure the Logger in Spring Boot? How to change default logging level?Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2, and Logback. By default, if we use the Starters, Logback is used for logging.

通过使用--debug标志启动应用程序来启用调试模式。 $ java -jar myapp.jar —调试 来自application.properties 调试=真 默认情况下,Spring Boot只记录到控制台,不写日志文件。 如果要写入日志文件,可以在application.properties中设置alogging.file或logging.path属性。

更改日志级别 logging.level.root =警告 logging.level.com.myapp =调试 logging.level.org.thirdpary.app =错误

  1. What are different approach available to load multiple external configuration/properties files?There are various ways we can provide load configuration files. Spring Boot properties are loaded in the following order. we can use one or more approach based on our requirement.

命令行参数:$ java -jar myapp.jar -Dspring.config.location = classpath:job1.properties,classpath:job2.properties com.app.MainClass

  1. 设置Java系统属性(System.getProperties())。设置操作系统环境变量。打包的jar之外的应用程序属性(application.properties,包括YAML和配置文件变体)。打包在jar中的应用程序属性(包括YAML和配置文件变体的application.properties)。@Configuration类上的@PropertySource批注。默认属性(使用SpringApplication.setDefaultProperties指定)。Explain how Spring Boot Profiles works? How to configure multiple Profiles?Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments. For example, we can use a separate profile for testing. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded.

@Configuration
@profile (“production”)
public class MySecurityConfiguration{
}
We can use a spring.profiles.active Environment property to specify which profiles are active.

spring.profiles.active =测试 在命令行中,我们传递以下参数

$ java -jar myapp.jar — spring.profiles.active = dev

  1. How to use the custom spring boot parent POM?Spring Boot provides the parent POM for easier creation of Spring Boot applications. The parent pom.xml takes care of dependency and plugin management.

为了创建自定义的父POM,我们可以使用spring-boot-starter-parent作为依赖项,并自定义spring-boot-maven-plugin。

org.springframework.boot 弹簧启动依赖 $ {springframework.boot.version} 绒球 进口

  1. How to change the default context path in Spring Boot?we can simply change the default context path by defining it in application.propertiesserver.servlet.context-path=/rest/

我们还可以从命令行设置系统属性,操作系统环境变量。

  1. How to depoy Spring Boot application as a WAR?spring-boot-maven-plugin added in ourpom.xmlautomatically tries to rewrite archives to make them executable by using the spring-boot:repackage goal.we can change the default packaging from pom.xml by

战争 并且,我们初始化Tomcat上下文所需的Servlet上下文。 我们可以实现SpringBootServletInitializer接口。

@SpringBootApplication 公共类MainClass扩展了SpringBootServletInitializer { }

  1. How to change default server port in Spring Boot?By default, the embedded server start on port 8080. we can change it by defining it in the application.properties

server.port = 8084 我们还可以通过实现WebServerFactoryCustomizer接口并从命令行传递它来动态更改它。

  1. How to customize the default Spring Security in Spring Boot?Spring Boot auto-configure the security by simply adding security starter dependency. The SecurityAutoConfiguration class contains various default auto configurations. By default, the Spring Boot will enable Basic Authentication security.

我们可以通过扩展WebSecurityConfigurerAdapter类并覆盖configure方法来自定义Spring安全性。

@组态 公共类SecurityConfig扩展了WebSecurityConfigurerAdapter { @Override 无效的configure(HttpSecurity http){ } }

  1. How to disable the Spring Security in Spring Boot application?we can disable security auto-configuration by excluding the SecurityAutoConfiguration class from Spring Boot application.

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class}) 公共类SpringSecurityConfig { } 我们还可以通过允许所有请求来绕过spring安全性

@组态 公共类SpringSecurityConfig { @豆 公共SecurityWebFilterChain filterChain(ServerHttpSecurity http){ http.authorizeExchange()。anyExchange()。permitAll(); 返回http.build(); } }

  1. How to write the integration test in Spring Boot?Spring Boot provides a number of utilities and annotations to help when testing your application. Spring Boot provides a @SpringBootTest annotation for integration testing. The @SpringBootTest annotation works by creating the ApplicationContext used in our tests. @SpringBootTest provide various customization also.

@RunWith(SpringRunner.class) @SpringBootTest 公共类IntegrationTest { }

  1. How to test only the database layer(JPA) in Spring Boot?We can use the @DataJpaTest annotation to test the database layer. By default, it configures an in-memory embedded database e.g. H2 database. It scans for @Entity beans, and configures Spring Data JPA repositories. Regular @Component beans are not loaded into the ApplicationContext. Also By default, data JPA tests are transactional and roll back at the end of each test.

@RunWith(SpringRunner.class) @DataJpaTest 公共类DatabaseTest {

} 我们可以注入一个TestEntityManager bean,它提供了专门为测试设计的标准JPA EntityManager的替代方案。 同样,Spring Boot为MongoDB测试提供@DataMongoTest批注,为Redis应用程序测试提供@DataRedisTest批注。

  1. How to configure two databases and two EntityManager in Spring Boot?In order to use multiple databases in our application, we have to create our own DataSource bean.

Database 1

first.url = [url] ...

Database

second.url = [url] ....

@Bean(“ db1”) @主 @ConfigurationProperties(prefix =“ first”) 公共数据源primaryDataSource(){ 返回DataSourceBuilder.create()。build(); }

@Bean(“ db2”) @ConfigurationProperties(prefix =“ second”) public DataSource secondaryDataSource(){ 返回DataSourceBuilder.create()。build(); } 同样,我们可以通过自定义bean创建多个EntityManager。 为了方便起见,Spring Boot提供了EntityManagerBuilder。

@Bean(“ em1”) 公共LocalContainerEntityManagerFactoryBean em1( EntityManagerFactoryBuilder构建器){ 退货建设者 .dataSource(customDataSource1()) .packages(DataSource.class) 。建立(); }

@Bean(“ em2”) 公共LocalContainerEntityManagerFactoryBean em2( EntityManagerFactoryBuilder构建器){ 返回...; }

  1. How to customize the support for multiple content-negotiation for returning XML or JSON?We can overwrite configureContentNegotiation method in our WebMvcConfigurerAdapter configuration class.

@Override 公共无效configureContentNegotiation(ContentNegotiationConfigurer配置器){ .... defaultContentType(MediaType.APPLICATION_JSON)。 mediaType(“ xml”,MediaType.APPLICATION_XML)。 mediaType(“ json”,MediaType.APPLICATION_JSON); } }

And

产生= {“应用程序/ json”,“应用程序/ xml”}

  1. How to register Servlet, Filter and Listener in Spring Boot?When using an embedded server, we can register servlets, filters, and the listeners by using Spring beans or by scanning for Servlet components.

我们可以使用FilterRegistrationBean bean注册一个过滤器,使用ServletRegistrationBean注册Servlet和使用ServletListenerRegistrationBean注册Listener。

@Bean
public FilterRegistrationBean custFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MyFilter());
return registration;
}
https://practiceoverflow.com/take-test/sieWV1ypGBd7iBKoT3Nu/Spring-Boot-IQ-Test-2019:-contains-top-20-questions-to-practice-your-Spring-boot-knowledge

from: https://dev.to//anilkkurmi/spring-boot-interview-questions-5ao8

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值