Spring Boot面试题
基础概念
Q1: Spring Boot的核心特性有哪些?
public class SpringBootBasicDemo { @SpringBootApplication public class DemoApplication { public static void main ( String [ ] args) { SpringApplication . run ( DemoApplication . class , args) ; } @Configuration public class CustomConfig { @Bean @ConditionalOnMissingBean public DataSource dataSource ( ) { return DataSourceBuilder . create ( ) . url ( "jdbc:mysql://localhost:3306/demo" ) . username ( "root" ) . password ( "password" ) . build ( ) ; } } } @Configuration @ConfigurationProperties ( prefix = "app" ) public class AppProperties { private String name; private String description; private Map < String , String > additionalHeaders = new HashMap < > ( ) ; }
}
Q2: Spring Boot的启动流程是怎样的?
public class SpringBootStartupDemo { public class StartupExample { public void startupDemo ( ) { @SpringBootApplication public class Application { public static void main ( String [ ] args) { SpringApplication app = new SpringApplication ( Application . class ) ; app. setBannerMode ( Banner. Mode . OFF ) ; app. setWebApplicationType ( WebApplicationType . SERVLET ) ; app. addListeners ( new ApplicationListener < ApplicationEvent > ( ) { @Override public void onApplicationEvent ( ApplicationEvent event) { if ( event instanceof ApplicationStartedEvent ) { } } } ) ; app. run ( args) ; } } } } public class CustomStarterExample { public void starterDemo ( ) { @Configuration @ConditionalOnClass ( CustomService . class ) @EnableConfigurationProperties ( CustomProperties . class ) public class CustomAutoConfiguration { @Autowired private CustomProperties properties; @Bean @ConditionalOnMissingBean public CustomService customService ( ) { return new CustomService ( properties) ; } } @ConfigurationProperties ( prefix = "custom" ) public class CustomProperties { private String property; private boolean enabled = true ; } } }
}
高级特性
Q3: Spring Boot的自动配置原理是怎样的?
public class AutoConfigurationDemo { public class ConditionalExample { public void conditionalDemo ( ) { @Configuration @ConditionalOnClass ( DataSource . class ) public class DatabaseConfiguration { @Bean @ConditionalOnMissingBean public DataSource dataSource ( ) { } @Bean @ConditionalOnProperty ( prefix = "spring.jpa" , name = "enabled" , havingValue = "true" , matchIfMissing = true ) public JpaTransactionManager transactionManager ( ) { } } } } public class AutoConfigImplementationExample { public void autoConfigDemo ( ) { @Configuration @EnableAutoConfiguration @ComponentScan public class AppConfig { @Bean public BeanFactoryPostProcessor beanFactoryPostProcessor ( ) { return new BeanFactoryPostProcessor ( ) { @Override public void postProcessBeanFactory ( ConfigurableListableBeanFactory beanFactory) throws BeansException { } } ; } } } }
}
Q4: Spring Boot的监控和运维是怎样的?
public class MonitoringDemo { public class ActuatorExample { public void actuatorDemo ( ) { @Configuration public class ActuatorConfig { @Bean public HealthIndicator customHealthIndicator ( ) { return new HealthIndicator ( ) { @Override public Health health ( ) { return Health . up ( ) . withDetail ( "app" , "Healthy" ) . withDetail ( "error" , "None" ) . build ( ) ; } } ; } @Bean public EndpointWebExtension metricsEndpointWebExtension ( MetricsEndpoint metricsEndpoint) { return new EndpointWebExtension ( ) { @ReadOperation public WebEndpointResponse < Map > getMetrics ( ) { return new WebEndpointResponse < > ( metricsEndpoint. metric ( "jvm.memory.used" , null ) . getMeasurements ( ) . stream ( ) . collect ( Collectors . toMap ( Measurement :: getStatistic , Measurement :: getValue ) ) , 200 ) ; } } ; } } } } public class CustomMetricsExample { public void metricsDemo ( ) { @Component public class CustomMetricsExporter { private final MeterRegistry registry; public CustomMetricsExporter ( MeterRegistry registry) { this . registry = registry; } public void recordCustomMetric ( String name, double value) { registry. gauge ( name, value) ; } public void incrementCounter ( String name) { registry. counter ( name) . increment ( ) ; } public void recordTimer ( String name, long timeInMs) { registry. timer ( name) . record ( Duration . ofMillis ( timeInMs) ) ; } } } }
}
Q5: Spring Boot的性能优化有哪些?
public class PerformanceOptimizationDemo { public class JVMOptimizationExample { public void jvmOptDemo ( ) { public class JVMConfig { public void configureJVM ( ) { System . setProperty ( "spring.jmx.enabled" , "false" ) ; System . setProperty ( "spring.config.location" , "classpath:/application.yml" ) ; System . setProperty ( "server.tomcat.max-threads" , "200" ) ; System . setProperty ( "server.tomcat.min-spare-threads" , "10" ) ; } } } } public class DatabaseOptimizationExample { public void dbOptDemo ( ) { @Configuration public class DatabaseConfig { @Bean public DataSource dataSource ( ) { HikariConfig config = new HikariConfig ( ) ; config. setMaximumPoolSize ( 10 ) ; config. setMinimumIdle ( 5 ) ; config. setIdleTimeout ( 300000 ) ; config. setConnectionTimeout ( 20000 ) ; config. setMaxLifetime ( 1200000 ) ; return new HikariDataSource ( config) ; } @Bean public JdbcTemplate jdbcTemplate ( DataSource dataSource) { JdbcTemplate template = new JdbcTemplate ( dataSource) ; template. setFetchSize ( 100 ) ; template. setMaxRows ( 500 ) ; return template; } } } } public class CacheOptimizationExample { public void cacheOptDemo ( ) { @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager ( ) { CaffeineCacheManager cacheManager = new CaffeineCacheManager ( "users" ) ; cacheManager. setCaffeine ( Caffeine . newBuilder ( ) . expireAfterWrite ( 60 , TimeUnit . MINUTES ) . maximumSize ( 100 ) ) ; return cacheManager; } } @Service public class UserService { @Cacheable ( value = "users" , key = "#id" ) public User getUser ( Long id) { return userRepository. findById ( id) . orElse ( null ) ; } @CacheEvict ( value = "users" , key = "#user.id" ) public void updateUser ( User user) { userRepository. save ( user) ; } } } }
}
面试关键点
理解Spring Boot的核心特性 掌握启动流程和原理 熟悉自动配置机制 了解监控和运维 掌握性能优化方法 理解配置管理 注意安全实践 关注最佳实践