5000+收藏的Spring Boot面试核心,助你乘风破浪

面试官问的好多技术我都用到了,但面试官的问题我们时却不能给出准确的答案。

我们平常在项目中主要关注使用,程序run起来就ok了,很少去了解原理、架构、和性能调优。这样在面试问题中总有一种无法直击问题本质的无力感,很难充分表现自己,最终影响面试结果。

其实,这是很多Java研发人员遇到的普遍问题,不清楚原理,虽然用起来没问题,但讲起来有困难!

为了避免此类问题,本文针对面试中涉及到的Spring Boot核心知识点进行了总结,帮助大家查漏补缺,在技术面试中能够一路通关!

本文目录


1. Spring Boot的使用

2. Spring Boot Application Starters

3. Spring Boot的常用组件及其使用

  • Spring Boot使用MySQL
  • Spring Boot使用Redis
  • Spring Boot使用MongoDB
  • Spring Boot使用Neo4j
  • Spring Boot使用Solr
  • Spring Boot使用ElasticSearch
  • Spring Boot使用Cassandra
  • Spring Boot使用RabbitMQ
  • Spring Boot使用Kafka

▊ Spring Boot的特点如下

(1)快速创建独立的Spring应用程序。

(2)嵌入Tomcat和Undertow等Web容器,实现快速部署。

(3)自动配置JAR包依赖和版本控制,简化Maven配置。

(4)自动装配Spring实例,不需要XML配置。

(5)提供诸如性能指标、健康检查、外部配置等线上监控和配置功能。

01

Spring Boot的使用

Spring Boot把传统的Spring项目从繁杂的XML配置中解放出来,应用只需要用注解自动扫描即可,同时Spring Boot为应用提供了统一的JAR管理和维护,不需要应用程序管理复杂的JAR依赖和处理多版本冲突问题,只需要在pom.xml文件中加入对应模块的Starter即可。对内部的JAR依赖的管理,Spring Boot会自动维护。具体使用过程如下。

(1)Spring Boot的引入。

Spring Boot项目定义简单,使用方便,第一步需要在pom.xml文件中引入org.springframework.boot及相关依赖。pom.xml文件如下。

 1<?xml version="1.0" encoding="UTF-8"?>
 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4    <modelVersion>4.0.0</modelVersion>
 5  
 6    <parent>
 7        <groupId>org.springframework.boot</groupId>
 8        <artifactId>spring-boot-starter-parent</artifactId>
 9        <version>2.1.4.RELEASE</version>
10        <relativePath/> 
11    </parent>
12      
13    <groupId>com.alex</groupId>
14    <artifactId>springboot</artifactId>
15    <version>0.0.1-SNAPSHOT</version>
16    <name>springboot</name>
17    <description>Demo project for Spring Boot</description>
18    
19    <properties>
20        <java.version>1.8</java.version>
21    </properties>
22    <dependencies>
23      
24        <dependency>
25            <groupId>org.springframework.boot</groupId>
26            <artifactId>spring-boot-starter-web</artifactId>
27        </dependency>
28   
29        <dependency>
30            <groupId>org.springframework.boot</groupId>
31            <artifactId>spring-boot-starter-test</artifactId>
32            <scope>test</scope>
33        </dependency>
34    </dependencies>
35    <build>
36        <plugins>
37   
38            <plugin>
39                <groupId>org.springframework.boot</groupId>
40                <artifactId>spring-boot-maven-plugin</artifactId>
41            </plugin>
42        </plugins>
43    </build>
44</project>

(2)配置文件设置。

Spring Boot的配置分为application.properties和application.yml两种,两种配置有语法差别,但其实现的功能相同。下面的配置文件通过server.port=9090设置了服务端口为9090,如果不设置,则默认端口为Tomcat的8080,通过server.name=hello设置了服务名称为hello。

 1server.port=9090 #服务端口号
 2server.name=hello #服务名称
 3server.tomcat.uri-encoding=UTF-8 #以Tomcat为Web容器时的字符编码为UTF-8
 4#spring.data.mongodb.uri=mongodb://localhost:27017/mydb #MongoDB连接地址定义
 5#spring.http.encoding.charset=UTF-8 #HTTP请求的字符编码为UTF-8
 6#spring.http.multipart.max-file-size=10MB #设置文件上传时单个文件的大小限制
 7#spring.http.multipart.max-request-size=100MB #设置文件上传时总文件的大小限制
 8#spring.mvc.static-path-pattern=/** #设置静态资源的请求路径
 9#spring.resources.static-locations=classpath:/static/,classpath:/public/ 
