Spring Interview Questions and Answers, Spring interview questions

Source: Internet
Author: User

Spring Interview Questions and Answers, Spring interview questions

Spring Overview

1. What is spring?

Spring is an open source development framework for java Enterprise applications. Spring is mainly used to develop Java applications, but some extensions are used to build web applications on the J2EE platform. The Spring framework aims to simplify Java enterprise-level application development and promote good programming habits through the POJO-based programming model.

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

Lightweight:Spring is lightweight and the basic version is about 2 MB.

Control reversal:Spring implements loose coupling through control inversion. Objects provide their dependencies, rather than creating or searching for dependent objects.

Aspect-oriented programming (AOP ):Spring supports Aspect-Oriented Programming and separates application business logic from system services.

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

MVC Framework: The Spring WEB framework is a well-designed framework and a good alternative to the Web framework.

Transaction Management:Spring provides a continuous transaction management interface that can be extended to local transactions and global transactions (JTA ).

Exception Handling:Spring provides convenient APIs to convert specific technical exceptions (such as thrown by JDBC, Hibernate or JDO) into consistent unchecked exceptions.

3. modules of Spring?

The following are the basic modules of the Spring framework:

Core module

Bean module

Context module

Expression Language module

JDBC module

ORM module

OXM module

Java Messaging Service (JMS) module

Transaction module

Web module

Web-Servlet module

Web-Struts module

Web-Portlet module

4. Core container (application context) Module.

This is a basic Spring module that provides basic functions of the spring framework. BeanFactory is the core of any spring-based application. The Spring framework is built on this module, which makes Spring a container.

5. BeanFactory-BeanFactory implementation example.

Bean factory is an implementation of the factory model and provides the control inversion function to separate application configurations and dependencies from the real application code.

The most common BeanFactory implementation is the XmlBeanFactory class.

6. XMLBeanFactory

The most common method is org. springframework. beans. factory. xml. XmlBeanFactory. It loads beans according to the definition in the XML file. The container reads configuration metadata from the XML file and uses it to create a fully configured system or application.

7. interpreting the AOP Module

The AOP module is used for face-oriented development for Spring applications. Many support is provided by the AOP alliance, which ensures the commonality between Spring and other AOP frameworks. This module introduces metadata programming to Spring.

8. Explain the JDBC abstraction and DAO Module.

By using the JDBC abstraction and DAO module, the database code is concise and can avoid problems caused by database resource error shutdown. It is based on error messages of different databases, provides a unified exception access layer. It also uses Spring's AOP module to provide transaction management services for objects in Spring applications.

9. interpreting object/link ing integration module.

By providing the ORM module, Spring allows us to use an object/relational ing (ORM) tool on direct JDBC. Spring supports integrating mainstream ORM frameworks, such as Hiberate, JDO and iBATIS SQL Maps. Spring transaction management also supports all the above ORM frameworks and JDBC.

10. Explanation of the WEB module.

The Spring WEB module is built on the application context module and provides a context suitable for web applications. This module also supports a variety of web-oriented tasks, such as transparently processing multiple file upload requests and binding Program-level request parameters to your business objects. It also supports Jakarta Struts.

12. Spring configuration file

The Spring configuration file is an XML file that contains class information, describes how to configure them, and how to call each other.

13. What is a Spring IOC container?

