69 Classic Spring Noodles Questions and Answers

Source: Internet
Author: User

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 for Web applications that build the EE platform. The Spring framework goal is to simplify Java enterprise application development and promote good programming habits through the pojo-based programming model.

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

    • Light weight: Spring is a lightweight, basic version of about 2MB

    • Control Inversion: Spring is loosely coupled through control inversion, and objects are given their dependencies, rather than creating or locating dependent objects

    • tangent-oriented Programming (AOP): Spring supports tangent-oriented programming and separates application business logic from System services

    • container: Spring contains and manages the life cycle and configuration of objects in your app

    • 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 can be extended up to the local transaction down to the global transaction (JTA)

    • exception Handling: Spring provides a convenient API for translating specific technology-related exceptions (such as those thrown by jdbc,hibernate or jdo) into a consistent unchecked exception

3. What modules does spring consist of?

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 the basic spring module that provides the basic functionality of the spring framework, and Beanfactory is at the heart of any spring-based Application. The spring framework builds on this module, which makes spring a container.

5. Beanfactory–beanfactory Implementation Examples

The Bean factory is an implementation of the factory pattern and provides a control reversal function that separates the Application's configuration and dependencies from the true application Code.

The most common beanfactory implementation is the Xmlbeanfactory class.

6. Xmlbeanfactory

The most common is org.springframework.beans.factory.xml.XmlBeanFactory, which loads the beans based on the definitions in the XML file. The container reads the configuration metadata from the XML file and uses it to create a fully configured system or Application.

7. Interpreting the AOP module

AOP modules are used to send to our spring applications for aspect-oriented development, and many support is provided by the AOP alliance, which ensures the commonality of spring and other AOP frameworks. This module introduces Meta-data programming to Spring.

8. Interpreting the JDBC Abstraction and DAO modules

By using JDBC Abstraction and DAO module, The database code is concise, and the problem of database resource error shutdown is avoided, It provides a unified exception access layer on the error information of different database. It also leverages Spring's AOP modules to provide transaction management services to objects in spring applications.

9. Interpreting the Object/relational Mapping integration module

Spring supports the integration of mainstream ORM frameworks such as HIBERATE,JDO and IBATIS SQL maps by providing an ORM module that enables us to use an object/relational mapping mapping (orm) tool on top of direct jdbc. Spring's transaction management also supports all of the above ORM frameworks and Jdbc.

10. Interpreting the Web module

Spring's Web modules are built on the application context module and provide a context suitable for web Applications. This module also includes support for multiple web-oriented tasks, such as transparently handling multiple file upload requests and binding of Program-level request parameters to your business Objects. It also has support for Jakarta Struts.

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 life cycle of these Objects.

What are the advantages of ioc?

IOC or dependency injection minimizes the amount of code Applied. It makes the application easy to test, and unit tests no longer require singleton and Jndi lookup Mechanisms. The minimum cost and minimal intrusion allow loose coupling to be achieved. The IOC container supports a hungry man initialization and lazy loading when the service is Loaded.

What is the usual implementation of applicationcontext?

    • filesystemxmlapplicationcontext: This container loads the definition of beans from an XML file, and 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, where you need to set the classpath correctly because the container will look for the bean configuration in Classpath.

    • webxmlapplicationcontext: This container loads an XML file that defines all the beans for a web app.

What is the difference between the Bean factory and the application contexts?

Application contexts provides a way to process text messages, and a common practice is to load file resources (such as Mirrors) that can publish events to beans registered as Listeners. In addition, operations performed on objects within a container or container that have to be programmatically handled by the Bean factory can be handled declaratively in the application Contexts. The application contexts implements the Messagesource interface, the implementation of which provides a way to get localized messages in a pluggable manner.

17. What does a spring application look like?

    • An interface that defines a number of functions

    • This implementation includes attributes, its setter, getter methods and functions, etc.

    • Spring AOP

    • XML configuration file for Spring

    • Client programs that use the above features

Dependency Injection

18. What is Spring's dependency injection?

