Do you think you spring boot to touch thorough, interview a few questions you put a Shredded

Do you think you spring boot to touch thorough, interview a few questions you put a Shredded


ac9359169a6640088b6e3276afb637e0


Preface:

With Spring Boot more widely used, has become a Spring Boot knowledge Java programmer interview, SpringBoot enterprise-class development of the overall integrated solution, enterprises now use in project development is very common, it can be said now is the Java programmer to the time necessary to learn SpringBoot, many students on Spring Boot understanding is not so profound, often several comic will be dry run to get on the ground!

For example, the following paragraph Spring Boot Q & A:

Q: What do you think the biggest advantage of Spring Boot is it?

A: Spring Boot's biggest advantage is the "convention over configuration." "Convention over configuration" is a software design paradigm, developers in the agreed manner to program, reducing the number of software developers need to make decisions, to obtain the benefits of simple, but without losing flexibility.

Q: Spring Boot of "convention over configuration" of specific products is reflected in where.

A: Spring Boot Starter, Spring Boot Jpa are a reflection of "convention over configuration". Are designed by "convention over configuration" design ideas, Spring Boot Starter will be initialized with the information resources in the agreed during startup; Spring Boot Jpa Sql generated automatically by agreed manner, avoiding a large number of invalid code write.

Q: What Works Spring Boot Starter is?

A: Spring Boot at startup will do these things:

Spring Boot at startup to rely on the Starter package to find resources / META-INF / spring.factories file, then according to package Jar files to configure the scanning project depends Jar package.

The configuration spring.factories class loading AutoConfigure

The conditions @Conditional annotations, automatic configuration and Bean injection Spring Context

To sum up, in fact, Spring Boot at startup, as agreed to read the configuration information Spring Boot Starter, and then the resource is initialized according to configuration information, and injected into the Spring container. After this Spring Boot boot is completed, it is ready all the resources, the course can be injected directly into the corresponding resources Bean.

It's just simple three serial questions and answers, do not know how many students can complete answer them.

In fact, Spring Boot has a lot of technical points that can be tapped, today to finish ten high-frequency Spring Boot interview questions, hope to help everyone in the latter part of the interview. Of course, the interview will have to face questions spring boot is not enough too, so the end of the article there are benefits Oh

A, Spring Boot autoconfiguration is how to achieve?

Start annotation Spring Boot project is: @SpringBootApplication, in fact, it is comprised of three notes consisting of:

@Configuration

@ComponentScan

@EnableAutoConfiguration

Wherein @EnableAutoConfiguration achieve automatic configuration of the inlet, through the annotations and notes introduced @Import AutoConfigurationImportSelector, loading configuration information META-INF / spring.factories in the class. In EnableAutoConfiguration then screened for the key data is loaded into the vessel IOC, automatic configuration feature!

Second, what is the embedded server? Why do we want to use the embedded server?

Consider deploying an application on a virtual machine what your needs.

Step 1: Install Java

Step 2: Install Web or application server (Tomat / Wbesphere / Weblogic, etc.)

Step Three: Deploying the application war package

If we want to simplify these steps, how? Let's think about how to make a server part of the application?

You only need one of the Java virtual machine installed, you can deploy applications directly on top, is not it cool?

The idea is the origin of the embedded server.

When we create an application that can be deployed, we will put a server (eg, tomcat) embedded server deployment.

For example, for a Spring Boot application, you can build an application that contains jar Embedded Tomcat. You'll be able to run as a web application to run as normal Java applications.

Our server is embedded executable binary file that contains the server unit (for example, tomcat.jar).

Third, micro-services at the same time calling multiple interfaces, how transactional ah?

Support distributed transactions, you can use Spring Boot integrated Aatomikos to solve, but I generally do not recommend such use, because the use of a distributed transaction will increase the response time of the request, TPS affect the system. In general practice, distributed transactions will be processed using the compensation mechanism messages.

Fourth, what is the relationship between them and shiro oauth there cas that? Ask permission under your company is how to design, there is a difference between these concepts.

cas and oauth component is a single sign-solving, shiro authority primarily responsible for security, so inconsistent function points. But often require a single sign-on and access control together to use, so there cas + shiro or a combination of such oauth + shiro.

token is usually the client login server generates a token, each time you access the server will be verified and generally can be saved to memory, you can also put other media; redis Session sharing can be done, if there are several front-end web server Taiwan load, but need to maintain the status of the user logged in, use this scene more common.

Our company uses oauth + shiro management background in such a way to do permission, oauth responsible for more than background unified login authentication, shiro responsible for logged-in user assign different access rights.

Fifth, the communication between the various services, for Restful and Rpc both ways on how to choose?

在传统的SOA治理中,使用rpc的居多;Spring Cloud默认使用restful进行服务之间的通讯。rpc通讯效率会比restful要高一些,但是对于大多数公司来讲,这点效率影响甚微。我建议使用restful这种方式,易于在不同语言实现的服务之间通讯。

六、怎么设计无状态服务?

对于无状态服务,首先说一下什么是状态:如果一个数据需要被多个服务共享,才能完成一笔交易,那么这个数据被称为状态。进而依赖这个“状态”数据的服务被称为有状态服务,反之称为无状态服务。

那么这个无状态服务原则并不是说在微服务架构里就不允许存在状态,表达的真实意思是要把有状态的业务服务改变为无状态的计算类服务,那么状态数据也就相应的迁移到对应的“有状态数据服务”中。

