Summary of the Spring



1. What is Spring?

Spring is an open source development framework for Java enterprise applications. Spring is primarily used to develop Java applications, but some extensions are targeted at building Web applications on the J2EE platform. The Spring framework aims to simplify Java enterprise application development and promote good programming habits through a POJO-based programming model.


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

  • Lightweight: Spring is lightweight, with a base version of about 2MB.
  • Inversion of control: Spring implements loose coupling through inversion of control, where objects give out their dependencies rather than creating or finding dependent objects.
  • Faceted Programming (AOP) : Spring supports faceted programming and separates application business logic from system services.
  • Container: Spring contains and manages the life cycle and configuration of objects in your application.
  • MVC Framework: Spring’s WEB framework is a well-designed framework and a good alternative to WEB frameworks.
  • Transaction Management: Spring provides a continuous transaction management interface that extends up to local and down to global transactions (JTA).
  • Exception handling: Spring provides convenient apis for turning technology-specific exceptions (such as those thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.


3. What modules does Spring consist of?

Here 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 the basic Spring module that provides the basic functionality of the Spring framework, and the BeanFactory is the core of any Spring-based application. The Spring framework builds on this module, which makes Spring a container.


5. BeanFactory – Implementation example of BeanFactory

The Bean factory is an implementation of the factory pattern that provides inversion of control to separate application configuration and dependencies from the real application code.

The most common implementation of BeanFactory is the XmlBeanFactory class.


6. XMLBeanFactory

The most common one is

Org. Springframework. Beans. Factory. XML. XmlBeanFactory, it according to the loading beans defined in the XML file. The container reads configuration metadata from an XML file and uses it to create a fully configured system or application.


Explain the AOP module

AOP modules are used to deliver faceted development to our Spring applications, and much of the support is provided by the AOP consortium, which ensures the commonality of Spring and other AOP frameworks. This module introduces metadata programming to Spring.


8. Explain the JDBC abstraction and DAO modules

By using THE JDBC abstraction and DAO module, the database code is kept simple and the problems caused by the incorrect shutdown of database resources can be avoided. It provides a unified exception access layer on the error information of various databases. It also leverages Spring’s AOP modules to provide transaction management services to objects in Spring applications.


9. Explain the object/relational mapping integration module

Spring supports the use of an object/relational mapping (ORM) tool on top of direct JDBC by providing ORM modules. Spring supports integration with major ORM frameworks such as Hiberate,JDO, and iBATIS SQL Maps. Spring’s transaction management also supports all of the above ORM frameworks and JDBC.


10. Explain WEB modules

Spring’s WEB module is built on the Application Context module to provide a context suitable for WEB applications. This module also includes support for a variety of Web-oriented tasks, such as transparently handling multiple file upload requests and program-level request parameters binding to your business objects. It also has support for 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 the Spring IOC container?

Spring IOC is responsible for creating objects, managing them (through dependency injection (DI), assembling them, configuring them, and managing their entire life cycle.


14. What are the advantages of IOC?

IOC or dependency injection minimizes the amount of code in your application. It makes applications easy to test, eliminating the need for singletons and JNDI lookups for unit tests. Loose coupling is possible with minimal cost and intrusion. The IOC container supports hunger-style initialization and lazy loading when loading services.


15. What is the common implementation of ApplicationContext?

FileSystemXmlApplicationContext: this container loading beans from an XML file, the definition of XML Bean configuration file the full path name must be provided to the constructor.

ClassPathXmlApplicationContext: this container is loaded from an XML file beans definition, here, you need to properly set the classpath because the container will be in the classpath to find bean configuration.

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 handle text messages, one common practice is to load file resources (such as mirrors) that can publish events to beans registered as listeners. In addition, operations performed on containers or objects inside containers that have to be handled programmatically by bean factories can be handled declaratively in Application Contexts.

Application Contexts implements the MessageSource interface, which provides a pluggable way to get localized messages.


17. What does a Spring application look like?

  • An interface that defines some functionality.
  • This implementation includes the property, its setters, getters, functions, etc.
  • Spring AOP.
  • Spring’s XML configuration file.
  • A client program that uses the above functionality.


Dependency injection


18. What is Spring’s dependency injection?

Dependency injection, an aspect of IOC, is a common concept that has multiple interpretations. The idea is that instead of creating an object, you just describe how it was created. You don’t assemble your components and services directly in code, but describe which components require which services in the configuration file, and then a container (IOC container) takes care of assembling them.


What are the different types of IOC (dependency injection) approaches?

Constructor dependency injection: Constructor dependency injection is implemented by the container firing the constructor of a class that has a series of parameters, each representing a dependency on another class.

Setter method injection: Setter method injection implements setter-based dependency injection by invoking Setter methods of a bean after the bean has been instantiated by calling either a no-argument constructor or a no-argument static factory method.


20. Which dependency injection method do you recommend, constructor injection or Setter injection?

You can use both methods of dependency, constructor injection and Setter injection. The best solution is to implement mandatory dependencies with constructor arguments and optional dependencies with setter methods.


Spring Beans


What are Spring beans?

Spring Beans are the Java objects that form the backbone of a Spring application. They are initialized, assembled, and managed by the Spring IOC container. The Beans are created from metadata configured in the container. For example, as defined in an XML file.

Beans defined by the Spring framework are singleton Beans. There is an attribute “singleton” in the bean tag. If it is set to TRUE, the bean is a singleton, otherwise it is a Prototype bean. The default is TRUE, so all Beans in the Spring framework are singleton by default.


22. What does a Spring Bean definition contain?

The definition of a Spring Bean contains all configuration metadata that the container must know, including how to create a Bean, its lifecycle details, and its dependencies.


23. How do I provide configuration metadata to the Spring container?

There are three important ways to provide configuration metadata to the Spring container:

  • 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 the bean. It can be defined by the scope property in the bean definition. For example, when Spring produces a new bean instance each time it needs to, the scope property of the bean is specified as prototype. On the other hand, a bean must return the same instance every time it is used, and the scope property of the bean must be set to Singleton.


25. Explain the scope of several beans supported by Spring.


  • Singleton: Beans have only one instance per Spring IOC container.
  • Prototype: A bean definition can have multiple instances.
  • Reuqest: Each HTTP request creates a bean, and this scope is only valid in the Web-based Spring ApplicationContext context.
  • Session: In an HTTP session, one bean definition corresponds to one instance. This scope is only valid in the Web-based Spring ApplicationContext context.
  • Global-session: In a global HTTP session, one bean definition corresponds to one instance. This scope is only valid in the Case of web-based Spring ApplicationContext.
  • The default Spring bean is scoped to Singleton.


26. Are singleton beans in the Spring framework thread-safe?

No, singleton beans in the Spring framework are not thread-safe.


27. Explain the lifecycle of beans in the Spring framework.

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

Spring populates all properties according to the bean’s definition.

  • If the bean implements the BeanNameAware interface, Spring passes the bean’s ID to the setBeanName method.
  • If the Bean implements the BeanFactoryAware interface, Spring passes beanFactory to the setBeanFactory method.
  • If you have any that is associated with the bean BeanPostProcessors, Spring will be postProcesserBeforeInitialization () method call them inside.
  • If the bean implements IntializingBean, its afterPropertySet method is called, or if the bean declares an initialization method.
  • If there are BeanPostProcessors associated with beans, these beans postProcessAfterInitialization () method will be invoked.
  • If the bean implements DisposableBean, it will call the destroy() method.


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

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

The bean tag has two important properties (init-method and destroy-method). With them you can customize your initialization and logout methods. They also have corresponding annotations (@postconstruct and @predestroy).


29. What are Spring’s internal beans?

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


30. How do I inject a Java collection in Spring?

Spring provides configuration elements for several collections:

  • Type is used to inject a list of values that are allowed to have the same value.
  • Type is used to inject a set of values that are not allowed to be the same.
  • Type is used to inject a set of key-value pairs, both of which can be of any type.
  • Type is used to inject a set of key-value pairs. The key and value can only be strings.


31. What is bean assembly?

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


32. What is bean autowiring?

The Spring container can automatically assemble beans that cooperate with each other, which means that the container does not need to be configured and can handle collaboration between beans automatically through the bean factory.


33. Explain the different ways of automatic assembly.

There are five autowiring methods that you can use to guide the Spring container to autowiring dependency injection.

  • No: The default mode is not to perform auto assembly, but to perform assembly by explicitly setting the ref attribute.
  • ByName: With parameter name auto-assembly, the Spring container finds in the configuration file that the bean’s Autowire property is set to byName. The container then tries to match and assemble beans with the same name as the bean’s property.
  • ByType: : With parameter type autowire, the Spring container finds that the bean’s autowire property is set to byType in the configuration file. The container then tries to match and assemble beans that have the same type as the bean’s property. If more than one bean meets the criteria, an error is thrown.
  • Constructor: This method is similar to byType, but provides arguments to the constructor. If no constructor argument type is identified with arguments, an exception will be thrown.
  • Autodetect: First try using Constructor for auto-assembly, and if that doesn’t work, use byType.


34. What are the limitations of automatic assembly?

The limitations of autowiring are:

  • Overwrite: You still need to define dependencies with and configuration, which means you always have to override autoassembly.
  • Primitive data types: You cannot auto-assemble simple properties such as primitive data types, strings, and classes.
  • Fuzzy features: Automatic assembly is less accurate than explicit assembly, and explicit assembly is recommended if possible.


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

You can.



The Spring annotations


36. What is the Java-based Spring annotation configuration? Give some examples of annotations.

Java-based configuration allows you to do most of your Spring configuration with the help of a few Java annotations instead of XML files.

The @Configuration annotation, for example, is used to mark the definition of a class that can be used as a bean by the Spring IOC container. Another example is the @bean annotation, which indicates that this method will return an object registered as a Bean in the Spring application context.


37. What is annotation-based container configuration?

In contrast to XML files, annotated configurations rely on assembling components through bytecode metadata rather than Angle bracket declarations.

Instead of using XML to express the assembly of beans, the developer configures the bean directly in the component class by using annotations on the corresponding class, method, or attribute.


38. How do I start annotation assembly?

Annotation assembly is not turned on by default, and in order to use annotation assembly, we must configure elements in the Spring configuration file.


39. @ Required annotation

This annotation indicates that the bean’s property must be set in the configuration, through a bean definition of explicit attribute values or by automatic assembly, if @ Required annotations bean attribute is not set, the container will throw BeanInitializationException.


40. The @autowired annotation

The @AutoWired annotation provides more fine-grained control over where and how autoassembly is done. It is used the same way as @required to modify setter methods, constructors, properties, or PN methods with arbitrary names and/or multiple parameters.


41. @ the Qualifier annotations

When there are multiple beans of the same type but only one that needs to be autoassembled, the @Qualifier annotation is used in combination with the @Autowire annotation to eliminate confusion and specify the exact bean that needs to be assembled.

In addition to these, I prepared a set of systematic architecture and technology system by myself, aiming at the technical needs of current Internet companies and combining with mainstream technologies. Only when you are technically proficient can you convince the public that you can solve technical problems. Many companies attach great importance to the technology of high concurrency and high availability, especially first-line Internet companies. Distributed, JVM, Spring source analysis, micro services and other knowledge points are essential questions for the interview. These things may have been exposed to you at work, but the lack of comprehensive and systematic learning, join the back-end development group: 943918498, or follow wechat public account: Java Information Library, reply to “architecture”, free of charge to receive architecture information.


Spring data Access


42. How can I use JDBC more effectively in the Spring framework?

Using the SpringJDBC framework, the cost of resource management and error handling is reduced. So developers only need to write statements and queries to retrieve data from data. JDBC can also be used more efficiently with the help of a template class provided by the Spring framework called JdbcTemplate (see here for an example).


43. JdbcTemplate

The JdbcTemplate class provides convenient methods for converting database data to primitive data types or objects, executing written or callable database operation statements, and providing custom data error handling.


44. Spring support for DAOs

Spring’s support for data Access Objects (DAOs) is designed to simplify its use with data access technologies such as JDBC, Hibernate, or JDO. This allows us to easily switch the persistence layer. You don’t have to worry about catching exceptions specific to each technology when you code.


45. How do YOU access Hibernate using Spring?

There are two ways to access Hibernate in Spring:

  • Inversion of control: Hibernate Template and Callback.
  • Inheritance: HibernateDAOSupport provides an AOP interceptor.


46. ORM supported by Spring

Spring supports the following ORMS:

  • Hibernate

  • iBatis

  • JPA (Java Persistence API)

  • TopLink

  • JDO (Java Data Objects)

  • OJB


47. How to combine Spring and Hibernate with Hibernateda Support?

LocalSessionFactory = LocalSessionFactory = LocalSessionFactory

  • Configure the Hibernate SessionFactory.
  • Implement a DAO by inheriting HibernateDaoSupport.
  • Assemble in an AOP supported transaction.


48. Transaction management types supported by Spring

Spring supports two types of transaction management:

  • Programmatic transaction management: This means you manage transactions programmatically, giving you great flexibility but being difficult to maintain.
  • Declarative transaction management: This means you can separate business code from transaction management, and you only need to manage transactions with annotations and XML configuration.


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

  • It provides an unchanging programming pattern for different transaction apis such as JTA, JDBC, Hibernate, JPA and JDO.
  • It provides a simple API for programmatic transaction management rather than complex transaction apis such as
  • It supports declarative transaction management.
  • It integrates well with Spring’s various data access abstraction layers.


50. What type of transaction management do you prefer?

Most users of the Spring framework opt for declarative transaction management because it has the least impact on application code and is therefore more in line with the idea of a lightweight container with no intrusion. Declarative transaction management is superior to programmatic transaction management, although it is less flexible than programmatic transaction management, which allows you to control transactions through code.



Spring Faceted Programming (AOP)


51. Explain the AOP

Section-oriented programming, or AOP, is a programming technique that allows programs to modularize horizontally cut concerns, or crosscut typical divisions of responsibility, such as logging and transaction management.


52. The Aspect Aspect

The AOP core is the aspect, which encapsulates the common behavior of multiple classes into a reusable module with a set of apis providing crosscutting functionality. For example, a logging module can be called an AOP aspect of logging. Depending on the requirements, an application can have several facets. In Spring AOP, aspects are implemented through classes annotated with @aspect.


53. What is the difference between concerns and crosscutting concerns in Spring AOP?

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

A crosscutting concern is a concern that is used and affects the entire application, such as logging, security and data transfer, and is required by almost every module of the application. So these are crosscutting concerns.


54. The connection point

Join points represent a location in an application where we can insert an AOP aspect, which is actually a location where the application executes Spring AOP.


55. The notification

An advice is an action to be taken before or after a method executes, and is actually a piece of code that is triggered by the SpringAOP framework during program execution.

Spring facets can apply five types of notifications:

  • Before: Pre-notification, called before a method is executed.
  • After: Notification called after method execution, whether or not the method execution was successful.
  • After-returning: notification that is executed only after a method has successfully completed.
  • After-throwing: Notification executed when a method throws an exception and exits.
  • Around: Notification of calls before and after method execution.


56. The point of tangency

A pointcut is a join point or set of join points at which advice will be executed. Pointcuts can be indicated by expression or match.


57. What is introduction?

Imports allow us to add new methods and attributes to existing classes.


58. What is the target audience?

An object notified by one or more facets. It is usually a proxy object. The person who is advised.


59. What is agency?

A proxy is an object created after notifying the target object. From the client’s point of view, the proxy object is the same as the target object.


60. How many different types of automatic agents are there?

  • BeanNameAutoProxyCreator

  • DefaultAdvisorAutoProxyCreator

  • Metadata autoproxying


61. What is weaving? What are the differences in weaving applications?

Weaving is the process of connecting facets to other application types or objects or creating a notified object.

Weaving can be done at compile time, load time, or run time.


62. Explain the implementation of aspects based on XML Schema.

In this case, the facets are implemented by regular classes as well as XML-based configurations.


63. Explain annotation-based aspect implementation

In this case (based on the @AspectJ implementation), the style of aspect declarations involved is consistent with normal Java classes with Java5 annotations.



Spring的MVC


64. What is Spring’s MVC framework?

Spring comes with a full-featured MVC framework for building Web applications. Spring can be easily integrated with other MVC frameworks, such as Struts, which uses inversion of control to separate business objects from control logic. It also allows you to declaratively bind request parameters to business objects.


65. DispatcherServlet

Spring’s MVC framework is designed around DispatcherServlet, which handles all HTTP requests and responses.


66. WebApplicationContext

WebApplicationContext inherits ApplicationContext and adds some features unique to WEB applications. It is different from ordinary ApplicationContext in that it can handle topics and find the associated servlets.


67. What is the Spring MVC framework controller?

A controller provides a behavior to access an application, which is typically implemented through a service interface. The controller parses user input and transforms it into a model that the view presents to the user. 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 the class acts as a controller and Spring does not require you to inherit any other controller base classes or reference the Servlet API.


69. @ RequestMapping annotation

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