"Interview" "Spring FAQ" "06"

Source: Internet
Author: User
Tags aliases naming convention to domain

"Common Interview questions Summary folder >>>"

51. Can the Applicationcontext.xml in spring be changed to another name ?

Contextloaderlistener is a servletcontextlistener that is initialized when your web app starts. By default, it will find spring's configuration in the Web-inf/applicationcontext.xml file. You can change the location of the spring configuration file by defining a <context-param> element named "Contextconfiglocation". The scale is as follows:

<span style= "FONT-SIZE:14PX;" ><listener>    <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class>    <context-param>       <param-name>contextConfigLocation</param-name>       <param-value>/WEB-INF/xyz.xml</param-value>    </context-param></listener></ Span>

52. How to configure spring in the Web

It is easy to configure spring in the Java EE Web application. Just need to add spring Contextloaderlistener to your Web. xml file to be able to, the ratio is as follows:

<span style= "FONT-SIZE:14PX;" ><listener>    <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class></listener></span>


53. The roles and differences of Beanfactory and ApplicationContext in spring

Beanfactory is responsible for reading the bean configuration document, managing the bean's loading, instantiating, maintaining the dependency between the beans, and responsible for the bean's declaration cycle.

In addition to providing the functionality provided by the above Beanfactory, ApplicationContext provides a more complete framework function:

A. Internationalization support

B. Resource access Q: Resource rs = ctx. GetResource ("Classpath:config.properties"),

"File:c:/config.properties"

C. Event delivery: By implementing the Applicationcontextaware interface.

54. Frequently used methods of acquiring ApplicationContext

Filesystemxmlapplicationcontext:

Created from the file system or URL-specified XML configuration file, which is an array of configuration file names or file names

Classpathxmlapplicationcontext:

Created from the classpath XML configuration file to read the configuration file from the jar package

Webapplicationcontextutils:

To read a configuration file from the root folder of the Web app, you need to first configure it in XML. config listener or servlet to implement

<span style= "FONT-SIZE:14PX;" ><listener>    <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class></listener><servlet>    <servlet-name>context</servlet-name>    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>    < Load-on-startup>1</load-on-startup></servlet></span>

Either way, the default profile is Web-inf/applicationcontext.xml, or you can use Context-param to specify the configuration file

<span style= "FONT-SIZE:14PX;" ><context-param>    <param-name>contextConfigLocation</param-name>    <param-value >/WEB-INF/myApplicationContext.xml</param-value></context-param></span>

What is the difference between the jdbc inspring and the traditional jdbc ?

Spring's JDBC is a simple package above the original eco JDBC, which provides a few interfaces to save code. Regardless of the connection (Connection), regardless of the transaction, regardless of the exception, regardless of the shutdown (Con.close () ps.close). Only the callback classes provided by spring are required.

The traditional JDBC run steps are as follows:

A. Create a connection

B, create statement (SQL)

C, run the statement

D. Return result set (set to JavaBean medium processing)

E. Close connection Release Resources

Spring JDBC only needs to implement the B and D two steps, and the others are finished by spring. Both B and D are implemented by implementing the specified interface. Then passing the implementation class to spring is OK.

What is spring, and what is its characteristic ?

Spring is a lightweight control inversion (IoC) and facet-oriented (AOP) container framework.

Lightweight-Spring is lightweight in terms of both size and overhead. The full spring framework can be advertised in a jar file of just a few megabytes in size.

and the processing overhead required by spring is negligible.

In addition, spring is non-intrusive (intrusive allows user code to generate dependency on the framework, which cannot be used outside the framework and is not conducive to the reuse of code.) But intrusive can make the user and the frame better union. Easier and fuller use of the functionality provided by the framework. Non-intrusive code is not overly dependent and can be easily migrated to other places. But the way you interact with user code can be more complex. ): Typically, objects in spring applications do not depend on a particular class of spring.

Control reversal--spring facilitates loose coupling through a technique called inversion of Control (IoC). When IOC is applied, other objects that an object relies on are passed in passively, rather than the object itself creating or locating dependent objects. You can think of the IOC as opposed to Jndi--not the object looking for dependencies from the container. Instead, the container actively passes the dependency to it when the object is initialized with unequal requests.

