2020 Spring tier companies recruit: Spring collection of interview questions and answers

Preface:

Recently, many people private letter to me, there is no spring on the face questions related to share, today small specially compiled some spring-related interview questions for everyone to share, another must see the last, oh, the end of the text there are big benefits to share.

43d9d50f9ef1471a8dcf9f333c3d3ef4



1. What is spring?

Spring is an open source development framework java enterprise applications. Spring is mainly used to develop Java applications, but some extensions for building web applications is the J2EE platform. Spring Framework goal is to simplify Java enterprise application development, and by POJO-based programming model to promote good programming practice.

2. What are the benefits of using Spring framework is?

Lightweight: Spring is lightweight, basic version is about 2MB.

Inversion of Control: Spring Inversion of Control achieved by loosely coupled, objects are given their dependencies instead of creating them or find dependent objects.

Aspect-oriented programming (AOP): Spring support aspect-oriented programming, and to separate the application business logic and system services.

Container: Spring contains and manages the lifecycle and configuration objects in the application.

MVC framework: Spring WEB framework of a well-designed framework, is a good alternative to Web framework.

Transaction management: Spring provides an ongoing transaction management interface can be extended to up to lower local transaction to a global transaction (JTA).

Exception Handling: Spring API provides a convenient technique related to specific abnormalities (such as a JDBC, Hibernate or JDO thrown) into uniform unchecked exceptions.

3. Spring of which modules?

The following are the basic module Spring Framework:

Core moduleBean moduleContext moduleExpression Language moduleJDBC moduleORM moduleOXM moduleJava Messaging Service(JMS) moduleTransaction moduleWeb moduleWeb-Servlet moduleWeb-Struts moduleWeb-Portlet module

4. core container (application context) module.

这是基本的Spring模块,提供spring 框架的基础功能,BeanFactory 是 任何以spring为基础的应用的核心。Spring 框架建立在此模块之上,它使Spring成为一个容器。

5. BeanFactory – BeanFactory 实现举例

Bean 工厂是工厂模式的一个实现,提供了控制反转功能,用来把应用的配置和依赖从正真的应用代码中分离。

最常用的BeanFactory 实现是XmlBeanFactory 类。

6. XMLBeanFactory

最常用的就是org.springframework.beans.factory.xml.XmlBeanFactory ,它根据XML文件中的定义加载beans。该容器从XML 文件读取配置元数据并用它去创建一个完全配置的系统或应用。

7. 解释AOP模块

AOP模块用于发给我们的Spring应用做面向切面的开发, 很多支持由AOP联盟提供,这样就确保了Spring和其他AOP框架的共通性。这个模块将元数据编程引入Spring。

8. 解释JDBC抽象和DAO模块。

通过使用JDBC抽象和DAO模块,保证数据库代码的简洁,并能避免数据库资源错误关闭导致的问题,它在各种不同的数据库的错误信息之上,提供了一个统一的异常访问层。它还利用Spring的AOP 模块给Spring应用中的对象提供事务管理服务。

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

Spring 通过提供ORM模块,支持我们在直接JDBC之上使用一个对象/关系映射映射(ORM)工具,Spring 支持集成主流的ORM框架,如Hiberate,JDO和 iBATIS SQL Maps。Spring的事务管理同样支持以上所有ORM框架及JDBC。

10. 解释WEB 模块。

Spring的WEB模块是构建在application context 模块基础之上,提供一个适合web应用的上下文。这个模块也包括支持多种面向web的任务,如透明地处理多个文件上传请求和程序级请求参数的绑定到你的业务对象。它也有对Jakarta Struts的支持。

11. Spring配置文件

Spring配置文件是个XML 文件,这个文件包含了类信息,描述了如何配置它们,以及如何相互调用。

12. 什么是Spring IOC 容器?