10#设置静态资源的路径,多个用逗号隔开
11# MySQL数据库配置
12#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect #设置数据库方言为MySQL
13#hibernate.show_sql=true #设置是否显示SQL语句
14#hibernate.hbm2dll.auto=update #设置使用Hibernate的自动建表
15#entitymanager.packagesToScan=com.zslin #设置自动扫描的包路径
16#spring.datasource.url=jdbc:mysql://localhost:3306/customer?\
17#useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true 
18#设置MySQL数据库连接
19#spring.datasource.username=root #设置数据库用户名
20#spring.datasource.password=123 #设置数据库root用户对应的密码
21#spring.datasource.driver-class-name=com.mysql.jdbc.Driver #设置数据库驱动名称

(3)定义启动类。

启动类是Spring Boot项目的入口,应用程序通过在类上设置一个@SpringBootApplication注解,声明该类是一个Spring Boot启动类,Spring Boot会扫描启动类所在的包及其子包中的所有类的注解,并将其加载到Spring Boot的容器中进行管理。只需要在main()函数中执行SpringApplication.run(SpringbootApplication.class, args),便完成了启动类的定义。代码如下。

1@SpringBootApplication
2public class SpringbootApplication {
3    public static void main(String[] args) {
4        SpringApplication.run(SpringbootApplication.class, args);
5    }
6}

在定义启动类后,单击右键执行run,便可启动该Spring Boot项目了。

(4)定义控制器

在SpringbootApplication的根目录下定义一个控制器,用于Web接口的访问。控制器的定义方式和在Spring项目中控制器的常规定义方式一样,具体代码如下。

1@RestController
2public class BaseController {
3    @RequestMapping("/hello")
4    public  String home() {
5        return "Hello World!";
6    }
7}

(5)项目启动和访问

