Java Interview--spring face test

Source: Internet
Author: User
Tags aop error handling
1. What is Spring?

Spring is an open source development framework for Java enterprise-class applications. Spring is primarily used to develop Java applications, but some of the extensions are for Web applications that build an EE platform. The Spring framework goal is to simplify Java enterprise application development and promote good programming practices through a POJO programming model. 2. What are the benefits of using the spring framework?

Light Weight: Spring is lightweight and the basic version is about 2MB.
Control reversal: Spring implements loose coupling through control inversion, and objects give them dependencies, rather than creating or locating dependent objects.
facet-oriented programming (AOP): Spring supports facet-oriented programming and separates application business logic from system services.
Container: Spring contains and manages the lifecycle and configuration of objects in the application.
MVC framework: The Spring web framework is a well-designed framework that is a good alternative to the web framework.
Transaction Management: Spring provides an ongoing transaction management interface that can be extended to the local transaction 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 consistent unchecked exceptions. 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) modules.

This is the basic spring module, which provides the basic functionality of the spring framework, and Beanfactory is the core of any spring based application. The spring framework is built on top of this module, which makes spring a container. 5. Beanfactory–beanfactory implementation examples.

The Bean factory is an implementation of the factory pattern, providing a control reversal function that separates the configuration and dependencies of applications from the real 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 according to the definition in the XML file. The container reads the configuration metadata from an XML file and uses it to create a fully configured system or application. 7. Interpreting AOP Modules

AOP modules are used to send our spring applications to do aspect-oriented development, and many of the 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. Explain JDBC abstraction and DAO modules.

By using JDBC Abstraction and DAO modules to ensure the simplicity of database code and to avoid problems caused by database resource error shutdown, it provides a unified exception access layer on top of the error messages of various databases. It also leverages Spring's AOP module to provide transaction management services to objects in spring applications. 9. Interpreting 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 supports our use of an object/relational mapping (ORM) tool over direct JDBC. Spring's transaction management also supports all of the above ORM frameworks and JDBC. 10. Explain the Web module.

The Spring Web module is built on the basis of the application context module, providing a framework for Web applications. This module also includes support for a variety of web-oriented tasks, such as transparently handling multiple file upload requests and the binding of application-level request parameters to your business object. 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 invoke each other. 13. What is the Spring IOC container.

