Important issues in the spring framework

Source: Internet
Author: User

This article summarizes some important questions about the spring framework that you might be asked during an interview or a written exam.

Directory

Spring Overview

Dependency Injection

Spring Beans

Spring annotations

Spring's Object access

Spring-oriented tangent programming

Spring MVC Framework

Spring Overview

1. What is Spring?

Spring is an open-source Java EE Development Framework. The core functionality of the spring framework can be applied in any Java application, but it is more extensible for Web applications on the Java EE platform. The goal of the spring framework is to simplify the development of Java EE applications by promoting good programming styles by using the Pojo-based programming model.

What are the advantages of 2.Spring?

    • Lightweight: Spring is absolutely lightweight in terms of size and transparency, and the base version of the spring framework is about 2MB.

    • inversion of Control (IOC): Spring uses the control inversion technique to achieve loose coupling. Dependencies are injected into the object instead of creating or looking for dependent objects.

    • facet-oriented programming (AOP): Spring supports aspect-oriented programming while separating the application's business logic from the services of the system.

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

    • MVC framework: The Spring web framework is a well-designed web MVC framework that replaces some web frameworks.

    • Transaction Management: Spring provides a unified transaction management interface for down-to-the-local business to Global Business (JAT).

    • Exception Handling: Spring provides a handy API for translating specific technology exceptions (thrown by JDBC, Hibernate, or JDO) into a consistent, unchecked exception.

What modules are available in the 3.Spring framework?

The basic modules of the spring framework are as follows:

    • 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. Explain the core container (application context) module

This is spring's basic module, which provides the basic functionality of the spring framework. Beanfactory is at the heart of all spring applications. The spring framework is built on top of this module, which makes spring a container as well.

5.beanfactory–beanfactory instances

Beanfactory is an implementation of the factory pattern that uses control inversion to separate the application's configuration and dependencies from the actual 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 beans based on what is defined in the XML file. The container reads the configuration metadata from the XML file and uses it to create a complete system or application.

7. Interpreting the AOP module

An AOP module is used to develop portions of a spring application that have a facet nature. Most of the services of this module are provided by AOP Aliance, which guarantees interoperability between the spring framework and other AOP frameworks. In addition, the module introduces metadata programming to spring.

8. Interpreting abstract JDBC and DAO modules

The use of abstract JDBC and DAO modules ensures that the database connection code is clean and simple, while avoiding problems caused by failure to shut down the database resources. It provides an important exception layer on top of the error messages of various database servers. It also leverages Spring's AOP modules to provide transaction management services for objects in spring applications.

9. Interpreting the object/relational mapping integration module

Spring supports the object-relational mapping tool by providing an ORM module on the basis of JDBC. This support allows spring to integrate with the mainstream ORM framework, including Hibernate, JDO, and Ibatis SQL Maps. Spring's transaction management can support one of these frameworks and JDBC at the same time.

10. Interpreting the Web module

The Spring Web module is built on the application context (application context) module and provides a context-sensitive environment for web-based applications. The module also supports several web-oriented tasks, such as transparent processing of multi-file upload requests and binding of request parameters to business objects.

11. Interpreting the Spring MVC module

Spring provides the MVC framework for building Web applications. Spring can easily be combined with other MVC frameworks, but spring's MVC is a better choice because it separates control logic and business objects completely with control inversion.

Configuration file for 12.Spring

Spring's configuration file is an XML file that contains class information and describes how these classes are configured and called each other.

What is the 13.Spring IOC container?

Spring IOC is responsible for creating objects, managing objects (through Dependency injection), consolidating objects, configuring objects, and managing the life cycle of these objects.

What are the advantages of 14.IOC?

IOC or dependency injection reduces the amount of code for the application. It makes it easy to test your application because you no longer need a singleton or Jndi lookup mechanism in unit tests. Simple implementations and fewer interference mechanisms allow loose coupling to be achieved. IOC containers support diligent singleton and deferred load services.

15. How is the application context implemented?

The filesystemxmlapplicationcontext container loads the definition of beans in the XML file. The full path to the XML bean configuration file must be passed to the constructor.

The Filesystemxmlapplicationcontext container also loads the definition of beans in the XML file. Note that you need to set classpath correctly because the container will see the bean's XML configuration file in Classpath.

Webxmlapplicationcontext: The container loads the XML files that define all the beans in the Web app.

What is the difference between 16.Bean factory and ApplicationContext?

