上次更新时间:2019年2月11日

这是有关Spring框架的一些最重要问题的摘要,在面试或面试测试过程中可能会要求您回答这些问题! 您无需担心下一次面试的机会,因为Java Code Geeks在这里为您服务!

您可能会被问到的大多数事情都收集在下面的列表中。 简短地介绍了所有核心模块,从基本的Spring功能(例如Spring Beans)到Spring MVC框架。 在检查了面试问题之后,您应该查看我们的Spring Tutorials页面。

所以,走吧...!

目录

A.Spring概述 B.依赖注入 C.Spring Bean D.Spring注释 E.Spring数据访问 F.Spring面向方面的编程(AOP) G.Spring模型视图控制器(MVC) H.认证与授权

A.Spring概述

1.什么是春天?

Spring是企业Java的开源开发框架。 Spring Framework的核心功能可用于开发任何Java应用程序,但是在Java EE平台上存在一些扩展,可用于构建Web应用程序。 Spring框架旨在通过启用基于POJO的编程模型来使Java EE开发更易于使用并促进良好的编程习惯。

2. Spring Framework有什么好处?

  • 轻量级:在尺寸和透明度方面,Spring是轻量级的。 spring框架的基本版本约为2MB。
  • 控制反转(IOC):使用控制反转技术在Spring中实现松耦合。 对象提供其依赖关系,而不是创建或寻找依赖对象。
  • 面向方面(AOP): Spring支持面向方面的编程 ,并将应用程序业务逻辑与系统服务分开。
  • 容器: Spring包含并管理应用程序对象的生命周期和配置。
  • MVC框架: Spring的Web框架是一个经过精心设计的Web MVC框架 ,它提供了Web框架的替代方案。
  • 事务管理: Spring提供了一个一致的事务管理接口,该接口可以缩小到本地事务,也可以扩大到全局事务(JTA)。
  • 异常处理: Spring提供了一个方便的API,可将技术特定的异常(由JDBC,Hibernate或JDO抛出)转换为一致的未经检查的异常。

3. Spring框架模块是哪些?

大约有20个模块,它们被概括为核心容器,数据访问/集成,Web,AOP(面向方面​​的编程),仪器和测试。 Spring框架的基本模块是:

弹簧芯容器

该层基本上是Spring框架的核心。 它包含以下模块:

  • 核心模块
  • Bean模块
  • 上下文模块
  • 表达语言模块

数据访问/集成

该层提供了与数据库交互的支持。 它包含以下模块:

  • JDBC模块
  • 对象关系映射(ORM)模块
  • Java Messaging Service(JMS)模块
  • 对象XML映射器(OXM)模块
  • 交易管理模块

网页

该层提供了创建Web应用程序的支持。 它包含以下模块:

  • 网络模块
  • Web-MVC模块
  • Web套接字模块
  • Web-Portlet模块

面向方面的编程( AOP )

在此层中,可以使用建议,切入点等来分离代码。
工具-该层为类工具和类加载器实现提供支持。

测试

该层为使用JUnit和TestNG的测试提供支持。

讯息传递

该模块提供对STOMP的支持。 它还支持注释编程模型,该模型用于路由和处理来自WebSocket客户端的STOMP消息。

方面

该模块提供了与AspectJ集成的支持。

4.解释核心容器(应用程序上下文)模块

这是基本的Spring模块,提供了Spring框架的基本功能。 BeanFactory是任何基于Spring的应用程序的心脏。 Spring框架建立在该模块的顶部,该模块构成了Spring容器。

5. BeanFactory实现示例

BeanFactory是工厂模式的实现,该模式应用控制反转将应用程序的配置和依赖项与实际应用程序代码分开。

最常用的BeanFactory实现是XmlBeanFactory类。

6. XMLBeanFactory

最有用的是org.springframework.beans.factory.xml.XmlBeanFactory ,它根据XML文件中包含的定义加载其bean。 该容器从XML文件读取配置元数据,并使用它创建完全配置的系统或应用程序。

7.解释AOP模块

AOP模块用于为支持Spring的应用程序开发方面。 AOP联盟已经提供了很多支持,以确保Spring和其他AOP框架之间的互操作性。 该模块还向Spring引入了元数据编程。

8.解释JDBC抽象和DAO模块

使用JDBC抽象和DAO模块,我们可以确保我们保持数据库代码简洁明了,并防止由于关闭数据库资源失败而导致的问题。 它在多个数据库服务器给出的错误消息之上提供了一层有意义的异常。 它还利用Spring的AOP模块为Spring应用程序中的对象提供事务管理服务。

9.解释对象/关系映射集成模块

通过提供ORM模块,Spring还支持在纯JDBC上使用对象/关系映射(ORM)工具。 Spring提供了与多种流行的ORM框架结合的支持,包括Hibernate,JDO和iBATIS SQL Maps。 Spring的事务管理支持所有这些ORM框架以及JDBC。

10.解释Web模块