在SpringbootApplication上单击右键执行run,便可启动该Spring Boot服务;在浏览器地址栏中输入127.0.0.1:9090/hello,便能访问定义好的REST服务。运行结果如下。

 1 .   ____          _            __ _ _
 2 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
 3( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 4 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 5  '  |____| .__|_| |_|_| |_\__, | / / / /
 6 =========|_|==============|___/=/_/_/_/
 7 :: Spring Boot ::        (v2.1.4.RELEASE)
 8 ......
 9 : Tomcat initialized with port(s): 9090 (http)
10 : Starting service [Tomcat]
11 : Starting Servlet engine: [Apache Tomcat/9.0.17]
12 : Tomcat started on port(s): 9090 (http) with context path ''
13 : Started SpringbootApplication in 1.671 seconds (JVM running for 2.154)
14 ......
15

02

Spring Boot Application Starters

Starters是一组资源依赖描述,用于为不同的Spring Boot应用提供一站式服务,而不必像传统的Spring项目那样,需要开发人员处理服务和服务之间的复杂依赖关系。例如,如果要使用Spring的JPA功能进行数据库访问,只需要应用程序在项目中加入spring-boot-starter-data-jpa依赖即可,具体的依赖细节由Starters统一处理,不需要应用程序分别处理各个JAR包的依赖关系。常用的Starters下如表所示。

03

Spring Boot的常用组件及其使用

Spring Boot的核心特点是通过Starter能快速将各个组件集成到应用中,并提供良好的操作接口。下面将简单介绍常用组件的使用。

1. Spring Boot使用MySQL 

Spring Boot基于Starter能够快速将不同的服务组件集成到应用程序中。Spring Boot服务组件的集成过程分为引入Starter、设置application.properties和使用服务组件(组件会根据配置文件自动装配)3步。MySQL的具体使用如下。

(1)引入Starter。

1      <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId>spring-boot-starter-jdbc</artifactId>
4        </dependency>

(2)设置application.properties。

1spring.datasource.url=jdbc:mysql://localhost/test#数据库地址
2spring.datasource.username=dbuser#数据库用户名
3spring.datasource.password=dbpass#数据库密码
4spring.datasource.driver-class-name=com.mysql.jdbc.Driver#数据库驱动

(3)使用服务组件。

1@Component
2public class MyBean {
3    private final JdbcTemplate jdbcTemplate;
4    @Autowired
5    public MyBean(JdbcTemplate jdbcTemplate) {
6        this.jdbcTemplate = jdbcTemplate;
7     }
8}

2. Spring Boot使用Redis 

(1)引入Starter。

1      <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId>spring-boot-starter-data-redis</artifactId>
4        </dependency>

(2)设置application.properties。

 1#Redis数据库名称(默认为0)
 2spring.redis.database=0
 3#Redis数据库地址
 4spring.redis.host=172.31.19.222
 5#Redis数据库端口
 6spring.redis.port=6379
 7#Redis数据库密码(默认为空)
 8spring.redis.password=
 9#Redis连接池的最大连接数(使用负值表示没有限制)
10spring.redis.pool.max-active=8
11#Redis连接池的最大阻塞等待时间(使用负值表示没有限制)
12spring.redis.pool.max-wait=-1
13#Redis连接池中的最大空闲连接
14spring.redis.pool.max-idle=8
15#Redis连接池中的最小空闲连接
16spring.redis.pool.min-idle=0

(3)使用服务组件。

1@Component
2public class MyBean {
3    private StringRedisTemplate template;
4    @Autowired
5    public MyBean(StringRedisTemplate template) {
6        this.template = template;
7    }
8}

3. Spring Boot使用MongoDB 

(1)引入Starter。

1<dependency>
2      <groupId>org.springframework.boot</groupId>
3      <artifactId> spring-boot-starter-data-mongodb</artifactId>
4</dependency>

(2)设置application.properties。

1spring.data.mongodb.uri=mongodb://user:[email protected]:12345,mongo
22.example.com:23456/test
3#数据库的连接地址

(3)使用服务组件。

1@Component
2public class MyBean {
3    private MongoTemplate template;
4    @Autowired
5    public MyBean(MongoTemplate template) {
6        this.template = template;
7    }
8}

4. Spring Boot使用Neo4j 

(1)引入Starter。

1      <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId> spring-boot-starter-data-neo4j</artifactId>
4        </dependency>

(2)设置application.properties。

1spring.data.neo4j.uri=bolt://my-server:7687  #Neo4j图数据库地址
2spring.data.neo4j.username=neo4j               #Neo4j图数据库用户名
3spring.data.neo4j.password=secret              #Neo4j图数据库密码

(3)使用服务组件。

1@Component
2public class MyBean {
3    private final Session session;
4    @Autowired
5    public MyBean(Session session) {
6        this.session = session;
7    }
8}

5. Spring Boot使用Solr 

(1)引入Starter。

1      <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId> spring-boot-starter-data-solr</artifactId>
4        </dependency>

(2)设置application.properties。

1#Solr数据库地址 
2spring.data.solr.host: http://127.0.0.1:8080/solr/ciri_core

(3)使用服务组件。

1@Component
2public class MyBean {
3    private SolrClient solr;
4    @Autowired
5    public MyBean(SolrClient solr) {
6        this.solr = solr;
7    }
8}

6. Spring Boot使用ElasticSearch 

(1)引入Starter。

1      <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId> spring-boot-starter-data-elasticsearch</artifactId>
4        </dependency>

(2)设置application.properties。

1#ElasticSearch数据库地址
2spring.data.elasticsearch.cluster-nodes=localhost:9300

(3)使用服务组件。

1@Component
2public class MyBean {
3    private final ElasticsearchTemplate template;
4    public MyBean(ElasticsearchTemplate template) {
5        this.template = template;
6    }
7}

7. Spring Boot使用Cassandra 

(1)引入Starter。

   

1     <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId> spring-boot-starter-data-cassandra</artifactId>
4        </dependency>

(2)设置application.properties。

1#Cassandra的命名空间
2spring.data.cassandra.keyspace-name=mykeyspace 
3#Cassandra数据库地址
4spring.data.cassandra.contact-points=cassandrahost1,cassandrahost2

(3)使用服务组件。

1@Component
2public class MyBean {
3    private CassandraTemplate template;
4    @Autowired
5    public MyBean(CassandraTemplate template) {
6        this.template = template;
7    }
8}

8. Spring Boot使用RabbitMQ 

(1)引入Starter。

1      <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId> spring-boot-starter-rabbitmq </artifactId>
4        </dependency>

(2)设置application.properties。

1spring.rabbitmq.host=localhost   #RabbitMQ服务地址
2spring.rabbitmq.port=5672         #RabbitMQ端口号
3spring.rabbitmq.username=admin   #RabbitMQ用户名
4spring.rabbitmq.password=secret  #RabbitMQ密码

(3)定义服务组件。

 1@Component
 2public class MyBean {
 3    private final AmqpAdmin amqpAdmin;
 4    private final AmqpTemplate amqpTemplate;
 5    @Autowired
 6    public MyBean(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
 7        this.amqpAdmin = amqpAdmin;
 8        this.amqpTemplate = amqpTemplate;
 9    }
10}

(4)定义队列。

1@Configuration
2public class QueueConf {
3        //1:定义Queue实例对象,队列名称为someQueue
4        @Bean(name="message")
5        public Queue queueMessage() {
6            return new Queue("someQueue");
7        }
8}

(5)发送消息。

1@Component
2public class MyBeanSender {
3    @Autowired
4    private AmqpTemplate template;
5    public void send() {
6    //向队列someQueue发送一条消息hello,rabbit
7    template.convertAndSend("someQueue","hello,rabbit");
8    }
9}

(6)接收消息。

1@Component
2public class MyBean {
3//监听和接收队列someQueue上的消息
4    @RabbitListener(queues = "someQueue")
5    public void processMessage(String content) {
6    }
7}

9. Spring Boot使用Kafka 

(1)引入Starter。

1      <dependency>
2            <groupId>org.springframework.boot</groupId>
3            <artifactId> spring-boot-starter-kafka </artifactId>
4        </dependency>

(2)设置application.properties。

1#Kafka服务地址
2spring.kafka.bootstrap-servers=localhost:9092
3#Kafka消费组
4spring.kafka.consumer.group-id=myGroup

(3)发送消息。

 1@Component
 2public class MyBean {
 3    private final KafkaTemplate kafkaTemplate;
 4    @Autowired
 5    public MyBean(KafkaTemplate kafkaTemplate) {
 6        this.kafkaTemplate = kafkaTemplate;
 7    }
 8   public Response sendKafka() {
 9           //向Kafka的someTopic发送一条消息
10            kafkaTemplate.send("someTopic", "key", message);
11    }
12} 

(4)接收消息。

1@Component
2public class MyBean {
3//监听并接收someTopic上的消息
4    @KafkaListener(topics = "someTopic")
5    public void processMessage(String content) {
6        System.out.println("message:"+ content);
7    }
8}

而这只是单纯的一个springboot,我上面总结的这些知识,基本都是在这一本书,包括和MongoDB、Redis、ES等的结合是如何使用的

需要这份资料的朋友,可以关注+转发,然后私信“资料”即可查看获取方式

当然了,只有springBoot怎么可以,今天也算是一点福利吧,送给大家,Java面试核心的知识点

在Java面试的时候祝大家一臂之力,共283页

需要这份资料的,老规矩,关注+转发后,私信“资料”即可查看获取方式

Java

内容涵盖:

JVM、JAVA集合、JAVA多线程并发、JAVA基础、Spring原理、微服务、Netty与RPC、网络、日志、 Zookeeper、 Kafka、RabbitMQ、Hbase、 MongoDB、Cassandra、设计模式、负载均衡、数据库、一致性算法、 JAVA算法、数据结构、加密算法、分布式缓存

大数据、云计算等

内容涵盖:

分布式缓存、Hadoop、Spark、Storm、YARN、机器学习、云计算

关注公众号:Java架构师联盟,每日更新技术好文,关注后,后台回复1000,惊喜相待哦

猜你喜欢

转载自blog.csdn.net/weixin_42864905/article/details/106866963
今日推荐