Dependency injection, an aspect of the ioc, is a common concept, and it has many interpretations. The idea is that you don't have to create an object, you just need to describe how it was created. You do not assemble your components and services directly in the code, but the configuration file describes which components require which services, and then a container (the IOC Container) is responsible for assembling Them.

19. What are the different types of IOC (dependency Injection) methods?

    • constructor Dependency Injection: constructor Dependency injection is implemented by a constructor that triggers a class by the container, which has a series of parameters, each representing a dependency on another class.

    • Setter Method Injection: Setter method Injection is the method by which a container invokes the setter of the bean by invoking the parameterless constructor or the Non-parametric static factory method to instantiate the bean, which implements a setter-based dependency Injection.

20. What kind of dependency injection do you recommend using, constructor injection, or setter method injection?

Both of your dependencies can be used, constructor injection and setter method Injection. The best solution is to implement a forced dependency with constructor parameters, and the setter method implements an optional Dependency.

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 the spring IOC container. These beans are created through the metadata that is configured in the Container. For example, it is defined in the form of <bean/> in an XML file.

The beans defined by the Spring framework are all single-piece beans. In the bean tag there is a property "singleton", if it is assigned to True,bean is a single piece, otherwise it is a prototype Bean. The default is true, so all beans in the spring frame are single-piece by Default.

22. What does a Spring Bean definition contain?

The definition of a spring Bean contains all the 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?

Here 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 wants to produce a new bean instance each time it is needed, the Bean's scope property is specified as Prototype. On the other hand, a bean must return the same instance each time it is used, and the Bean's scope property must be set to Singleton.

25. explain the scope of several beans supported by spring

The spring framework supports the following five types of bean scopes:

    • singleton: The bean has only one instance in each spring IOC container.

    • prototype: The definition of a bean can have multiple instances.

    • request: Each HTTP request creates a bean that is only valid in the case of a web-based spring Applicationcontext.

    • session: in an HTTP session, a bean definition corresponds to an instance. This scope is valid only in the case of a web-based spring Applicationcontext.

    • global-session: in a global http session, a bean definition corresponds to an instance. This scope is valid only in the case of a web-based spring Applicationcontext.

The scope of the default spring bean is Singleton.

is a singleton bean in the spring framework thread-safe?

no, Singleton beans in the spring framework are not thread-safe.

27. explaining the Bean's life cycle in the spring framework

    • The spring container reads the bean definition from the XML file and instantiates the Bean.

    • Spring populates all properties based on 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 any beanpostprocessors,spring associated with the bean are called within the postprocesserbeforeinitialization () method.

    • If the bean implementation is intializingbean, call its afterpropertyset method, and if the Bean declares the initialization method, call this initialization method.

    • If there are beanpostprocessors and Bean associations, The postprocessafterinitialization () method of these beans will be called.

    • If the bean implements disposablebean, it will call the destroy () Method.

28. What are the important bean life-cycle methods? Can you reload them?

There are two important bean life-cycle methods, the first of which is setup, which is called when the container loads the Bean. The second method is teardown it 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 the initialization and logoff methods Yourself. They also have corresponding annotations (@PostConstruct and @predestroy).

29. What is Spring's internal bean?

When a bean is used only as a property of another bean, it can be declared as an internal bean, in order to define the inner bean, in Spring's xml-based configuration metadata, in the <property/> or < The constructor-arg/> element uses <bean/> elements, and internal beans are usually anonymous, and their scope is generally prototype.

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

Spring provides configuration elements for the following collections:

    • The <list> type is used to inject a column of values, allowing the same value.

    • The <set> type is used to inject a set of values that do not allow the same value.

    • The <map> type is used to inject a set of Key-value pairs, and the keys and values can be any type.

    • The <props> type is used to inject a set of Key-value pairs, and the keys and values can only be of type string.

31. What is a bean assembly?

assembly, or Bean assembly, is the assembly of beans in a spring container, provided that the container needs to know the Bean's dependencies and how to assemble them together through dependency Injection.

32. What is the automatic assembly of beans?

Spring containers can automatically assemble mutually cooperative beans, which means that containers do not require <constructor-arg> and <property> configuration, and can automatically handle collaboration between beans through the bean factory.