Spring Web模块建立在应用程序上下文模块的基础上,提供适用于基于Web的应用程序的上下文。 该模块还包含对一些面向Web的任务的支持,例如透明地处理文件上传的多部分请求以及将请求参数以编程方式绑定到业务对象。 它还包含与Jakarta Struts的集成支持。

11.解释Spring MVC模块

Spring提供了MVC框架来构建Web应用程序。 Spring可以轻松地与其他MVC框架集成,但是Spring的MVC框架是一个更好的选择,因为它使用IoC来提供控制器逻辑与业务对象的清晰分离。 使用Spring MVC,您可以声明式地将请求参数绑定到业务对象。

12. Spring配置文件

Spring配置文件是一个XML文件。 该文件包含类信息,并描述如何配置这些类以及如何将它们相互引入。

13.我们如何拥有多个Spring配置文件?

  • web.xml contextConfigLocation:您可以通过ContextConfigLocation元素将它们全部加载到Web应用程序上下文中。 假设您正在编写Web应用程序,那么您已经在这里拥有了主要的applicationContext。 您需要做的就是在下一个上下文的声明之间放置一些空格。
  • applicationContext.xml导入资源:您可以将主applicationContext.xml添加到web.xml,然后在该主上下文中使用import语句。

14. ApplicationContext的常见实现是什么?

  • FileSystemXmlApplicationContext容器从XML文件加载Bean的定义。 必须将XML bean配置文件的完整路径提供给构造函数。
  • ClassPathXmlApplicationContext容器还从XML文件加载Bean的定义。 在这里,您需要正确设置CLASSPATH ,因为此容器将在CLASSPATH查找bean配置XML文件。
  • WebXmlApplicationContext:容器使用Web应用程序中的所有bean定义加载XML文件。

15. Bean Factory和ApplicationContext有什么区别?

应用程序上下文提供了一种解决文本消息的方法,一种加载文件资源(例如图像)的通用方法,它们可以将事件发布到注册为侦听器的bean。 另外,必须在应用程序上下文中声明性地处理对容器或容器中的Bean的操作,这些操作必须由Bean工厂以编程方式进行处理。 应用程序上下文实现MessageSource ,这是一个用于获取本地化消息的接口,实际实现是可插入的。

16. Spring框架有哪些最佳实践?

Spring框架的一些最佳实践是:

  • 定义名称与类或接口名称相同的单例bean
  • 将Spring bean配置文件放在文件夹而不是根文件夹下
  • 为Spring bean配置文件提供通用的前缀或后缀
  • 尽量避免在Spring XML配置文件中使用导入元素
  • 避免基于XML的Bean配置中的自动接线
  • 始终使用属性占位符将bean属性值外部化
  • 导入名称空间定义时选择默认的无版本XSD
  • 始终在资源路径中放置类路径前缀
  • 即使使用现场级自动接线,也要创建设置器方法
  • 即使服务方法几乎没有将其职责委托给相应的DAO方法,也要创建一个单独的服务层

17.使用Spring Framework的方式有哪些?

您可以使用Spring Framework:

  • 用于编写Web应用程序
  • 公开RESTful服务
  • 保护您的Web应用程序
  • 用于与数据库通信
  • 用于处理长期运行的工作
  • 处理您必须使用的外部资源或系统
  • 用于测试目的
  • 用于独立的Java项目
  • 将您的应用程序转换为可执行文件
  • 将社交媒体集成到您的应用程序中

18.我们如何使用Spring创建返回JSON响应的Restful Web Service?

只要Jackson2在类路径中,Spring Boot应用程序中的任何Spring @RestController默认情况下都应呈现JSON响应。

19. Spring vs Spring MVC vs Spring Boot?

  • Spring:Spring的最重要特征是依赖注入或控制反转。
  • Spring MVC:是一个完整的面向HTTP的MVC框架,由Spring框架管理,基于Servlet。 它相当于JavaEE堆栈中的JSF。
  • Spring Boot:一个实用程序,用于快速设置应用程序,提供开箱即用的配置以构建Spring支持的应用程序。

20. Spring应用程序是什么样的?

  • 接口:定义功能的接口。
  • Bean类:它包含属性,其setter和getter方法,函数等。
  • Spring AOP:提供跨领域关注点的功能。
  • 配置XML文件:包含类的信息以及如何配置它们。
  • 客户端程序:使用该功能。

B.依赖注入

21.什么是Spring IoC容器?

Spring IoC负责创建对象,通过依赖项注入(DI)管理它们,将它们连接在一起,对其进行配置以及管理其完整生命周期。

22.国际奥委会有什么好处?

  • IOC或依赖项注入可最大程度地减少应用程序中的代码量。
  • 由于在单元测试中不需要单例或JNDI查找机制,因此它使测试应用程序变得容易。
  • 以最小的努力和最少的干扰机制促进了松耦合。
  • IOC容器支持急切的实例化和服务的延迟加载。

