Java Interview Framework chapter (IX)

Source: Internet
Author: User

Spring is undoubtedly the hottest frame in Java, with a wide range of uses, almost every company interview involves spring and database, you can not be familiar with struts, but must not show that spring does not understand. the Nineth article wins the interview to introduce the spring regular test center. The weight of the database will be in the back of five to introduce common problem type

Bayi, 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.

What is the usual implementation of the 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.

83, what is Spring's dependency injection ? What are the methods for 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.

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.

84, 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.

85, explains 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.

86, explains the Bean's life cycle in the spring framework.

The 1,spring container reads the bean definition from the XML file and instantiates the bean.

2,spring fills all properties according to the Bean's definition.

3, if the bean implements the Beannameaware interface, Spring passes the Bean's ID to the Setbeanname method.

4, if the Bean implements the Beanfactoryaware interface, spring passes beanfactory to the Setbeanfactory method.

5, if any beanpostprocessors,spring associated with the bean are called within the Postprocesserbeforeinitialization () method.

6, if the bean implementation Intializingbean, call its Afterpropertyset method, if the bean declares the initialization method, call this initialization method.

7, if there are beanpostprocessors and bean associations, the Postprocessafterinitialization () method of these beans will be called.

8, if the bean implements Disposablebean, it will call the Destroy () method.

87, how to 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.

88, explain the different ways of automatic assembly.

There are five automated assembly methods that instruct the spring container 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 benefits of transaction management for the 89,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.

90. 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.

Transfer from http://www.cnblogs.com/peke/p/7887619.html

Java Interview Framework chapter (IX)

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.