11个春季面试必备问题 *

最好的Spring开发人员和工程师可以回答的基本问题. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

Hire a Top Spring Developer Now
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, and project managers in the world. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

What is a Spring Bean Factory? 有哪些可用的实现?

View answer

A BeanFactory 是实例化的实际容器吗, 配置和管理所有Spring bean及其依赖项. Bean工厂由接口表示 org.springframework.beans.factory.BeanFactory 其子接口包括:

  • ApplicationContext
  • WebApplicationContext
  • AutowireCapableBeanFactory

所有这些都是通过:

  • AnnotationConfigWebApplicationContext
  • XmlWebApplicationContext
  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext

重要的是要注意实现可以对应于多个接口.

2.

可以覆盖或控制bean生命周期的哪些步骤?

View answer

The init() 方法将bean加载到容器时调用 init-method 属性添加到XML配置中 @PostConstruct annotation. The destroy() 方法在从容器中卸载bean时调用 destroy-method 属性添加到XML配置中 @PreDestroy annotation. If a bean is a prototype-scoped, 客户端代码必须清理对象并释放原型bean所持有的昂贵资源. 要让Spring容器释放由原型作用域bean持有的资源,请尝试使用自定义 BeanPostProcessor,其中包含对需要清理的bean的引用.

开发人员可以实现各种接口来调用bean生命周期中的特定行为, such as InitializingBean and DisposableBean, as well as BeanNameAware, BeanFactoryAware and ApplicationContextAware.

3.

Spring支持哪些bean作用域?它们意味着什么? Which is used by default?

View answer

Spring框架支持以下作用域:

  • singleton (used by default): This means a single instance per Spring container; not thread-safe
  • prototype:表示任意数量的对象实例.
  • request:这将bean定义限定为HTTP请求. 只在具有web感知的Spring ApplicationContext上下文中有效.
  • session:这将bean定义限定为HTTP会话. 只在具有web感知的Spring ApplicationContext上下文中有效.
  • global-session:这将bean定义限定为全局HTTP会话. 只在具有web感知的Spring ApplicationContext上下文中有效.

申请加入Toptal的发展网络

并享受可靠、稳定、远程 Freelance Spring Developer Jobs

Apply as a Freelancer
4.

什么是依赖注入(DI)? What are the types of DI?

View answer

依赖注入是这样一个概念:您不创建对象,而是描述应该如何创建它们, 然后期望预先创建的对象被传入. Likewise, 您不需要直接将组件连接在一起,而是通过配置文件或注释来描述需要哪些组件. Spring容器负责其余的工作.

DI可以是基于构造函数的,也可以是基于setter的. 当容器调用带有多个参数的类构造函数时,基于构造函数的DI就完成了, 每个类都表示对其他类的依赖. 当容器在实例化bean之后调用bean上的Setter方法时,基于Setter的依赖注入就完成了.

5.

描述Spring bean的生命周期.

View answer

Spring bean的生命周期包括以下步骤:

  1. Instantiation
  2. Properties population
  3. Call of setBeanName() method of BeanNameAware
  4. Call of setBeanFactory() method of BeanFactoryAware
  5. Call of setApplicationContext() of ApplicationContextAware
  6. Pre-initialization with BeanPostProcessor
  7. Call of afterPropertiesSet() method of InitializingBean
  8. Custom init method
  9. Post-initialization with BeanPostProcessor
  10. Bean is ready to use
  11. Call of destroy() method of DisposableBean
  12. Custom destroy method

数字11-12是除原型外所有瞄准镜的实际值, 因为Spring不管理原型bean的完整生命周期:容器实例化, configures, 然后组装一个原型对象并将其交给客户端,而不再记录该原型实例.

6.

什么是Spring应用程序上下文? one的一些例子用法是什么?

View answer

An ApplicationContext 是扩展BeanFactory功能的接口吗. In addition to the BeanFactory’s methods, ApplicationContext provides the ability to:

  • 加载文件资源 ResourcePatternResolver interface
  • 将事件发布到已注册的侦听器(通过 ApplicationEventPublisher interface)
  • 解析支持国际化的消息(使用 MessageSource interface).

在应用程序运行时,它是只读的.

The easiest way to create an ApplicationContent instance is:

ApplicationContext ctx = new FileSystemXmlApplicationContext("应用程序.xml");

Loading resources is done with:

ctx.getresource (String locationPattern);
ctx.getResource(String location);

发布事件非常简单:

ctx.publishEvent (ApplicationEvent事件);
ctx.publishEvent(Object event);

国际化支持消息可以通过以下方式完成:

ctx.getMessage(String code, Object[] args, String defaultMessage, Locale);
ctx.getMessage(String code, Object[] args, Locale Locale);
ctx.getMessage(MessageSourceResolvable, resolvable, Locale);
7.

在Spring的上下文中,什么是“stereotype”?? 现有的刻板印象是什么?它们之间的区别是什么?

View answer

构造型是一个类级别的注释,表示类型或方法在整个体系结构中的角色(在概念级别), rather than implementation). 在Spring中,这些注释位于包中 org.springframework.stereotype.