23.Spring有多少种IOC容器?

  • BeanFactory:本质上,BeanFactory只是高级工厂的接口,该工厂能够维护不同Bean及其依赖项的注册表。 BeanFactory使您可以读取Bean定义并使用Bean工厂访问它们。
  • ApplicationContext:ApplicationContext是Spring应用程序中的中央接口,用于向应用程序提供配置信息。 它在运行时是只读的,但是可以在必要时重新加载并得到应用程序的支持。 许多类实现ApplicationContext接口,从而允许使用各种配置选项和应用程序类型。

24. BeanFactory与ApplicationContext

应用环境 :

  • Bean实例化/接线
  • 自动BeanPostProcessor注册
  • 自动BeanFactoryPostProcessor注册
  • 方便的MessageSource访问(适用于i18n)
  • ApplicationEvent发布

BeanFactor :

  • Bean实例化/接线

25. Spring中的依赖注入是什么?

依赖注入是控制反转(IoC)的一个方面,是一个通用概念,可以用许多不同的方式表示。此概念表示您不创建对象而是描述了如何创建对象。 您不会直接用代码将组件和服务连接在一起,而是描述配置文件中的哪些组件需要哪些服务。 然后,一个容器(IOC容器)负责将其全部挂接。

26.紧耦合和松耦合有什么区别?

紧耦合 :

  • 紧密耦合是一组类彼此高度依赖时。

松耦合 :

  • 松耦合是通过促进单一责任和关注点分离的设计实现的。

27. IoC(依赖注入)有哪些不同类型?

  • 基于构造函数的依赖项注入:当容器调用带有多个参数的类构造函数时,将完成基于构造函数的DI,每个参数代表对其他类的依赖关系。
  • 基于setter的依赖项注入:基于setter的DI是通过在调用无参数构造函数或无参数静态工厂方法实例化bean之后,在bean上调用setter方法来完成的。

28.您会建议哪个DI基于构造函数或Setter的DI?

您可以同时使用基于构造函数和基于Setter的依赖注入。 最好的解决方案是将构造函数参数用于强制性依赖项,将setter用于可选的依赖性。

C.Spring Bean

29.什么是Spring Bean?

Spring Bean是构成Spring应用程序主干的Java对象。 它们由Spring IoC容器实例化,组装和管理。 这些bean是使用提供给容器的配置元数据创建的,例如以XML <bean/>定义的形式。

Spring框架中定义的Bean是单例Bean。 如果指定为true,则bean标记中有一个名为"singleton"的属性,则bean成为单例;如果设置为false,则bean成为原型bean。 默认情况下,它设置为true。 因此,默认情况下,spring框架中的所有bean都是单例bean。

30. Spring Bean定义包含什么?

Spring Bean定义包含容器了解如何创建Bean所需的所有配置元数据,其生命周期详细信息及其依赖项。

31.如何为Spring容器提供配置元数据?

有三种重要的方法可以为Spring容器提供配置元数据:

  • 基于XML的配置文件。
  • 基于注释的配置
  • 基于Java的配置

32.您如何定义bean的范围?

在Spring中定义<bean>时,我们还可以声明bean的作用域。 可以通过bean定义中的scope属性定义。 例如,当Spring每次需要一个新的bean实例时,该bean的scope属性为prototype 。 另一方面,当每次每次都必须由Spring返回bean的相同实例时,必须将bean scope属性设置为singleton

33.解释Spring支持的bean作用域

Spring框架提供了五个作用域,支持以下五个作用域:

  • 单例范围中,Spring将每个Spring IoC容器的bean定义范围限制为一个实例。
  • 原型范围内,单个bean定义可以具有任意数量的对象实例。
  • 请求范围内,将bean定义为HTTP请求。 该范围仅在可感知网络的Spring ApplicationContext中有效。
  • 会话范围中,bean定义的范围是HTTP会话。 此范围也仅在可感知网络的Spring ApplicationContext中有效。
  • 全局会话作用域中,bean定义的作用域是全局HTTP会话。 在Web感知的Spring ApplicationContext中也是这种情况。

Spring Bean的默认范围是Singleton

34. Singleton bean在Spring Framework中线程安全吗?

不,在Spring框架中,单例bean不是线程安全的。

35.在Spring框架中解释Bean的生命周期

  • spring容器从XML文件中找到bean的定义并实例化bean。
  • Spring填充bean定义(DI)中指定的所有属性。
  • 如果bean实现BeanNameAware接口,则spring将bean的id传递给setBeanName()方法。
  • 如果Bean实现BeanFactoryAware接口,则spring将beanfactory传递给setBeanFactory()方法。
  • 如果有任何与Bean相关联的BeanPostProcessors ,Spring会调用postProcesserBeforeInitialization()方法。
  • 如果bean实现IntializingBean ,其afterPropertySet()方法被调用。 如果bean具有init方法声明,则调用指定的初始化方法。
  • 如果有任何与Bean关联的BeanPostProcessor,则将调用其postProcessAfterInitialization()方法。
  • 如果Bean实现DisposableBean ,它将调用destroy()方法。

您可以覆盖它们吗?