The Spring IOC is responsible for creating objects, managing objects (through Dependency injection (DI), assembling objects, configuring objects, and managing the entire lifecycle of these objects. What is the merit 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 a single and Jndi lookup mechanism. The minimum cost and minimal intrusion make the loose coupling possible. 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 beans definition from an XML file, and the full path name of the XML Bean configuration file must be supplied to its constructor.
Classpathxmlapplicationcontext: This container also loads the beans definition from an XML file, where you need to set the classpath correctly because the container will find the bean configuration in classpath.
Webxmlapplicationcontext: This container loads an XML file that defines all the beans for a Web application. What is the difference between Bean factory and application contexts?

Application contexts provides a way to handle text messages, a common practice is to load file resources (such as mirrors), which can publish events to beans registered as listeners. In addition, operations performed on objects within a container or container that have to be handled programmatically by the Bean factory can be handled declaratively in application contexts. The application contexts implements the Messagesource interface, which provides a way to obtain localized messages in a pluggable manner. 17. What a spring application looks like.

An interface that defines a number of functions.
This implementation includes attributes, its setter, getter methods, and functions.
Spring AOP.
The 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, and you just need to describe how it was created. You don't assemble your components and services directly in your code, but to describe which components are required in the configuration file, a container (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 through a container that triggers a class constructor, which has a series of arguments, each representing a dependency on another class.
Setter Method Injection: Setter method Injection is when the container invokes the bean's setter method by calling the parameterless constructor or the parameterless static factory method to instantiate the bean, that is, the setter based dependency injection is implemented. 20. Which dependency injection method do you recommend to use, constructor injection, or setter method injection.

You can use both forms of dependency, constructor injection and setter method injection. The best solution is to enforce dependency with constructor parameters, and the setter method implements optional dependencies.

Spring Beans 21. What is Spring Beans?

Spring Beans is the Java object that forms the backbone of the spring application. They are initialized, assembled, and managed by the spring IOC container. These beans are created through the metadata configured in the container. For example, defined in the form of an XML file.

The beans of the Spring framework definition is a single piece of beans. There is an attribute "singleton" in the bean tag, which is a prototype bean if it is assigned to True,bean as a single piece. The default is true, so all beans in the spring frame are by default a single piece. 22. What a Spring Bean definition contains.

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?

There are three important ways to provide configuration metadata to the spring container.

XML configuration file.

Annotations based configuration.

Java-based configuration. 24. How do you define the scope of a class?

When defining one in spring, we can also declare a scope for this bean. It can be defined by the scope attribute 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 this bean's scope attribute must be set to Singleton. 25. Explain the scope of several beans that spring supports.

The spring framework supports the scope of the following five types of beans:

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 valid only under the Web-based spring applicationcontext scenario.
Session : in an HTTP session, a bean definition corresponds to an instance. This scope is only valid under the Web-based spring applicationcontext scenario.
global-session: in a global HTTP session, a bean definition corresponds to an instance. This scope is only valid under the Web-based spring applicationcontext scenario.
The default spring bean scope is singleton. is the single case bean in the Spring Framework thread-safe ?

No, the single case Bean in the spring framework is not thread-safe. 27. Explain the life cycle of beans in the spring framework.

The spring container reads the bean's definition from the XML file and instantiates the bean.
Spring populates all of the properties with 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 there are any beanpostprocessors,spring associated with the bean, they are called within the Postprocesserbeforeinitialization () method.
If the bean implements Intializingbean, call its Afterpropertyset method, and call this initialization method if the bean declares an initialization method.
If there are beanpostprocessors and bean associations, the Postprocessafterinitialization () method of these beans will be invoked.
If the bean implements Disposablebean, it calls the Destroy () method. 28. What are the important bean lifecycle methods? Can you overload them?

There are two important bean lifecycle methods, the first of which is setup, which is invoked when the container loads the bean. The second method is to teardown it to be invoked when the container unloads the class.

The bean label 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, elements can be used within or elements, and internal beans are usually anonymous. Their scope is generally prototype. 30. How to inject a Java collection into spring.

Spring provides configuration elements for the following collections:

Type is used to inject a column of values, allowing for the same value.
The type is used to inject a set of values, and the same values are not allowed.
Type is used to inject a set of key-value pairs, and both the key and the value can be of any type.
Type is used to inject a set of key-value pairs, and both keys and values can only be of type string.
31. What is a bean assembly?

Assembly, or bean assembly, is to assemble the beans together in a spring container, provided that the container needs to know the bean dependencies and how to assemble them together through dependency injection. 32. What is the automatic assembly of beans?

The Spring container is able to automatically assemble mutually cooperative beans, which means that the container is not required and configured to automate the collaboration between beans through the bean factory. 33. Explain the different ways of automatic assembly.

There are five kinds of automated assembly methods that can be used to guide the spring container in Automatic assembly mode for dependency injection.

No: The default approach is to assemble without automatic assembly by explicitly setting the Ref property.
byname: automatically assembled with parameter names, the spring container found in the configuration file that the bean's Autowire attribute was set to ByName, after which the container attempted to match, assemble, and Bean with the same name as the Bean's properties.
Bytype: automatically assembled by parameter type, the spring container found in the configuration file that the bean's Autowire attribute was set to Bytype, after which the container attempted to match, assemble, and have the same type of bean as the bean's properties. If more than one bean meets the criteria, an error is thrown.
constructor: This approach is similar to Bytype, but to provide constructor parameters, an exception is thrown if there is no determined constructor parameter type with parameters.
AutoDetect: first try to use constructor from the dynamic assembly, and if it is not working, use the Bytype method. 34. What are the limitations of automatic assembly?

The limitations of automatic assembly are:

rewrite: You still need and configure to define dependencies, which means always rewriting automatic assembly.
basic Data type: You cannot automatically assemble simple attributes, such as basic data types, string strings, and classes.
Fuzzy Characteristics: Automatic assembly is not as accurate as explicit assembly, it is recommended to use explicit assembly if possible. 35. You can inject a null and an empty string into spring.

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

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

Take the @configuration annotation as an example, it is used to mark the definition of a class as a bean, which is used by the spring IOC container. Another example is the @bean annotation, which indicates that this method will return an object and register it as a bean into the spring application context. 37. What is the annotation based container configuration?

In contrast to an XML file, the annotation-type configuration relies on assembling the component through bytecode metadata, rather than the statement of the angle bracket.

Instead of using XML to express the assembly relationship of the Bean, the developer configures the direct component class by using annotations on the corresponding class, method, or attribute. 38. How to open the annotation assembly.

The annotation assembly is not enabled by default, and in order to use the annotation assembly, we must configure the element in the spring configuration file. @Required Notes

This annotation indicates that the bean's properties must be set at the time of configuration, with an explicit property value defined by a bean or by automatic assembly, the container throws beaninitializationexception if the Bean property of the @required annotation is not set. @Autowired Notes

@Autowired annotations provide finer granularity of control, including where and how to automate assembly. It is used in the same way as @required, which modifies a setter method, constructor, property, or a PN method with arbitrary names and/or multiple parameters. @Qualifier Notes

When there are multiple beans of the same type and only one needs to be assembled automatically, use the @qualifier annotation with the @autowire annotation to eliminate this confusion and specify the exact bean to assemble.

Spring data Access 42. How to use JDBC more effectively in the spring framework?

With the SPRINGJDBC framework, the cost of resource management and error handling can be mitigated. So developers simply write statements and queries from data access data, and JDBC can be used more effectively with the help of the template classes provided by the spring framework, called JdbcTemplate (see here for example) 43. JdbcTemplate

The JdbcTemplate class provides a number of convenient ways to solve such things as translating database data into basic data types or objects, executing written or callable database operations statements, and providing custom data error handling. Spring's support for DAO

Spring's support for data Access Objects (DAO) is designed to simplify it 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 capturing exceptions that are specific to each technique when coding. 45. How do I access hibernate using spring?

There are two ways of accessing Hibernate in spring:

Control reversal Hibernate template and Callback.
Inheritance Hibernatedaosupport provides an AOP interceptor. the ORM supported by spring

Spring supports the following ORM:

Hibernate
IBatis
JPA (Java persistence API)
TopLink
JDO (Java Data Objects)
OJB 47. How to combine spring and hibernate by Hibernatedaosupport.

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

Configure the Hibernate sessionfactory.
Inheritance Hibernatedaosupport implements a DAO.
Assembled in an AOP-supported transaction. transaction management types supported by spring

Spring supports two types of transaction management:

Programmatic Transaction Management: This means that you manage your business programmatically, giving you a great deal of flexibility, but difficult to maintain.
Declarative Transaction Management: This means that you can separate the business code from the transaction management, and you only need to use annotations and XML configuration to manage the transaction. What are the benefits of the spring framework's transaction management?

It provides a constant programming pattern for different transaction APIs, such as JTA,JDBC,HIBERNATE,JPA and JDO.
It provides a simple set of APIs for programmatic transaction management rather than some complex transaction APIs such as
It supports declarative transaction management.
It integrates well with spring's various data access abstraction layers. 50. You are more inclined to use that type of transaction management.

Most of the spring framework users choose declarative transaction management because it has minimal impact on the application code and therefore more in line with the idea of a lightweight container without 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 facet-oriented programming (AOP) 51. Interpreting AOP

Facet-oriented programming, or AOP, is a programming technique that allows programs to modularize horizontal cutting concerns, or crosscutting typical division of responsibilities, such as logging and transaction management. Aspect Slice

The AOP core is the slice, 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, the slice is implemented by a class with a @aspect annotation. 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 the entire application will use and affect the entire application, such as logging, security, and data transfer, and the functionality required for almost every module that is applied. So these are crosscutting concerns. 54. Connection Point

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

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

The spring slice can apply five types of notifications:

Before: A predecessor notification that is invoked before a method executes.
After: The notification that is invoked after the method executes, regardless of whether the method was executed successfully.
After-returning: A notification that executes only if the method completes successfully.
After-throwing: A notification that executes when a method throws an exception exit.
Around: A notification that is invoked before and after the method executes. 56. Tangent Points

Pointcuts are one or a set of connection points that the notification will execute in these locations. The pointcut can be indicated by an expression or a matching method. 57. What is Introduction?

The introduction allows us to add new methods and properties to existing classes. 58. What is the target object?

An object that is notified by one or more facets. It is usually a proxy object. Also refers to the object being notified (advised). 59. What is an agent?

An agent is an object that is created after notifying the target object. From the client's perspective, the proxy object is the same as the target object. 60. There are several different types of automatic agents.

Beannameautoproxycreator

Defaultadvisorautoproxycreator

Metadata autoproxying 61. What is woven into. What are the different points of the weaving application.

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

Weaving can be done at compile time, at load time, or at run time. 62. Explain the facet implementation based on the XML schema approach.

In this case, the slice is implemented by the General class and the xml-based configuration. 63. Interpreting the annotation based facet implementation

In this case (based on the @aspectj implementation), the cross section declaration style is the same as the normal Java class with Java5 annotations.

Spring's MVC 64. What is Spring's MVC framework.

Spring is equipped with a fully functional MVC framework for building Web applications. Spring can be easily integrated with other MVC frameworks, such as the struts,spring MVC framework, which uses control inversion to isolate business objects and control logic clearly. It also allows for the binding of request parameters and business objects in a declarative manner. Dispatcherservlet.

Spring's MVC framework is designed around Dispatcherservlet to handle all HTTP requests and responses. Webapplicationcontext.

Webapplicationcontext inherits ApplicationContext and adds some unique features that are necessary for Web applications, unlike general ApplicationContext, because it handles topics and finds the associated servlet. 67. What is the controller for the Spring MVC framework.

The controller provides an access to the application's behavior, which is typically implemented through the service interface. The controller resolves user input and converts it into a model that is presented to the user by the view. Spring implements a control layer in a very abstract way that allows users to create multiple-purpose controllers. notes @Controller.

This annotation shows that the class plays the role of the controller, and spring does not require you to inherit any other controller base classes or to reference the servlet API. @RequestMapping Annotations

The annotation is used to map a URL to a class or to a specific method of square processing.

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.