目前,这个包有以下注释:

  • @Component 指示带注释的类是一个“组件”。. 当使用基于注释的配置和类路径扫描时,这些类被认为是自动检测的候选者.
  • @Controller 表示一个带注释的类是一个“控制器”(Controller).g. a web controller).
  • @Repository 表示一个带注释的类是一个“存储库”。, 最初由领域驱动设计(Evans)定义, 2003)作为“封装存储的机制”, retrieval, 搜索行为模拟了一组对象.
  • @Service 表示带注释的类是一个“服务”。, 最初由领域驱动设计(Evans)定义, 2003)作为“作为独立于模型中的接口提供的操作”, with no encapsulated state.也可能表明一个类是业务服务Facade(在核心J2EE模式意义上)或类似的东西.

这些不同的类型主要允许开发人员轻松区分带注释的类的目的. Starting with Spring 2.5, @Controller, @Repository and @Service serve as a specialization of @Component,允许通过类路径扫描自动检测实现类.

8.

如何将属性加载并注入到Spring Bean中?

View answer

Let’s say we have a custom.properties 定义数据库连接超时属性的文件 connection.timeout. 要将这个属性加载到Spring上下文中,我们需要定义一个 propertyConfigurer bean:


    

之后我们可以使用Spring表达式语言将属性注入到其他bean中:


    

在基于注释的配置中也是如此,如下所示:

@Value("${connection.timeout}") 
private int timeout;
9.

将类配置为Spring Bean有哪些不同的方法?

View answer

配置Spring Bean有多种方法:XML配置, 基于Java的配置和基于注释的配置.

XML configuration

Java Based Configuration

任何对象都可以放入Spring Context中,并在以后作为普通bean重用.

ConfigurableApplicationContext上下文;
context.getBeanFactory().registerSingleton(name, obj);
Annotation Based Configuration

配置Spring Bean可以使用 @Bean 注释,它与 @Configuration classes.

@Configuration
public class MyConfiguration {

	@Bean
	public MyService getService(){
		return new MyService();
	}
}

The annotations @Component, @Service, @Repository and @Controller 也可以与类一起使用,将它们配置为Spring bean. 在这种情况下,必须提供基本包位置来扫描这些类,如下所示:


10.

What is Bean wiring? How does autowiring work?

View answer

Bean连接是在初始化时注入Spring Bean依赖项的过程. 通常最佳实践是显式地连接所有依赖项, (with XML configuration, for example), 但是Spring也支持自动装配 @Autowired annotation. 要启用此注释,我们需要将 context:annotation-config 元素添加到Spring配置文件中. 为避免自动装配时bean映射中的冲突,bean名称必须提供 @Qualifier annotation.

有几种不同的方法可以自动装配Spring Bean:

  • byName -使用此类型设置器方法进行依赖注入, 在注入依赖项的类和Spring配置文件中,变量名应该是相同的.
  • byType 为了使其发挥作用,应该只为该特定类配置一个bean.
  • Via constructor - similar to byType,但type应用于构造函数参数.
  • 通过自动检测——现在已经过时了,在Spring 3中使用.0或更早的时候,这被用于构造函数或 byType.
11.

哪种依赖注入方法更好:基于构造器还是基于设置器?

View answer

你既可以使用基于构造函数的依赖注入,也可以使用基于setter的依赖注入. 最好的解决方案是对强制依赖项使用构造函数参数,对可选依赖项使用setter.

面试不仅仅是棘手的技术问题, 所以这些只是作为一个指南. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, 回答所有问题也不能保证成为A级考生. At the end of the day, 招聘仍然是一门艺术,一门科学,需要大量的工作.

Why Toptal

Tired of interviewing candidates? 不知道该问什么才能让你得到一份好工作?

让Toptal为你找到最合适的人.

Hire a Top Spring Developer Now

我们的Spring开发者专属网络

希望找到一份Spring开发人员的工作?

让Toptal为你找到合适的工作.

Apply as a Spring Developer

工作机会从我们的网络

Submit an interview question

提交的问题和答案将被审查和编辑, 并可能会或可能不会选择张贴, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for Spring Developers?

Looking for Spring Developers? 查看Toptal的Spring开发人员.

Joe Cavazos

Freelance Spring Developer

United StatesToptal Member Since June 19, 2016

Joe是一名Java应用程序开发人员,拥有超过8年的企业和小型应用程序开发经验. 他精通关系数据库、应用服务器、REST api和测试.

Show More

Slava Medvediev

Freelance Spring Developer

PortugalToptal Member Since October 21, 2019

Slava是一名经过认证的Java程序员,在IT行业拥有12年以上的经验. 他作为测试工程师开始了他的IT职业生涯,此后一直担任测试主管, Java developer, lead developer, and tech lead. 除了在服务器端技术方面的丰富经验外,他还具有很强的工程和沟通技巧, frameworks, and development tools.

Show More

Nemanja Stefanovic

Freelance Spring Developer

SerbiaToptal Member Since September 10, 2019

Nemanja是一位积极进取的软件工程师,拥有出色的技术和解决问题的能力. Recently, 他在微软的Office自然语言团队中参与了不同的项目——从设计到产品的各个方面, coding, testing, 以及后端服务api的验证. Nemanja对客户端/服务器应用程序感兴趣,并擅长于 .NET技术,还可以与Java技术栈一起工作.

Show More

Toptal Connects the Top 3% 世界各地的自由职业人才.

Join the Toptal community.

Learn more