bean有两种重要的生命周期方法。 第一个是setup ,当将Bean装入容器时调用。 第二种方法是teardown方法,当从容器中卸载bean时会调用该方法。
bean标签具有两个重要的属性( init-methoddestroy-method ),您可以使用它们定义自己的自定义初始化和destroy方法。 还有相应的注释( @PostConstruct@PreDestroy )。

37.Spring的内在豆是什么?

当一个bean仅用作另一个bean的属性时,可以将其声明为内部bean。 Spring的基于XML的配置元数据提供了在bean定义的<property/><constructor-arg/>元素内使用<bean/>元素的功能,以便定义所谓的内部bean。 内部bean始终是匿名的,并且始终以原型为范围。

38.如何在Spring中注入Java Collection?

Spring提供以下类型的集合配置元素 :

  • 在允许重复的情况下, <list>类型用于注入值列表。
  • <set>类型用于连接一组值,但不能重复。
  • <map>类型用于注入名称-值对的集合,其中名称和值可以是任何类型。
  • <props>类型可用于注入名称-值对的集合,其中名称和值均为字符串。

39.什么是豆类接线?

在Spring容器中将bean组合在一起时,就是进行接线,否则就进行bean接线。 在对bean进行接线时,Spring容器需要知道需要哪些bean,以及如何使用依赖注入将它们绑定在一起。

40.什么是bean自动装配?

Spring容器能够自动装配协作bean之间的关系 。 这意味着可以通过检查BeanFactory的内容而无需使用<constructor-arg><property>元素来自动让Spring通过bean解析协作者(其他bean)。

41.解释自动接线的不同模式吗?

自动装配功能具有五种模式,可用于指示Spring容器使用自动装配进行依赖项注入:

  • 否:这是默认设置。 显式bean参考应该用于接线。
  • byName:自动byName ,Spring容器查看XML配置文件byName autowire属性设置为byName autowire属性。 然后,它尝试将其属性与配置文件中由相同名称定义的bean进行匹配和关联。
  • byType:datatype自动装配时,Spring容器会查看XML配置文件中将autowire属性设置为byType autowire属性。 然后,如果属性的类型与配置文件中的bean名称之一完全匹配,它将尝试匹配并连接属性。 如果存在多个这样的bean,则会引发致命异常。
  • 构造函数:此模式类似于byType ,但类型适用于构造函数参数。 如果容器中不存在构造函数参数类型的一个bean,则将引发致命错误。
  • autodetect: Spring首先尝试通过构造函数使用autowire进行连线,如果它不起作用,Spring尝试通过byType进行自动byType

42.自动装配是否有限制?

自动装配的局限性是:

  • 覆盖:您仍然可以使用<constructor-arg><property>设置指定依赖项,这些设置将始终覆盖自动装配。
  • 原始数据类型:您不能自动连接简单属性,例如原始数据,字符串和类。
  • 令人困惑的性质:自动装配不如显式接线精确,因此,如果可能的话,更喜欢使用显式接线。

43.可以在Spring中注入空字符串值吗?

是的你可以。

D.Spring注释

44.什么是重要的Spring注释?

我在项目中使用的一些Spring注释是:

  • @Component用于指示类是组件。 这些类用于自动检测,并在使用基于注释的配置时配置为Bean。
  • @Controller是一种特定类型的组件,在MVC应用程序中使用,并且通常与@RequestMapping批注一起使用。
  • @Repository批注用于指示组件用作存储库,以及用于存储/检索/搜索数据的机制。 我们可以将此注释与DAO模式实现类一起应用。
  • @Service用于指示类是服务。 通常,提供一些服务的Business Facade类都带有此注释。
  • @Required –此注释仅指示必须在配置时通过bean定义中的显式属性值或通过自动装配来填充受影响的bean属性。 如果尚未填充受影响的bean属性,则容器将引发BeanInitializationException。
  • @ResponseBody –用于发送对象作为响应,通常用于发送XML或JSON数据作为响应。
  • @PathVariable –用于将动态值从URI映射到处理程序方法参数。
  • @Autowired –提供更细粒度的控制,以实现自动布线的位置和方式。 它可以用于在setter方法上自动装配bean,就像@Required批注,在构造函数上,在属性或具有任意名称和/或多个参数的pn方法上一样。
  • @Qualifier –如果有多个相同类型的bean,并且只需要一个属性进行连接,则@Qualifier注释与@Autowired注释一起使用,以通过指定要连接的确切bean来消除混淆。
  • @Scope –用于配置Spring bean的范围。
  • @Configuration –指示Spring IoC容器可以将该类用作bean定义的源。
  • @ComponentScan –应用此注释时,将扫描软件包下所有可用的类。
  • @Bean –对于基于Java的配置,告诉spring用@Bean注释的方法将返回一个对象,该对象应在spring应用程序上下文中注册为bean。
  • 用于配置方面和建议的AspectJ批注,@ Aspect,@ Before,@ After,@ Around,@ Pointcut等。

45. @RequestParam批注做什么?

spring中的@RequestParam批注将查询字符串的参数值绑定到控制器的method参数。

46. @Primary批注的重要性是什么