Spring IOC 负责创建对象,管理对象(通过依赖注入(DI),装配对象,配置对象,并且管理这些对象的整个生命周期。

13. 你可以在Spring中注入一个null 和一个空字符串吗?

可以。

14. IOC的优点是什么?

IOC 或 依赖注入把应用的代码量降到最低。它使应用容易测试,单元测试不再需要单例和JNDI查找机制。最小的代价和最小的侵入性使松散耦合得以实现。IOC容器支持加载服务时的饿汉式初始化和懒加载。

15. ApplicationContext通常的实现是什么?

FileSystemXmlApplicationContext :此容器从一个XML文件中加载beans的定义,XML Bean 配置文件的全路径名必须提供给它的构造函数。

ClassPathXmlApplicationContext:此容器也从一个XML文件中加载beans的定义,这里,你需要正确设置classpath因为这个容器将在classpath里找bean配置。

WebXmlApplicationContext:此容器加载一个XML文件,此文件定义了一个WEB应用的所有bean。

16. Bean 工厂和 Application contexts 有什么区别?

Application contexts提供一种方法处理文本消息,一个通常的做法是加载文件资源(比如镜像),它们可以向注册为监听器的bean发布事件。另外,在容器或容器内的对象上执行的那些不得不由bean工厂以程序化方式处理的操作,可以在Application contexts中以声明的方式处理。Application contexts实现了MessageSource接口,该接口的实现以可插拔的方式提供获取本地化消息的方法。

17. 一个Spring的应用看起来象什么?

一个定义了一些功能的接口。

这实现包括属性,它的Setter , getter 方法和函数等。

Spring AOP。

Spring 的XML 配置文件。

使用以上功能的客户端程序。

18. 什么是Spring的依赖注入?

依赖注入,是IOC的一个方面,是个通常的概念,它有多种解释。这概念是说你不用创建对象,而只需要描述它如何被创建。你不在代码里直接组装你的组件和服务,但是要在配置文件里描述哪些组件需要哪些服务,之后一个容器(IOC容器)负责把他们组装起来。

19. 有哪些不同类型的IOC(依赖注入)方式?

构造器依赖注入:构造器依赖注入通过容器触发一个类的构造器来实现的,该类有一系列参数,每个参数代表一个对其他类的依赖。

Setter方法注入:Setter方法注入是容器通过调用无参构造器或无参static工厂 方法实例化bean之后,调用该bean的setter方法,即实现了基于setter的依赖注入。

20. 哪种依赖注入方式你建议使用,构造器注入,还是 Setter方法注入?

你两种依赖方式都可以使用,构造器注入和Setter方法注入。最好的解决方案是用构造器参数实现强制依赖,setter方法实现可选依赖。

21.什么是Spring beans?

Spring beans 是那些形成Spring应用的主干的java对象。它们被Spring IOC容器初始化,装配,和管理。这些beans通过容器中配置的元数据创建。比如,以XML文件中<bean/> 的形式定义。

Spring 框架定义的beans都是单件beans。在bean tag中有个属性”singleton”,如果它被赋为TRUE,bean 就是单件,否则就是一个 prototype bean。默认是TRUE,所以所有在Spring框架中的beans 缺省都是单件。

22. 一个 Spring Bean 定义 包含什么?

一个Spring Bean 的定义包含容器必知的所有配置元数据,包括如何创建一个bean,它的生命周期详情及它的依赖。

23. 如何给Spring 容器提供配置元数据?

这里有三种重要的方法给Spring 容器提供配置元数据。

XML配置文件。

基于注解的配置。

基于java的配置。

24. 你怎样定义类的作用域?

当定义一个<bean> 在Spring里,我们还能给这个bean声明一个作用域。它可以通过bean 定义中的scope属性来定义。如,当Spring要在需要的时候每次生产一个新的bean实例,bean的scope属性被指定为prototype。另一方面,一个bean每次使用的时候必须返回同一个实例,这个bean的scope 属性 必须设为 singleton。

25. 解释Spring支持的几种bean的作用域。

Spring框架支持以下五种bean的作用域:

singleton: bean在每个Spring ioc 容器中只有一个实例。

prototype:一个bean的定义可以有多个实例。

request:每次http请求都会创建一个bean,该作用域仅在基于web的Spring ApplicationContext情形下有效。

session:在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。

global-session:在一个全局的HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。

缺省的Spring bean 的作用域是Singleton.

26. Spring框架中的单例bean是线程安全的吗?

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

27. 解释Spring框架中bean的生命周期。

Spring容器 从XML 文件中读取bean的定义,并实例化bean。Spring根据bean的定义填充所有的属性。如果bean实现了BeanNameAware 接口,Spring 传递bean 的ID 到 setBeanName方法。如果Bean 实现了 BeanFactoryAware 接口, Spring传递beanfactory 给setBeanFactory 方法。如果有任何与bean相关联的BeanPostProcessors,Spring会在postProcesserBeforeInitialization()方法内调用它们。如果bean实现IntializingBean了,调用它的afterPropertySet方法,如果bean声明了初始化方法,调用此初始化方法。如果有BeanPostProcessors 和bean 关联,这些bean的postProcessAfterInitialization() 方法将被调用。如果bean实现了 DisposableBean,它将调用destroy()方法。

28. 哪些是重要的bean生命周期方法? 你能重载它们吗?

有两个重要的bean 生命周期方法,第一个是setup , 它是在容器加载bean的时候被调用。第二个方法是 teardown 它是在容器卸载类的时候被调用。

The bean 标签有两个重要的属性(init-method和destroy-method)。用它们你可以自己定制初始化和注销方法。它们也有相应的注解(@PostConstruct和@PreDestroy)。

29. 什么是Spring的内部bean?

当一个bean仅被用作另一个bean的属性时,它能被声明为一个内部bean,为了定义inner bean,在Spring 的 基于XML的 配置元数据中,可以在 <property/>或 <constructor-arg/> 元素内使用<bean/> 元素,内部bean通常是匿名的,它们的Scope一般是prototype。

30. 在 Spring中如何注入一个java集合?

Spring提供以下几种集合的配置元素:

<list>类型用于注入一列值,允许有相同的值。

<set> 类型用于注入一组值,不允许有相同的值。

<map> 类型用于注入一组键值对,键和值都可以为任意类型。

<props>类型用于注入一组键值对,键和值都只能为String类型。

31. 什么是bean装配?

装配,或bean 装配是指在Spring 容器中把bean组装到一起,前提是容器需要知道bean的依赖关系,如何通过依赖注入来把它们装配到一起。

32. 什么是bean的自动装配?

Spring 容器能够自动装配相互合作的bean,这意味着容器不需要<constructor-arg>和<property>配置,能通过Bean工厂自动处理bean之间的协作。

33. 解释不同方式的自动装配 。

有五种自动装配的方式,可以用来指导Spring容器用自动装配方式来进行依赖注入。

no:默认的方式是不进行自动装配,通过显式设置ref 属性来进行装配。

byName: automatic assembly parameter names, Spring found bean container property is set to the autowire byname in the configuration file, after trying to match the container, and the assembly of the bean property with the same name bean.

byType: Type parameter automatic assembly, Spring containers found in the configuration file autowire the bean property is set to byType, * after trying to match the container, and the assembly of the bean property with the same type of bean. If more than one bean qualify, then throw an error.

constructor: This mode is similar to byType, but the constructor parameters to be provided to, if not determined constructor parameter type arguments, it will throw an exception.

autodetect: First, try to use the constructor to automatically assemble, if not work, use byType way.

34. What are the limitations of automatic assembly?

Automatic limitation of the assembly is:

Rewrite: You still need to use <constructor-arg> and <property> configuration to define dependencies means always override the automatic assembly.

Basic data types: You can not simply attribute automatic assembly, as the basic data types, String String, and class.

Blur characteristic: automatic assembly explicit fitting accuracy is better, if possible, use the explicit assembly.

Readers Benefits:

Top manufacturers of interview answers and more manufacturers Zhenti Xiao Bian also compiled together, but also prepared a framework advanced video material

Receive mode: Focus on jobs number [my people] can receive Java Zhou

Face questions over 400 pages of documentation (pdf)

708cf88526a64308ae02083fc5dd8e76


Advanced video learning videos

acd14392dfc54ff3ada05f3c3cab75f8


Rational use of every minute of their own time to enhance their learning, do not use the "no time" to hide his ideological laziness! Young, hard fight, give an account of their own future!


Guess you like

Origin blog.51cto.com/14456091/2471183