33. explain the different ways of automatic assembly

There are five types of automated assembly methods that can be used to guide spring containers with automatic assembly for dependency Injection

    • no: The default way is not to assemble automatically, but by explicitly setting the ref Property.

    • byname: automatically assembled by the parameter name, the spring container discovers that the Bean's Autowire property is set to ByName in the configuration file, and then the container tries to match, assemble, and Bean whose attributes have the same name.

    • bytype: Automatic assembly by parameter type, the spring container discovers that the Bean's Autowire property is set to Bytype in the configuration file, and then the container tries to match, assemble, and have the same bean type as the Bean's Property. If more than one bean meets the criteria, an error is Thrown.

    • constructor: This is similar to bytype, but to give the constructor arguments, an exception will be thrown if there is no deterministic constructor parameter type for the Parameter.

    • autodetect: First try to use constructor to move the assembly, if not, use the Bytype method.

34. What are the limitations of automatic assembly?

The limitations of automatic Assembly Are:

    • rewrite: You still need <constructor-arg> and <property> configuration to define dependencies, which means you always rewrite automatic assembly.

    • Basic Data type: You cannot automatically assemble simple properties such as basic data types, string strings, and Classes.

    • Fuzzy characteristics: automatic assembly is not as precise as explicit assembly, it is recommended to use explicit assembly if possible.

35. can you inject a null and an empty string in spring?

OK.

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, rather than through an XML file, with the help of a small number of Java Annotations.

As an example of the @configuration annotation, it is used to mark a class as a Bean's definition and be used by the spring IOC container. Another example is the @bean annotation, which indicates that this method will return an object that is registered as a bean into the spring application Context.

37. What is annotation-based container configuration?

In contrast to XML files, Annotation-type configuration relies on the assembly of bytecode metadata rather than the declaration of Angle Brackets.

Instead of using XML to describe the assembly relationship of a bean, a developer is configured in a direct component class by using annotations on the corresponding class, method, or Property.

38. How to open the annotation assembly?

Annotation assembly is not turned on by default, in order to use annotation assembly, We must configure the <context:annotation-config/> element in the spring configuration File.

@Required notes

This note indicates that the Bean attribute must be set at configuration time, through an explicit attribute value defined by a bean, or by automatic assembly, if the Bean property of the @required annotation is not set, the container throws Beaninitializationexception.

@Autowired notes

@Autowired annotations provide finer granularity of control, including where and how automated assembly is DONE. Its usage, like @required, modifies a setter method, a constructor, a property, or a PN method with any name and/or multiple Parameters.

@Qualifier annotations

When there are multiple beans of the same type but only one need to be automatically assembled, use @qualifier annotations and @autowire annotations in conjunction to eliminate this confusion and specify the exact beans that need to be assembled.

Spring data access

42. How do I use JDBC more effectively in the spring framework?

With the Springjdbc framework, the cost of resource management and error handling will be Mitigated. So developers just write statements and queries from data access 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 (see here for examples)

Jdbctemplate.

The JdbcTemplate class provides a number of convenient ways to address such things as transforming database data into basic data types or objects, executing written or callable database operation statements, and providing custom data error handling.

Spring's support for DAO

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 persistence layer. Don't worry about capturing each technology-specific exception when coding.

45. How do I access hibernate by using spring?

There are two ways to access hibernate in Spring:

    • Control invert Hibernate template and Callback

    • Inheritance Hibernatedaosupport provides an AOP interceptor

Spring-supported ORM

Spring supports the following orm:

    • Hibernate

    • IBatis

    • JPA (Java Persistence API)

    • TopLink

    • JDO (Java Data Objects)

    • Ojb

47. How do I combine spring and hibernate with hibernatedaosupport?

Call Localsessionfactory with Spring's sessionfactory. The integration process is divided into three steps:

    • Configure the Hibernate sessionfactory

    • Inherit hibernatedaosupport implement a DAO

    • Assembling in aop-supported transactions

The type of transaction management supported by Spring.