当存在多个具有相同数据类型的bean时,开发人员将使用特定于Spring的@Primary批注 ,该批注自动为特定bean赋予更高的优先级。 此注释可用于直接或间接使用@Component注释进行注释的任何类,或用于使用@Bean注释进行注释的方法。

47.配置类型XML和注释之间有什么区别?

注释的优点 :

  • 所有信息都在一个文件中
  • 当类更改时,无需修改xml文件

XML文件的优点 :

  • POJO与行为之间的清晰区分
  • 当您不知道哪个POJO负责该行为时,更容易找到该POJO

48. @SpringBootApplication的作用是什么?

@SpringBootApplication注释是在Spring Boot 1.2.0中引入的,它启用了自动配置功能。

该注释封装了三种不同注释的工作:

  • @Configuration :允许开发人员显式注册Bean
  • @ComponentScan :启用组件扫描,以便在Spring的应用程序上下文中自动发现控制器类和其他组件并将其注册为Bean
  • @EnableAutoConfiguration :启用Spring Boot的自动配置功能

该注释采用以下可选参数:

  • exclude :从自动配置中排除类列表
  • excludeNames :从自动配置中排除标准类名的列表
  • scanBasePackage :提供必须应用于扫描的软件包列表
  • scanBasePackageClasses :提供其他包中必须用于扫描的类的列表

49.解释@InitBinder?

该注释以声明日期格式的方法修饰,并且在整个类中,都使用定义的日期格式。 每当使用日期字段@InitBinder进行绑定时; 批注说使用CustomDateEditor,而CustomDateEditor使用@InitBinder中提到的日期格式。

50.定义@ControllerAdvice?

带有@ControllerAdvice的类可以显式声明为Spring Bean,或通过类路径扫描自动检测。 所有此类bean均通过AnnotationAwareOrderComparator进行排序,即基于@Order和Ordered,并在运行时以该顺序应用。 为了处理异常,将在第一个建议中使用匹配的异常处理程序方法选择@ExceptionHandler。 对于模型属性和InitBinder初始化,@ ModelAttribute和@InitBinder方法也将遵循@ControllerAdvice顺序。

51.我们可以发送一个对象作为控制器处理程序方法的响应吗?

是的,我们可以使用@ResponseBody批注在静态 Web服务中发送基于JSON或XML的响应。

52.解释@ModelAttribute?

@ModelAttribute批注引用Model对象的属性,用于准备模型数据。 该注释将方法变量或模型对象绑定到命名的模型属性。 批注接受一个可选值,该值指示模型属性的名称。 @ModelAttribute批注可以在参数级别或方法级别使用。 在参数级别使用此注释是为了接受请求表单的值,而在方法级别使用的是将默认值分配给模型。 让我借助一些示例进一步解释您。

53. @RequestMapping批注

@RequestMapping批注用于将Web请求映射到处理程序类(即Controller)或处理程序方法上,并且可以在方法级别或类级别使用。 如果开发人员在类级别使用@RequestMapping批注,它将用作方法级别路径的相对路径。

给出一些注释示例。

基于Java的配置选项使您无需XML即可编写大多数Spring配置,而借助少量基于Java的注释。
一个示例是@Configuration批注,该批注指示该类可以由Spring IoC容器用作Bean定义的源。 另一个示例是@Bean注释的方法,该方法将返回一个对象,该对象应在Spring应用程序上下文中注册为Bean。

55.什么是基于注释的容器配置?

基于注释的配置提供了XML设置的替代方法,该配置依赖字节码元数据来连接组件,而不是尖括号声明。 通过使用相关类,方法或字段声明上的注释,开发人员无需使用XML来描述bean的连接,而是将配置移入组件类本身。

56.如何打开注释布线?

默认情况下,Spring容器中的注释接线未打开。 为了使用基于注释的接线,我们必须通过配置<context:annotation-config/>元素在Spring配置文件中启用它。

E.Spring数据访问

57. spring JDBC API中存在哪些类?

Spring框架为Jdbc数据库访问提供了以下方法:

  • Jdbc模板
  • SimpleJdbcTemplate
  • NamedParameterJdbcTemplate
  • SimpleJdbcInsert
  • SimpleJdbcCall

58.如何在Spring框架中更有效地使用JDBC?

使用Spring JDBC框架时,减轻了资源管理和错误处理的负担。 因此,开发人员只需编写语句和查询即可将数据往返数据库。 借助于Spring框架提供的模板类JdbcTemplate ( 此处为示例),可以更有效地使用JDBC。

59. JdbcTemplate

JdbcTemplate类提供了许多方便的方法来执行操作,例如将数据库数据转换为原语或对象,执行准备好的和可调用的语句以及提供自定义数据库错误处理。

60.如何通过Spring JdbcTemplate获取记录?

有两个接口可用于从数据库中获取记录:

  • ResultSetExtractor
  • 行映射器

61. NamedParameterJdbcTemplate的优点是什么?