ApplicationContext provides a way to address document information, a way to load file resources (slices), and they can send messages to the beans that listen to them. In addition, operations that are beans in containers or containers that must be handled programmatically by the Bean factory can be handled declaratively in the context of the application. The application context implements the Messagesource, which is used to get local messages, and the actual implementation is optional.

What does the 17.Spring application look like?

    • An interface that defines functionality
    • Implementations include attributes, setter and Getter methods, functions, etc.
    • Spring AOP
    • XML configuration file for spring
    • Client-side programming using this feature

Dependency Injection

What is dependency injection in 18.Spring?

Dependency injection, as a level of control inversion (IOC), can be interpreted in many ways. In this concept, you do not need to create objects but just describe how to create them. Instead of connecting components and services directly through code, you can use a configuration file to describe what services are required by which components. The IOC container is then responsible for convergence.

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

    • constructor Dependency Injection: constructor Dependency Injection is done when the container triggers the constructor, which has a series of parameters, each representing the injected object.

    • Setter Method Dependency Injection: first the container triggers an parameterless constructor or a parameterless static factory method instantiation object, and the container invokes the setter method in the bean to complete the setter method dependency injection.

20. What kind of dependency injection do you recommend? constructor Dependency injection or Setter method dependency injection?

You can use two ways of dependency injection at the same time, the best option is to use constructor parameters to implement forced dependency injection, using setter methods to implement optional dependencies.

Spring Beans

21. What is Spring Beans?

Spring Beans is the Java object that makes up the core of the spring application. These objects are instantiated, assembled, and managed by the spring IOC container. These objects are created through the metadata that is configured in the container, for example, by using the creation defined in the XML file.

The beans created in spring are all singleton beans. There is a property of "singleton" in the bean tag, and if set to true, the bean is singleton and if set to false, the bean is a prototype bean. The Singleton property is set to True by default. Therefore, all beans in the spring framework default to singleton beans.

What is defined in the 22.Spring bean?

All of the configuration metadata is defined in the Spring bean, which tells the container how to create it, what its life cycle is, and its dependencies.

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

There are three ways to provide metadata to the spring container:

    • XML configuration file

    • Annotation-based configuration

    • Java-based configuration

24. How do you define the scope of the bean?

When we create a bean in spring, we can declare its scope. You only need to define the bean as defined by the ' scope ' property. For example, when spring needs to produce a new bean instance every time, it should declare that the bean's scope property is prototype. If every time you want spring to return an instance, you should declare that the bean's scope property is singleton.

25. Describe the bean scopes supported in spring

The spring framework supports the following five different scopes:

    • Singleton: Only one bean instance exists in the spring IOC container, and the bean exists as a single instance.

    • prototype: A bean can define multiple instances.

    • Request: A new bean is created each time the HTTP request occurs. This scope applies only to the WEBAPPLICATIONCONTEXT environment.

    • session: An HTTP session defines a bean. This scope applies only to the WEBAPPLICATIONCONTEXT environment.

    • globalsession: The same global HTTP session defines a bean. The scope also applies only to webapplicationcontext environments.

The bean default Scope property is 'Singleton'.

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

No, the 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 definition of the bean in the XML file and instantiates the bean.

    • Spring sets the property value 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 the beanfactory to the Setbeanfactory () method.

    • If any bean beanpostprocessors is associated with the bean, spring calls the Postprocessbeforeinitialization () method.

    • If the bean implements the Initializingbean interface, call the Afterpropertiesset method in the bean. If the bean has an initialization function declaration, the appropriate initialization method is called.

    • If any bean beanpostprocessors is associated with the bean, call the Postprocessafterinitialization () method.

    • If the bean implements Disposablebean, call the Destroy () method.

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

There are two important bean life-cycle methods. The first is the Setup method, which is called when the container loads the bean. The second is the Teardown method, which is called when the bean is removed from the container.

The bean tag has two important properties (Init-method and Destroy-method) that you can use to define your own initialization methods and destructor methods. Spring also has the corresponding annotations: @PostConstruct and @PreDestroy.

29. What is spring's internal bean?

When a bean is used as a property of another bean, the bean can be declared as an internal bean. In XML-based configuration metadata, an internal bean can be defined by defining an element within or within an element. Internal beans are always anonymous and their scope is always prototype.

30. How do I inject a Java collection class into spring?

Spring provides several types of collection configuration elements:

    • The list element is used to inject a series of values that allow the same value.

    • The set element is used to inject values from some columns, and the same value is not allowed.

    • Map is used to inject a set of "key-value" pairs, the key, the value can be any type.

    • Props can also be used to inject a set of "key-value" pairs, where the key and value are string types.

