Java Interview questions: 69 Spring noodles Questions and Answers

Source: Internet
Author: User

Directory
    • Spring Overview
    • Dependency Injection
    • Spring Beans
    • Spring annotations
    • Spring data access
    • Spring aspect-oriented programming (AOP)
    • Spring MVC
Spring Overview 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 Spring consists 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 some functionality.
    • This implementation includes attributes, its setter, getter methods, and functions.
    • Spring AOP.
    • The XML configuration file for Spring.
    • A client program that uses 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. Explain 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 automatic assembly methods that can be used to instruct spring containers to use 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:: with automatic assembly of parameter types, the spring container discovers that the bean's Autowire property is set to Bytype in the configuration file, after which the container tries to match, assemble, and have the same type of bean 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.
. 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.
    • Inheriting hibernatedaosupport implements a DAO.
    • Assemble in an AOP-supported transaction.
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: A pre-notification that is called before a method executes.
    • After : A notification that is invoked after the method executes, regardless of whether the method execution succeeds.
    • after-returning: notification that is executed only when the method completes successfully.
    • after-throwing: The notification that is executed when the method throws an exception exits.
    • around: A notification that is called before and after the method executes.
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. Explain the facets implementation based on the XML Schema pattern.

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.

http://ifeve.com/spring-interview-questions-and-answers/

Java Interview questions: 69 Spring noodles Questions and Answers

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.