NamedParameterJdbcTemplate建立在spring提供的JDBCTemplate之上,用于与数据库的低层通信。 它使得可以将SQL查询参数作为键值对传递。 结果,程序代码比索引或“?”更具可读性,因此可以作为更好的文档。 占位符方法。 如果参数数量巨大,则后者尤其难以遵循。

62.什么是Spring JDBCTemplate类以及如何使用它?

JdbcTemplate类执行SQL查询,更新语句和存储过程调用,对ResultSet执行迭代并提取返回的参数值。 它处理资源的创建和释放,从而避免出现诸如忘记关闭连接之类的错误。 它还捕获JDBC异常,并将其转换为org.springframework.dao包中定义的通用,信息量更大的异常层次结构。

63. JDBC和Spring JDBC有什么区别?

Spring JDBC顶层提供的Spring JDBC增值

  1. 定义连接参数
  2. 打开连接
  3. 指定语句
  4. 准备并执行语句
  5. 设置循环以遍历结果(如果有)
  6. 进行每次迭代的工作
  7. 处理任何异常
  8. 处理交易
  9. 关闭连接

64. Spring DAO支持

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows us to switch between the persistence technologies fairly easily and to code without worrying about catching exceptions that are specific to each technology.

65. What are the ways to access Hibernate by using Spring?

There are two ways to access Hibernate with Spring:

  • Inversion of Control with a Hibernate Template and Callback.
  • Extending HibernateDAOSupport and Applying an AOP Interceptor node.

66. ORM's Spring support

Spring supports the following ORM's:

  • 冬眠
  • iBatis
  • JPA (Java Persistence API)
  • TopLink
  • JDO (Java Data Objects)
  • OJB

67. How can we integrate Spring and Hibernate using HibernateDaoSupport?

Use Spring's SessionFactory called LocalSessionFactory . The integration process is of 3 steps:

  • Configure the Hibernate SessionFactory
  • Extend a DAO Implementation from HibernateDaoSupport
  • Wire in Transaction Support with AOP

68. Types of the transaction management Spring support

Spring supports two types of transaction management:

  • Programmatic transaction management: This means that you have managed the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
  • Declarative transaction management: This means you separate transaction management from the business code . You only use annotations or XML based configuration to manage the transactions.

69. What are the benefits of the Spring Framework's transaction management?

  • It provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.
  • It provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA.
  • It supports declarative transaction management.
  • It integrates very well with Spring's various data access abstractions.

70. Which Transaction management type is more preferable?

Most users of the Spring Framework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non-invasive lightweight container. Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code.

F.Spring Aspect Oriented Programming (AOP)

71. Explain AOP

Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management.

72. What are the advantages of spring AOP?

一个。 It is non-invasive

  • Your service/domain classes get advised by the aspects (cross cutting concerns) without adding any Spring AOP related classes or interfaces into the service/domain classes.
  • Allows the developer to concentrate on the business code, instead the cross cutting concerns.

b。 Its implemented in pure Java

  • No need for a special compilation unit, or a special class loader

C。 It uses Spring's IOC for dependency injection

  • Aspects can be configured as normal spring beans.

d。 As any other AOP framework, it weaves cross cutting concerns into the classes, without making a call to the cross cutting concerns from those classes.

e。 Centralize or modularize the cross cutting concerns

  • Easy to maintain and make changes to the aspects
  • Changes need to be made in one place.
  • In one of your classes you don't want to have logging, it can easily be achieved by modifying the point cut in the respective aspect (logging aspect). So you need to make changes in only one place.

F。 Provision to create aspects using schema based (XML configuration) or @AspectJ annotation based style.

G。 Easy to configure

73. What are the AOP implementation?

AOP implementations:

  • Spring AOP :

    • Runtime weaving through proxy is done
    • It supports only method level PointCut
    • It is DTD based
  • Apache AspectJ
    • Compile time weaving through AspectJ Java tools is done
    • It suports field level Pointcuts
    • It is schema based and Annotation configuration
  • JBoss AOP
    • JBoss AOP is not only a framework, but also a prepackaged set of aspects that are applied via annotations, pointcut expressions, or dynamically at runtime. Some of these include caching, asynchronous communication, transactions, security, remoting, and many many more.

74. What are the AOP terminology?

  1. 方面
  2. Advice
  3. 切入点
  1. JoinPoint
  2. 介绍
  3. Target Object
  4. AOP Proxy
  5. 织造

75. Aspect

The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. It ia a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. In Spring AOP, aspects are implemented using regular classes annotated with the @Aspect annotation ( @AspectJ style).

76. Join point

The join point represents a point in an application where we can plug-in an AOP aspect. It is the actual place in the application where an action will be taken using Spring AOP framework.

77. Advice

The advice is the actual action that will be taken either before or after the method execution. This is actual piece of code that is invoked during the program execution by the Spring AOP framework.

Spring aspects can work with five kinds of advice:

  • before: Run advice before the a method execution.
  • after: Run advice after the a method execution regardless of its outcome.
  • after-returning: Run advice after the a method execution only if method completes successfully.
  • after-throwing: Run advice after the a method execution only if method exits by throwing an exception.
  • around: Run advice before and after the advised method is invoked.