Facets-oriented--spring provides rich support for aspect-oriented programming. Consent to the development of cohesion by separating the business logic of the application with system-level services such as auditing (auditing) and transaction (transacntion) management.

The Application object simply implements what they should do-the business logic--that's all. They are not responsible (or even conscious) for other system-level concerns. such as log or transaction support.

The container--spring includes and manages the configuration and life cycle of the Application object. In this sense it is a container in which you can configure how each of your beans is created-based on a configurable prototype (prototype), your bean can create a separate instance or generate a new instance each time it is needed-and how they relate to each other.

However, spring should not be confused with traditional heavyweight ejb containers, which are often bulky and cumbersome and difficult to use.

Framework--spring is able to configure and assemble simple components into complex applications. In spring, application objects are combined declaratively, typically in an XML file. Spring also provides a lot of basic functionality (transaction management, persistence framework integration, and so on). The development of application logic is left to you.

56. What are the advantages of using spring?

Spring can effectively organize your middle-tier objects.

Spring can eliminate excessive use of singleton on many project.

Spring can eliminate the need to customize files with a wide variety of formatting properties, and can be configured in a consistent way throughout the application and project. With spring, you can very easily see the JavaBean property of a class. The use of dependency injection helps to complete this simplification.

Spring can promote good programming habits through interfaces rather than classes, reducing the cost of programming to almost zero.

Spring is designed to allow applications created with it to rely as little as possible on his APIs. Most business objects in a spring application are not dependent on spring.

• Applications built using spring are easy to unit test.

Spring enables the use of EJBS to be an implementation choice rather than a necessary choice for the application architecture. You can choose to use POJOs or local EJBS to implement the business interface. Does not affect the calling code.

L Spring helps you solve many problems without using EJBS.

Spring provides an alternative to EJBS that are suitable for many Web applications. For example, spring can use AOP to provide declarative transactions without using an EJB container, assuming that you only have to deal with a single database, not even the JTA implementation.

Spring provides a consistent framework for data access, whether using JDBC or O/R mapping products such as hibernate.

To sum up, spring has the following advantages such as:

L Low-intrusive design with very low code contamination

L independent of various application server, can truly implement write Once, Run anywhere's promise

L spring's di mechanism reduces the complexity of business object substitution

Spring does not rely entirely on spring, and developers are free to choose part or all of the spring framework.

57, talk about the spring framework of the main components?

The spring framework consists of seven modules, 7 modules (or components) that can exist separately. can also be used in conjunction with one or more of the other modules, the main features are as follows:

L Spring core container (CORE): Provides the basic functionality of the spring framework. The main component of the core container is beanfactory. She is the realization of the factory pattern. The beanfactory uses the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code program.

L Spring AOP: With the configuration management feature, the Spring AOP module directly integrates the aspect-oriented programming functionality into the spring framework, so it is very easy to enable any object managed by the spring framework to support AOP. The spring AOP module provides transaction management services for objects in spring-based applications.

By using spring AOP, you can integrate declarative transaction management into your application without relying on EJB components.

The Spring orm:spring Framework integrates several ORM frameworks to provide an ORM object-relational tool that includes JDO, Hibernate, MyBatis, and TopLink.

All of these follow spring's common transaction and DAO exception layer structure.

The Spring dao:jdbc DAO Abstraction Layer provides a meaningful hierarchy of exceptions that can be used to manage exception handling and exception error messages thrown by different data vendors. The exception hierarchy simplifies error handling and greatly reduces the number of exception codes that need to be written (for example,. Open and relational connections). Spring DAO's JDBC-oriented exception conforms to the common DAO exception layer structure.

The Spring web:web context module is built on the context module to provide a context-based service for WEB service applications. So the spring framework supports Jakarta struts integration.

The Web module also simplifies the process of working with multipart requests and binding request parameters to domain objects.

L Spring Context: The spring context is a configuration file that provides contextual information to the spring framework.

The spring context contains enterprise services. such as Jndi, EJB, e-mail, internationalization check and dispatch function.

L Spring Mvc:spring's MVC framework is a full-featured MVC implementation of building Web applications.

Through the policy interface. The MVC framework becomes highly configurable, and MVC accommodates a large number of view technologies. Includes JSP, Velocity, Tiles, Itext, and Pol.

57, ApplicationContext interface Get Bean method Simple introduction:

L Object Getbean (String name) returns a bean by name. The client needs to type-convert itself.