Spring IOC is responsible for creating objects, managing objects (through dependency injection (DI), assembling objects, configuring objects, and managing the entire lifecycle of these objects.

14. What are the advantages of IOC?

IOC or dependency injection minimizes the amount of application code. It makes applications easy to test and does not require singleton and JNDI lookup mechanisms for unit tests. Minimum Cost and minimal intrusion allow loose coupling. The IOC container supports hungry class initialization and lazy loading during service loading.

15. What are the common implementations of ApplicationContext?

FileSystemXmlApplicationContext:This container loads the definition of beans from an XML file. The full path name of the XML Bean configuration file must be provided to its constructor.

ClassPathXmlApplicationContext:This container also loads the definition of beans from an XML file. Here, you need to set classpath correctly because the container will find bean configuration in classpath.

WebXmlApplicationContext:This container loads an XML file that defines all the beans of a WEB application.

16. What is the difference between Bean Factory and Application contexts?

Application contexts provides a way to process text messages. A common practice is to Load file resources (such as images) and they can publish events to beans registered as listeners. In addition, the operations that have to be processed programmatically by the bean factory on the objects in the container or container can be processed in the Application contexts in a declarative manner. Application contexts implements the MessageSource interface, which can be used to obtain localized messages in a pluggable manner.

17. What does a Spring application look like?

An interface that defines some features.

This includes attributes, its Setter, getter methods, and functions.

Spring AOP.

Spring XML configuration file.

Client programs that use the above features.

Dependency Injection

18. What is Spring dependency injection??

Dependency injection is a common concept in IOC and has multiple interpretations. This concept means that you do not need to create an object, but only need to describe how it is created. You do not directly assemble your components and services in the code, but you need to describe which components need the services in the configuration file, and then a container (IOC container) is responsible for assembling them.

19. What types of IOC (dependency injection) methods are available?

Constructor dependency injection:Constructor dependency injection is implemented through the constructor that triggers a class. This class has a series of parameters, and each parameter represents a dependency on other classes.

Setter method injection:Setter method injection is the setter method that the container calls to instantiate the bean by calling the non-argument constructor or the non-argument static factory method, that is, the setter-based dependency injection is implemented.

20. Which of the following dependency injection methods are recommended: constructor injection or Setter method injection?

You can use either of the following dependency Methods: constructor injection and Setter method injection. The best solution is to use the constructor parameters to implement mandatory dependencies, and the setter method to implement optional dependencies.

Spring Beans

21. What is Spring beans?

Spring beans are java objects that form the backbone of Spring applications. They are initialized, assembled, and managed by Spring IOC containers. These beans are created through the metadata configured in the container. For example, it is defined as an XML file.

All beans defined by the Spring framework are single-piece beans. There is an attribute "singleton" in the bean tag. If it is assigned TRUE, bean is a single piece, otherwise it is a prototype bean. The default value is TRUE, so all beans in the Spring framework are single pieces by default.

22. What does a Spring Bean definition contain?

The definition of a Spring Bean contains all the configuration metadata required by the container, including how to create a bean, its lifecycle details and its dependencies.

23. How to provide configuration metadata for Spring containers?

There are three important methods to provide configuration metadata for Spring containers.

XML configuration file.

Annotation-based configuration.

Java-based configuration.

24. How do you define the scope of a class?

When defining a bean in Spring, we can also declare a scope for this bean. It can be defined through the scope attribute in bean definition. For example, when Spring needs to produce a new bean instance each time, the scope attribute of bean is specified as prototype. On the other hand, each time a bean is used, the same instance must be returned, and the scope attribute of this bean must be set to singleton.

25. Explain the scopes of several bean supported by Spring.

The Spring framework supports the following five bean scopes:

Singleton:Bean has only one instance in each Spring ioc container.

Prototype: A bean can have multiple instances.

Request: A bean is created for each http request. This scope is only valid in the case of web-based Spring ApplicationContext.

Session: In an HTTP Session, a bean defines an instance. This scope is only valid in the case of web-based Spring ApplicationContext.

Global-session: In a global HTTP Session, a bean defines an instance. This scope is only valid in the case of web-based Spring ApplicationContext.

The default Spring bean scope is Singleton.

26. Is the singleton bean in the Spring framework thread-safe?

No. The Singleton bean in the Spring framework is not thread-safe.

27. Explain the bean lifecycle in the Spring framework.

The Spring container reads the bean definition from the XML file and instantiates the bean.

Spring fills in all attributes according to bean definitions.

If bean implements the BeanNameAware interface, Spring transmits the bean ID to the setBeanName method.

If Bean implements the BeanFactoryAware interface, Spring passes beanfactory to the setBeanFactory method.

If there are any BeanPostProcessors associated with the bean, Spring will call them in the postProcesserBeforeInitialization () method.

If the bean implements IntializingBean, call its afterPropertySet method. If the bean declares the initialization method, call this initialization method.

If BeanPostProcessors is associated with beans, the postProcessAfterInitialization () method of these beans will be called.

If bean implements DisposableBean, it will call the destroy () method.

28. What are important bean lifecycle methods? Can you reload them?

There are two important bean lifecycle methods. The first one is setup, which is called when the container loads the bean. The second method is teardown, which is called when the container unmounts the class.

The bean tag has two important attributes (init-method and destroy-method ). You can use them to customize the initialization and logout methods. They also have corresponding annotations (@ PostConstruct and @ PreDestroy ).

29. What is Spring internal bean?

When a bean is used only as the attribute of another bean, it can be declared as an internal bean. To define the inner bean, in Spring's XML-based configuration metadata, elements can be used in or within an element. Internal beans are usually anonymous, and their Scope is generally prototype.

30. How to inject a java set into Spring?

Spring provides the following configuration elements:

Type is used to inject a column value, and the same value is allowed.

Type is used to inject a group of values, and the same value is not allowed.

Type is used to inject a group of key-value pairs. The key and value can be of any type.

Type is used to inject a group of key-value pairs. The key and value can only be of the String type.

31. What is bean assembly?

Assembly, or bean assembly, refers to assembling beans in the Spring container, provided that the container needs to know the dependencies of beans and how to assemble them together through dependency injection.

32. What is automatic bean assembly?

Spring containers can automatically assemble beans for mutual cooperation, which means containers do not need to be configured and can automatically process bean collaboration through Bean factories.

33. Explain different methods of automatic assembly.

There are five automatic assembly methods that can be used to guide Spring containers to use automatic assembly methods for dependency injection.

No: Automatic assembly is disabled by default. The ref attribute is explicitly set for assembly.

ByName:Through automatic assembly of parameter names, the Spring container finds in the configuration file that the autowire attribute of bean is set to byname, and then the container tries to match, assemble the bean with the same name as the bean property.

ByType ::Through automatic assembly of parameter types, the Spring container finds in the configuration file that the autowire attribute of bean is set to byType, and then the container tries to match and assemble beans of the same type as the attributes of the bean. If multiple beans meet the conditions, an error is thrown.

Constructor: This method is similarByType, but it must be provided to the constructor parameters. If no parameter type is specified for the constructor parameter, an exception is thrown.

Autodetect:First, try to use constructor for automatic assembly. If it cannot work, use byType.

34.What are the limitations of automatic assembly?

The limitations of Automatic Assembly are:

Rewrite: You still need to define dependencies with configurations, which means you must always rewrite automatic assembly.

Basic Data Type: You cannot automatically Assemble simple attributes, such as basic data types, String strings, and classes.

Fuzzy features:Automatic Assembly is not as accurate as explicit assembly. If possible, explicit assembly is recommended.

35. Can you inject a null and an empty string into Spring?

Yes.

Spring Annotation

36. What is Java-based Spring annotation configuration? Example of some annotations.

Java-based configuration allows you to perform most of your Spring configurations with the help of a small amount of Java annotations, rather than using XML files.

Take the @ Configuration annotation as an example. It is used to mark classes as bean definitions and is used by Spring IOC containers. Another example is the @ Bean annotation, which indicates that this method will return an object and register it as a bean into the Spring application context.

37. What is annotation-based container configuration?

Compared with XML files, annotation-based configuration relies on assembly components through bytecode metadata, rather than the declaration of angle brackets.

Developers can directly configure component classes by using Annotations on corresponding classes, methods, or attributes, rather than using xml to express bean assembly relationships.

38. How to enable annotation assembly?

Annotation assembly is disabled by default. To use annotation Assembly, we must configure elements in the Spring configuration file.

39. @ Required Annotation

This annotation indicates that bean attributes must be set during configuration, explicitly defined by a bean or automatically assembled. If the bean attribute of @ Required annotation is not set, the container will throw BeanInitializationException.

40. @ Autowired Annotation

@ Autowired annotation provides more fine-grained control, including where and how to complete automatic assembly. Like @ Required, it modifies the setter method, constructor, attribute, or PN method with any name and/or multiple parameters.

41. @ Qualifier Annotation

When only one bean of the same type needs to be automatically assembled, use the @ Qualifier annotation and @ Autowire annotation together to eliminate this confusion and specify the exact bean to be assembled.

Spring Data Access

42. How to Use JDBC more effectively in the Spring framework?

The SpringJDBC framework reduces the cost of resource management and error handling. Therefore, developers only need to write statements and queries to access data from data. JDBC can also be used more effectively with the help of the template class provided by the Spring framework. This template is called JdbcTemplate (for example, here)

43. JdbcTemplate

The JdbcTemplate class provides many convenient solutions, such as converting database data into basic data types or objects, executing written or callable database operation statements, and providing custom data error handling.

44. Support for DAO by Spring

Spring's support for data access objects (DAO) is designed to simplify its use with data access technologies such as JDBC, Hibernate or JDO. This allows us to easily switch the persistent layer. During coding, you don't have to worry about capturing exceptions specific to each technology.

45. How does Spring access Hibernate?

There are two methods to access Hibernate in Spring:

Controls reverse Hibernate Template and Callback.

Inherit from HibernateDAOSupport to provide an AOP interceptor.

46. ORM supported by Spring

Spring supports the following ORM:

Hibernate

IBatis

JPA (Java Persistence API)

TopLink

JDO (Java Data Objects)

OJB

47. How to combine Spring and Hibernate through HibernateDaoSupport?

Use Spring SessionFactory to call LocalSessionFactory. The integration process consists of three steps:

Configure the Hibernate SessionFactory.

Inherit from HibernateDaoSupport to implement a DAO.

It is assembled in transactions supported by AOP.

48. Transaction Management types supported by Spring

Spring supports two types of transaction management:

Programmed Transaction Management: This means that you manage transactions programmatically, which brings you great flexibility but is difficult to maintain.

Declarative transaction management:This means that you can separate business code from transaction management. You only need to use annotations and XML configurations to manage transactions.

49. What are the advantages of Spring framework transaction management?

It provides a constant programming mode for different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.

It provides a simple set of APIS for programmatic transaction management, instead of complex transaction APIs such

It supports declarative transaction management.

It is well integrated with various Spring Data Access abstraction layers.

50. Which transaction management type do you prefer?

Most Spring framework users choose declarative transaction management, because it has the least impact on application code, so it is more in line with the idea of a lightweight container without intrusion. Declarative transaction management is better than programmatic transaction management, although it is less flexible than programmatic transaction management (which allows you to control transactions through code.

Spring Aspect-oriented programming (AOP)

51. Interpretation of AOP

Aspect-Oriented Programming, or AOP, is a programming technique that allows the program to modularize the focus, or to cross typical division of responsibility, such as logging and transaction management.

52. Aspect

The core of AOP is the aspect. It encapsulates the common behaviors of multiple classes into Reusable Modules. This module contains a set of APIs to provide cross-cutting functions. For example, a log module can be called the AOP aspect of a log. Depending on your needs, an application can have several facets. In Spring AOP, the Section is implemented by class with @ Aspect annotation.

52. What are the differences between attention points and cross-cutting attention in Spring AOP?

The focus is the behavior of a module in an application. A focus may be defined as a function we want to implement.

The cross-cutting focus is a focus. This focus is on the functions used by the entire application and affects the entire application, such as log, security, and data transmission. Almost every module of the application needs the functions. Therefore, these are all cross-cutting concerns.

54. Connection Point

The connection point represents a certain position of an application. In this position, we can insert an AOP aspect, which is actually the position where an application executes Spring AOP.

55. Notification

A notification is an action to be performed before or after the method is executed. It is actually a code segment that is triggered by the SpringAOP framework during program execution.

Five types of notifications can be applied to the Spring aspect:

Before: Pre-notification, called before a method is executed.

After:Notifications called after method execution, whether or not the method execution is successful.

After-returning:The notification is executed only after the method is successfully completed.

After-throwing:The notification that is executed when the method throws an exception and exits.

Around und:Notifications called before and after method execution.

56. Cut Point

The entry point is one or more connection points, and the notification will be executed in these locations. You can specify the entry point by expression or matching.

57. What is introduction?

The introduction allows us to add new methods and attributes to an existing class.

58. What is the target object?

Objects notified by one or more slices. It is usually a proxy object. It also refers to the notification object.

59. What is proxy?

Proxy is the object created after the target object is notified. From the client perspective, the proxy object is the same as the target object.

60. How many automatic proxies are available?

BeanNameAutoProxyCreator

DefaultAdvisorAutoProxyCreator

Metadata autoproxying

61. What is woven. What are the differences between woven applications?

Weaving is the process of connecting a plane to another application type or object or creating a notification object.

Weaving can be completed during compilation, loading, or running.

62. Explain the implementation of the section based on the XML Schema method.

In this case, the Section is implemented by a regular class and XML-based configuration.

63. annotation-based aspect implementation

In this case (based on the implementation of @ AspectJ), the style of the section Declaration involved is consistent with that of the common java class with java 5 annotation.

Spring MVC

64. What is Spring's MVC framework?

Spring is equipped with a full-featured MVC framework for building Web applications. Spring can be easily integrated with other MVC frameworks, such as Struts and Spring MVC frameworks, to clearly isolate Business Objects and control logic with control inversion. It can also bind request parameters to business objects in a declarative manner.

65. DispatcherServlet

Spring's MVC framework is designed around DispatcherServlet to process all HTTP requests and responses.

66. WebApplicationContext

WebApplicationContext inherits ApplicationContext and adds some special functions necessary for WEB applications. It is different from general ApplicationContext because it can process topics and find associated servlets.

67. What is the controller of Spring MVC framework?

The Controller provides an action to access an application, which is usually implemented through a service interface. The Controller parses user input and converts it to a model presented to the user by a view. Spring implements a control layer in a very abstract way, allowing users to create controllers for multiple purposes.

68. @ Controller Annotation

This annotation indicates that this class assumes the Controller role. Spring does not require you to inherit any other controller base class or reference Servlet APIs.

69. @ RequestMapping Annotation

This annotation is used to map a URL to a class or a specific method.


Join the study exchange group 569772982.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.