31. What is bean wiring?

Wiring, or Bean Wiring, refers to the case where beans is combined in a spring container. When assembling a bean, the spring container needs to know what beans are needed and how to use dependency injection to bind them together.

32. What is bean automatic assembly?

The spring container can automatically configure the association between beans. This means that spring can automatically configure a relationship between a bean and other collaboration beans, by examining the contents of the Beanfactory without using and < property> elements.

33. Explain the various modes of automatic assembly?

Automatic assembly provides five different modes for the spring container to automatically assemble the dependency injection between the beans:

    • No: The default way is not to assemble automatically, but to assemble the bean by manually setting the Ref property.

    • byname: automatically assembled by the parameter name, the Spring container looks for properties of the beans, which are set to ByName in the XML configuration file. The container then attempts to match, assemble, and Bean with the same name as the Bean's property.

    • Bytype: automatic assembly with parameter data type, Spring container looks for properties of beans, these beans are set to Bytype in XML configuration file. The container then attempts to match and assemble the same bean as the Bean's property type. If more than one bean meets the criteria, an error is thrown.

    • constructor: This is similar to Bytype, but a parameter applied to the constructor. If there is not exactly one bean in beanfactory that has the same type as the constructor parameter, a serious error is thrown.

    • AutoDetect: If there is a default construction method, it is automatically assembled by means of construct, otherwise it is automatically assembled using the Bytype method.

34. What are the limitations of automatic assembly?

Automatic assembly has the following limitations:

    • rewrite: you still need to use the and < property> settings to indicate dependencies, which means always overriding automatic assembly.

    • native data type: You cannot automatically assemble simple properties, such as native types, strings, and classes.

    • Blur Characteristics: Automatic assembly is always not custom assembled precisely, so if possible use custom assembly.

35. Can you inject null or an empty string in spring?

It's perfectly possible.

Spring annotations

36. What is spring Java-based configuration? Give some examples of annotations

Java-based configuration allows you to use Java annotations for most of spring's configuration rather than through traditional XML file configurations.

As an example of annotated @configuration, it is used to mark classes, which are defined as beans and can be used by the spring IOC container. Another example is the @bean annotation, which indicates that the bean for the method definition is to be registered in the Spring application context.

37. What is annotation-based container configuration?

Another alternative to XML configuration is the annotation-based configuration, which assembles components over byte metadata rather than using angle brackets to declare them. Developers will be configured directly in the class, tagging related classes, methods, or field declarations with annotations instead of using XML to describe the connection relationships between beans.

38. How do I turn on annotation assembly?

Annotation assembly is not turned on by default in the Spring container. If you want to turn on annotation-based assembly, simply configure the element in the spring configuration file.

[Email protected] Annotations

@Required indicates that the Bean's properties must be set at configuration time and can be explicitly specified in the Bean's definition or through automatic assembly settings. If the Bean's property is not set, the Beaninitializationexception exception is thrown.

[Email protected] Annotations

@Autowired annotations provide finer control, including where automated assembly is done and how it is done. It can automatically assemble setter methods, constructors, attributes, or PN methods with arbitrary names and/or multiple parameters like @required.

@Qualifier annotations

When there are multiple beans of the same type and only one of them needs to be automatically assembled, use @qualifier annotations and @autowire annotations together to eliminate this confusion and to indicate which beans need to be assembled.

Spring data access

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

With the spring JDBC Framework, the cost of resource management and error handling can be mitigated. Developers only need to access data from the database through statements and queries statements. Using the template classes in the Spring framework makes it more efficient to use JDBC, the so-called JdbcTemplate (example).

43.JdbcTemplate

The JdbcTemplate class provides a number of ways to facilitate our interaction with a database. For example, it can convert data from a database into a native type or object, execute a written or callable database action statement, and provide a custom database error handling capability.

44.Spring support for DAO

Spring's support for data Access Objects (DAO) is designed to make it easy to work with data access technologies such as JDBC, Hibernate, and JDO. This allows us to easily switch between the different persistence layer technologies, while coding without worrying about throwing out specific technology exceptions.

45. How can I access hibernate by using spring?

There are two ways to access hibernate using spring:

    • Inversion control and callback methods using Hibernate template

    • Inherit the Hibernatedaosupport and request an AOP interceptor node

46.Spring-enabled ORM