场景说明:例如我们以前在本地内存中建立的数据缓存、Session 缓存,到现在的微服务架构中就应该把这些数据迁移到分布式缓存中存储,让业务服务变成一个无状态的计算节点。迁移后,就可以做到按需动态伸缩,微服务应用在运行时动态增删节点,就不再需要考虑缓存数据如何同步的问题。

七、Spring Cache 三种常用的缓存注解和意义?

@Cacheable ,用来声明方法是可缓存,将结果存储到缓存中以便后续使用相同参数调用时不需执行实际的方法,直接从缓存中取值。

@CachePut,使用 @CachePut 标注的方法在执行前,不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

@CacheEvict,是用来标注在需要清除缓存元素的方法或类上的,当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。

八、Spring Boot 如何设置支持跨域请求?

现代浏览器出于安全的考虑, HTTP 请求时必须遵守同源策略,否则就是跨域的 HTTP 请求,默认情况下是被禁止的,IP(域名)不同、或者端口不同、协议不同(比如 HTTP、HTTPS)都会造成跨域问题。

一般前端的解决方案有:

使用 JSONP 来支持跨域的请求,JSONP 实现跨域请求的原理简单的说,就是动态创建<script>标签,然后利用<script>的 SRC 不受同源策略约束来跨域获取数据。缺点是需要后端配合输出特定的返回信息。

利用反应代理的机制来解决跨域的问题,前端请求的时候先将请求发送到同源地址的后端,通过后端请求转发来避免跨域的访问。

后来 HTML5 支持了 CORS 协议。CORS 是一个 W3C 标准,全称是”跨域资源共享”(Cross-origin resource sharing),允许浏览器向跨源服务器,发出 XMLHttpRequest 请求,从而克服了 AJAX 只能同源使用的限制。它通过服务器增加一个特殊的 Header[Access-Control-Allow-Origin]来告诉客户端跨域的限制,如果浏览器支持 CORS、并且判断 Origin 通过的话,就会允许 XMLHttpRequest 发起跨域请求。

前端使用了 CORS 协议,就需要后端设置支持非同源的请求,Spring Boot 设置支持非同源的请求有两种方式。

第一,配置 CorsFilter。

@Configuration

public class GlobalCorsConfig {

@Bean

public CorsFilter corsFilter() {

CorsConfiguration config = new CorsConfiguration();

config.addAllowedOrigin("");

config.setAllowCredentials(true);

config.addAllowedMethod("");

config.addAllowedHeader("");

config.addExposedHeader("");

 UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
 configSource.registerCorsConfiguration("/**", config); return new CorsFilter(configSource);
}

}

需要配置上述的一段代码。第二种方式稍微简单一些。

第二,在启动类上添加:

public class Application extends WebMvcConfigurerAdapter {

@Override public void addCorsMappings(CorsRegistry registry) { 
 registry.addMapping("/**") 
 .allowCredentials(true) 
 .allowedHeaders("*") 
 .allowedOrigins("*") 
 .allowedMethods("*"); 
}

}

九、JPA 和 Hibernate 有哪些区别?JPA 可以支持动态 SQL 吗?

JPA 本身是一种规范,它的本质是一种 ORM 规范(不是 ORM 框架,因为 JPA 并未提供 ORM 实现,只是制定了规范)因为 JPA 是一种规范,所以,只是提供了一些相关的接口,但是接口并不能直接使用,JPA 底层需要某种 JPA 实现,Hibernate 是 JPA 的一个实现集。

JPA 是根据实体类的注解来创建对应的表和字段,如果需要动态创建表或者字段,需要动态构建对应的实体类,再重新调用Jpa刷新整个 Entity。动态 SQL,mybatis 支持的最好,jpa 也可以支持,但是没有 Mybatis 那么灵活。

十、Spring 、Spring Boot 和 Spring Cloud 的关系?

Spring 最初最核心的两大核心功能 Spring Ioc 和 Spring Aop 成就了 Spring,Spring 在这两大核心的功能上不断的发展,才有了 Spring 事务、Spring Mvc 等一系列伟大的产品,最终成就了 Spring 帝国,到了后期 Spring 几乎可以解决企业开发中的所有问题。

Spring Boot 是在强大的 Spring 帝国生态基础上面发展而来,发明 Spring Boot 不是为了取代 Spring ,是为了让人们更容易的使用 Spring 。

Spring Cloud 是一系列框架的有序集合。它利用 Spring Boot 的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用 Spring Boot 的开发风格做到一键启动和部署。

Spring Cloud 是为了解决微服务架构中服务治理而提供的一系列功能的开发框架,并且 Spring Cloud 是完全基于 Spring Boot 而开发,Spring Cloud 利用 Spring Boot 特性整合了开源行业中优秀的组件,整体对外提供了一套在微服务架构中服务治理的解决方案。

用一组不太合理的包含关系来表达它们之间的关系。

Spring ioc/aop > Spring > Spring Boot > Spring Cloud

读者福利赠送

同时还有一份Java中高级面试高频考点文档免费分享给大家,与上面那份文档面经掌握其中的大部分知识足以面对很多互联网公司包括阿里蚂蚁金服等面试了

Java senior interview finishing high frequency test sites

Which include the JVM, locks, concurrency, Java reflection, Spring principle, micro-services, a lot of knowledge Zookeeper, databases, data structures, and so on.

Plus my learning architect exchange group interview 778,477,315 receive free encyclopedia

dfc373eed08349d592bf38128dd0159f


7b465dba53fd465aa12e30a02be35024



Guess you like

Origin blog.51cto.com/14456091/2443045