Spring supports two types of transaction management:

    • Programmatic transaction Management: This means that you manage transactions programmatically, giving you great flexibility, but difficult to maintain.

    • declarative Transaction Management: This means that you can separate business code from transaction management, and you only need to manage transactions with annotations and XML configurations.

What are the benefits of the transaction management of the spring framework?

    • It provides a constant programming pattern for different transactional APIs such as JTA,JDBC,HIBERNATE,JPA and jdo.

    • It provides a simple set of APIs for programmatic transaction management rather than some complex transactional APIs such as

    • It supports declarative transaction Management.

    • It is well integrated with spring's various data access abstraction Layers.

50. What type of transaction management are you more inclined to use?

Most spring framework users choose declarative transaction management because it has the least impact on application code and therefore more in line with the idea of a lightweight, non-intrusive container. 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. Interpreting AOP

Aspect-oriented programming, or aop, is a programming technique that allows a program to modularize horizontal-cut concerns, or to cross-cutting typical segregation of responsibilities, such as logging and transaction Management.

Aspect Facets

The AOP core is the facet, which encapsulates the common behavior of multiple classes into reusable modules that contain a set of APIs that provide crosscutting Functionality. For example, a log module can be called an AOP facet of a log. Depending on the requirements, an application can have several facets. In spring aop, facets are implemented by classes with @aspect Annotations.

52. What is the difference between focus and crosscutting concerns in spring AOP?

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

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

54. Connection Points

A connection point represents a location for an application where we can insert an AOP slice, which is actually where the application executes spring aop.

55. Notice

A notification is an action to be made before or after the execution of a method, and is actually a code snippet that is triggered by the SPRINGAOP framework when the program Executes.

There are five types of notifications that can be applied to spring facets:

    • before: pre-notification, called before a method is executed

    • after : A notification that is called after the method executes, regardless of whether the method executes successfully

    • after-returning: notification to be executed only when the method completes successfully

    • after-throwing: notification that is executed when a method throws an exception exits

    • around: notifications that are called before and after method execution

56. pointcuts

A pointcut is a connection point or set of connections that the notification will perform at those locations. Pointcuts can be indicated by an expression or by a matching method.

57. What is the introduction?

Introduction allows us to add new methods and properties to existing Classes.

58. what is a target object?

An object that is notified by one or more facets. It is usually a proxy object. Also refers to the notified (advised) Object.

59. What is an agent?

The agent is the object that is created after the target object is Notified. From the Client's perspective, the proxy object and the target object are the Same.

60. are there several different types of automatic proxies?

Beannameautoproxycreator

Defaultadvisorautoproxycreator

Metadata autoproxying

61. What is woven into. What are the different points of weaving applications?

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

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

62. Explanation of facets implementation based on XML schema

In this case, the facets are implemented by a regular class and an xml-based Configuration.

63. explanation of annotation-based facet implementations

In this case (based on the @aspectj implementation), the style of the facet declaration involved is consistent with the ordinary Java class with the Java5 Callout.

Spring's MVC

64. What is the spring MVC framework?

Spring is equipped with a full-featured MVC framework for building web Applications. Spring makes it easy to integrate with other MVC frameworks, such as the struts,spring MVC framework, which cleanly isolates business objects and control logic with control Inversion. It also allows binding of request parameters and business objects in a declarative manner.

Dispatcherservlet.

The Spring MVC Framework is designed around Dispatcherservlet to handle all HTTP requests and Responses.

Webapplicationcontext.

Webapplicationcontext inherits ApplicationContext and adds some unique features to the Web application that are different from the general ApplicationContext because it handles the subject and finds the associated Servlet.

67. What is the controller of the spring MVC framework?

The controller provides an access to the Application's behavior, which is typically implemented through the service Interface. The controller parses the user input and translates it into a model that is presented to the user by the VIEW. Spring implements a control layer in a very abstract way, allowing the user to create Multiple-purpose controllers.

@Controller annotations

This note indicates that the class plays the role of the controller, and spring does not require you to inherit any other controller base classes or reference servlet Apis.

@RequestMapping annotations

This annotation is used to map a URL to a class or to a particular party-handling method.

69 Classic Spring Noodles Questions and Answers

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.