L T Getbean (String name,class<t> requiredtype) returns a bean,client by name and specified type without having to do the type conversion itself, assuming the type conversion fails. The container throws an exception.

L T Getbean (class<t> requiredtype) returns a bean,client based on the specified type without having to do the type conversion itself. Assuming that there are no or more than one bean present, the container throws an exception.

L map<string, T>getbeansoftype (class<t> type) returns a map with a key value of name and value for the Bean object based on the specified type, assuming that no bean object exists to return an empty map.

58. Name of Bean

Each bean can have one or more IDs (or identifiers or names), where we call the first ID "identifier". The remaining IDs are called "aliases", which must be unique within the IOC container. There are several ways to specify an ID for a bean.

L do not specify an ID, only the required fully qualified class name is configured, and the IOC container generates an identity for it, and the client must obtain the bean through the interface "T Getbean (Class<t>requiredtype)".

<span style= "FONT-SIZE:14PX;" ><bean class= "Cn.javass.spring.chapter2.helloworld.HelloImpl"/></span>

L Specify the ID. Must be unique within the IOC container;

<span style= "FONT-SIZE:14PX;" ><bean id= "Bean" class= "Cn.javass.spring.chapter2.helloworld.HelloImpl"/></span>

L Specify name so that name is "identifier" and must be unique within the IOC container.

<span style= "FONT-SIZE:14PX;" ><bean name= "Bean" class= "Cn.javass.spring.chapter2.helloworld.HelloImpl"/></span>
l Specify ID and Name,id are identifiers. Name is an alias and must be unique in the IOC container .

<span style= "FONT-SIZE:14PX;" ><bean id= "Bean1" name= "alias1" class= "Cn.javass.spring.chapter2.helloworld.HelloImpl"/><!-- If the ID and name are the same, the IOC container can detect. and eliminate conflicts--><bean id= "Bean3" name= "bean3" class= "Cn.javass.spring.chapter2.helloworld.HelloImpl"/></span >
L Specify more than one name, multiple names with ",", ";", "" cut, the first is used as an identifier, the other (ALIAS1, ALIAS2, ALIAS3) is an alias, and all identifiers must be unique within the IOC container;

<bean name= "Bean1;alias11,alias12;alias13 alias14" class= "Cn.javass.spring.chapter2.helloworld.HelloImpl"/ >

<span style= "FONT-SIZE:14PX;" ><!--When the ID is specified. Name specifies the identifier for all aliases--><bean id= "bean2" name= "Alias21;alias22" class= " Cn.javass.spring.chapter2.helloworld.HelloImpl "/></span>

59. Bean Naming Convention

The name of the bean follows the XML naming convention, but it is best to conform to the Java naming convention, which consists of "letters, numbers, underscores", and should form a good naming habit, such as "hump", that is, starting with the first letter beginning with the beginning of the second word, so that readability can be added.

60. How does the Spring IOC container instantiate beans?

1. Instantiate the bean using the constructor:

Use the empty constructor for the definition, using this method. Class property specified must have null constructor

<span style= "FONT-SIZE:14PX;" ><bean name= "Bean1" class= "CN.JAVASS.SPRING.CHAPTER2.HELLOIMPL2"/></span>
defined using the parameter constructor, which allows you to specify the constructor parameters using the < constructor-arg> tag. Where index represents a position, value represents a constant value, and a reference can be specified. The specified reference also uses ref to refer to a bean definition.

<span style= "FONT-SIZE:14PX;" ><bean name= "bean2" class= "CN.JAVASS.SPRING.CHAPTER2.HELLOIMPL2" ><!--specifying constructor parameters-->< Constructor-arg index= "0" value= "Hello spring!" /></bean></span>
2, using the static factory method to instantiate the bean, in addition to specifying the required class attribute, also specify the Factory-method property to specify the method of instantiating the bean, and using the static factory method also agreed to specify the method parameters, spring The IOC container will call the method specified by this property to get the Bean,

3. Use the instance factory method to instantiate the bean, in such a way that the class attribute cannot be specified. You must use the Factory-bean property to specify the factory bean. The Factory-method property specifies the method that instantiates the bean and uses the instance factory method to agree to specify the method parameters. The same way you use constructors

"Interview" "Spring FAQ" "06"

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.