78. Pointcut

The pointcut is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns.

79. What is Introduction?

An Introduction allows us to add new methods or attributes to existing classes.

80. What is Target object?

The target object is an object being advised by one or more aspects. It will always be a proxy object. It is also referred to as the advised object.

81. What is a Proxy?

A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.

82. What are the different types of AutoProxying?

  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator
  • Metadata autoproxying

83. What is Weaving? What are the different points where weaving can be applied?

Weaving is the process of linking aspects with other application types or objects to create an advised object.
Weaving can be done at compile time, at load time, or at runtime.

84. What is the difference between concern and cross-cutting concern in Spring AOP

The Concern is behavior we want to have in a module of an application. A Concern may be defined as a functionality we want to implement.
The cross-cutting concern is a concern which is applicable throughout the application and it affects the entire application. For example, logging, security and data transfer are the concerns which are needed in almost every module of an application, hence they are cross-cutting concerns.

85. Explain XML Schema-based aspect implementation?

In this implementation case, aspects are implemented using regular classes along with XML based configuration.

86. Explain annotation-based (@AspectJ based) aspect implementation

This implementation case ( @AspectJ based implementation) refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.

G.Spring Model View Controller (MVC)

87. What is Spring MVC framework?

Spring comes with a full-featured MVC framework for building web applications . Although Spring can easily be integrated with other MVC frameworks, such as Struts, Spring's MVC framework uses IoC to provide a clean separation of controller logic from business objects. It also allows to declaratively bind request parameters to business objects.

88. What are the minimum configurations needed to create Spring MVC application?

For creating a simple Spring MVC application, we would need to do the following tasks:

  • Add spring-context and spring-webmvc dependencies in the project.
    Configure DispatcherServlet in the web.xml file to handle requests through spring container.
  • Spring bean configuration file to define beans, if using annotations then it has to be configured here. Also we need to configure view resolver for view pages.
  • Controller class with request mappings defined to handle the client requests.

89. List out all the concepts that are available in the MVC Architecture?

  • The browser sends a request to DispatcherServlet
  • DispatcherServlet knows the HanderMapping and can find the appropriate controllers
  • Controllers execute the request and put the data in the model and return back the view name to the DispatcherServlet.
  • DispatcherServlet uses the view name and ViewResolver to map to the view.

90. DispatcherServlet

The Spring Web MVC framework is designed around a DispatcherServlet that handles all the HTTP requests and responses.

91. WebApplicationContext

The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes, and that it knows which servlet it is associated with.

92. What is Controller in Spring MVC framework?

Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.

93. How would you relate Spring MVC Framework to MVC architecture?

Spring MVC framework :

The Spring Framework is an open source application framework and inversion of control container for the Java platform.

MVC architecture :

Model View Controller (MVC) as it is popularly called, is a software design pattern for developing web applications

94. What is ViewResolver in Spring MVC?

Spring provides ViewResolver , which enable you to render models in a browser without tying you to a specific view technology. Out of the box, Spring enables you to use JSPs, Velocity templates and XSLT views, for example. The two interfaces which are important to the way Spring handles views are ViewResolver and View. The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.

95. What is a MultipartResolver and when its used?

Spring MVC provide multipart support with MultipartResolver . The MultipartResolver parses inbound multipart requests. You can enable multipart support by registering a MultipartResolver bean in the DispatcherServlet application context.

96. How to upload file in Spring MVC Application?

Spring provides built-in support for uploading files through MultipartResolver interface implementations. There is also a validator for the field, which will be used to check if the file uploaded is of size greater than zero. There is finally a simple view that contains a form with the option to upload a file.

97. How to validate form data in Spring Web MVC Framework?

There are 3 different ways to perform validation : using annotation, manually, or a mix of both.

98. What is Spring MVC Interceptor and how to use it?

Spring's handler mapping mechanism includes handler interceptors , which are useful when you want to apply specific functionality to certain requests, for example, checking for a principal. Interceptors must implement HandlerInterceptor from the org.springframework.web.servlet package. This interface defines three methods:

  • preHandle is called before the actual handler is executed.
  • postHandle is called after the handler is executed.
  • afterCompletion is called after the complete request has finished .

H.Authentication and authorization

99. What is Spring Security?

Spring security is one of the most important modules of the Spring framework. It enables the developers to integrate the security features easily and in a managed way. In the following example, we will show how to implement Spring Security in a Spring MVC application.

100. Why Spring Boot?

Here are some useful benefits of using Spring Boot :

  • Automatic configuration of an application uses intelligent defaults based on the classpath and the application context, but they can be overridden to suit the developer's requirements as needed.
  • When creating a Spring Boot Starter project, you select the features that your application needs and Spring Boot will manage the dependencies for you.
  • A Spring Boot application can be packaged as a JAR file. The application can be run as a standalone Java application from the command line using the java -jar command.
  • When developing a web application, Spring Boot configures an embedded Tomcat server so that it can be run as a standalone application. (Tomcat is the default, but you can configure Jetty or Undertow instead.) You can package the application as a WAR file and deploy it to an external servlet container if you prefer
  • Spring Boot includes many useful non-functional features (such as security and health checks) right out of the box.