Spring supports 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 using Spring's sessionfactory. The combined process is divided into the following three steps:

    • Configure Hibernate Sessionfactory

    • Inherit hibernatedaosupport implement a DAO

    • Using AOP to load transaction support

Types of transaction management supported by 48.Spring

Spring supports transaction management in the following two ways:

    • Programmatic Transaction Management: This means that you can manage transactions programmatically, which provides a lot of flexibility, but is difficult to maintain.

    • Declarative Transaction Management: This means that you can separate transaction management from business code. You only need to configure management transactions through annotations or XML.

What are the benefits of transaction management for 49.Spring frameworks?

    • It provides a unified programming model for different transactional APIs such as JTA, JDBC, Hibernate, JPA, and JDO.

    • It provides a simple API for programmatic transaction management rather than a series of complex transactional APIs such as JTA.

    • It supports declarative transaction management.

    • It can be well integrated with spring's various data access technologies.

50. What kind of transaction management do you recommend?

Many spring framework users choose declarative transaction management because this is less relevant to the application and therefore more consistent with the concept of lightweight containers. Declarative transaction management is better than programmatic transaction management, although it is weaker than programming transaction management in terms of flexibility, which allows you to control your business through code.

Spring aspect-oriented programming (AOP)

51. Interpreting AOP

Aspect-oriented programming, or AOP, allows programmers to modularize horizontal business logic, or define core parts of functionality, such as log management and transaction management.

52. Facets (Aspect)

The core of AOP is slicing, which encapsulates the common behavior of multiple classes into reusable modules. The module contains a set of APIs that provide cross-cutting functionality. For example, a log module is 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.

53. What is the difference between concern and cross-cutting concern in spring AOP?

Concern (Core Logic): Represents the behavior of a module in an application. Concern can be defined as the function we want to implement.

Cross-cutting concern (horizontal general logic): refers to the functionality that is used throughout the application, and it affects the entire application. For example, log Management (Logging), security management, and data interaction are all involved in each module of an application, so these belong to cross-cutting concern.

54. Connection points (join point)

A connection point represents a place in an application where an AOP slice is inserted. It is actually the place where the spring AOP framework performs actions in the application.

55. Notice (Advice)

A notification represents an action that needs to be performed before or after a method executes. It is actually some code that the Spring AOP framework triggers during program execution.

Spring facets can perform five types of notifications:

    • Before (pre-notification): a notification that is executed before a method.

    • After (final notification): a notification that is executed when a connection point exits (whether it is a normal return or an unexpected exit).

    • After-returning (post notification): A notification that is executed after a connection point is completed normally.

    • After-throwing (Exception notification): A notification that is executed when a method throws an exception exits.

    • Around (surround notification): a notification that fires before or after a method call.

56. Entry point (Pointcut)

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 or 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 called made informed (advised) objects.

59. What is an agent?

An agent is an object that is created after the notification is applied to the target object. 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:bean Name Auto Proxy Creator

    • Defaultadvisorautoproxycreator: Default notifier Auto Proxy Creator

    • Metadata autoproxying: Meta data Auto Proxy

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

Weaving is the process of creating a notification object by connecting facets to other application types or objects. Weaving can be done at compile, load, or run time.

62. Explanation of facets implementation based on XML schema

In this case, the facets are implemented by classes that are configured with XML files.

63. Explanation of facet implementations based on annotation mode (@aspectj based)

In this case (based on the @aspectj implementation), the corresponding class of the slice is declared using the Java 5 annotation.

Spring's MVC Framework

64. What is the Spring MVC framework?

Spring provides a full-featured MVC framework for building Web applications. The spring framework can be easily fused with other MVC frameworks (such as struts), which uses the inversion of control (IOC) to separate the controller logic from the business object. It also allows the request parameters to be bound declaratively on the business object.

65.DispatcherServlet

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

66.WebApplicationContext

Webapplicationcontext inherits the ApplicationContext and adds the functionality that some Web applications require. Unlike normal ApplicationContext, Webapplicationcontext can be used to handle theme styles, and it also knows how to find the appropriate servlet.

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

The controller provides access to the application's behavior, typically through the service interface. The controller parses the user's input and translates it into a model that is presented to the user by the view. Spring implements the controller in an extremely abstract way, which allows the user to create multiple types of controllers.

[Email protected] Annotation

@Controller annotations indicate that the class plays the role of the controller. Spring does not need to inherit any controller base classes or apply Servlet APIs.

[Email protected] Annotation

@RequestMapping annotations are used to map URLs to any class or to a specific processing method.

Important issues in the spring framework

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.