好的,现在您可以开始面试了! Don't forget to check our dedicated page full of Spring Tutorials , and our Examples dedicated subsection !

如果您喜欢此功能,请订阅我们的时事通讯,以享受每周更新和免费白皮书! 另外,请查看我们的课程以获得更高级的培训!

欢迎您提出您的意见,我们会将其纳入本文!

翻译自: https://www.javacodegeeks.com/2014/05/spring-interview-questions-and-answers.html

100份Spring面试问答-最终名单(PDF下载)相关推荐

  1. Spring面试问答

    I have posted a lot of Spring Tutorials recently. This post will help you get through Spring intervi ...

  2. 多线程面试题_100多线程和Java并发面试问答–最终清单(PDF下载)

    多线程面试题 在这篇文章中,我们将提供有关多线程和Java并发面试问答的综合文章. 编者注:并发始终是开发人员的挑战,编写并发程序可能非常困难. 引入并发时,有很多事情可能会崩溃,并且系统的复杂性会大 ...

  3. Spring Boot 之 itext导出pdf下载

    Java操作pdf框架详解 itext是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有 ...

  4. 150个Java面试问答-最终清单(PDF下载)

    我们的Java面试问题和答案集合全都涉及可以在Java面试中使用的不同类型的问题,以使雇主可以测试您在Java和面向对象编程方面的技能. 在以下各节中,我们将讨论有关面向对象编程及其特性的Java面试 ...

  5. 资深和新手的100大 Selenium面试问答

    下面的Java Selenium问题指南涵盖了100个最重要的Selenium自动化面试问题,包括简单的Selenium Java面试问题以及带答案的Selenium自动化测试面试问题.本文包含了面向 ...

  6. 你连原理都还没弄明白?快来瞧瞧这份Spring面试小抄

    二叉树 定义 二叉树是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的.分别称为根结点的左子树和右子树组成. 图解 二叉树特点 由二叉树定义以及 ...

  7. 休眠面试问答-最终清单

    这是有关Hibernate Framework的一些最重要问题的摘要,可能会要求您在访谈中回答! 您无需担心下一次面试的机会,因为Java Code Geeks在这里为您服务! 您可能会被问到的大多数 ...

  8. 25个经典的Spring面试问答

    为什么80%的码农都做不了架构师?>>>    本人收集了一些在大家在面试时被经常问及的关于Spring的主要问题,这些问题有可能在你下次面试时就会被问到.对于本文中未提及的Spri ...

  9. Spring学习总结(8)——25个经典的Spring面试问答

    1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...

最新文章

  1. 算术表达式求解背景_1.8 C++算术运算符与表达式
  2. 图形处理(二)固定边界参数化
  3. android一天一次弹窗,Android自定义Toast,多次弹出时取消上次弹出,最后一次弹出为准...
  4. Qt读取TXT文件时,GBK与UTF-8编码判断
  5. Ubuntu16.04安装Python3.7及其pip3并切换为默认版本
  6. linux自动备份网站和数据库,到另外服务器上,为当前用户创建定时任务
  7. OpenCV_03 图像的算数操作:图像的加法+图像的混合
  8. Linux调试时常见问题,C程序在linux下调试时经常出现的问题
  9. 中信建投软件测试,中信建投笔试经验-范例
  10. SharePoint 2019 图文安装教程
  11. 添加附件测试的测试点
  12. rk3399 Android9.0 ota升级失败
  13. ANN to SNN
  14. 事件冒泡 vs 事件捕获 vs 事件委托 的区别
  15. Cadence Allegro 巧用直接打印功能得到PDF图纸图文教程及视频演示
  16. 机器学习中特征选择概述
  17. ZCU104开发板:开发板介绍1
  18. 华为开发者贡献 Linux 内核补丁,将核心内核函数速度提升 715 倍
  19. 苹果新专利获授权;谷歌因 反竞争 的安卓政策在印度被罚款;微软将很快支持编辑器校对电子邮件| 每日大事件...
  20. 计算机桌面没有cd驱动器,电脑没有光盘驱动器怎么解决?

热门文章

  1. 阳泉2021高考成绩查询时间段,2021年阳泉高考成绩排名及成绩公布时间什么时候出来...
  2. java运行环境变量及自定义变量
  3. JSP引入CSS文件无法生效的问题
  4. java 字符串文字筛选_重新开始Java的原始字符串文字讨论
  5. 度量空间的应用_使用Dropwizard度量标准监视和测量无功应用
  6. java8 streams_Java 8 Streams API:对流进行分组和分区
  7. javaone_JavaOne 2015:为JDK 9做准备– blog @ CodeFX
  8. 内核中断处理流程_处理中断
  9. ogm session_带有Hibernate OGM的NoSQL –第一部分:持久化您的第一个实体
  10. jax-ws和jax-rs_使用JAX-RS和Spring构建HATEOAS API