"Interview" "Spring FAQ" "06"

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

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". Examples are 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 very simple to configure spring in the Java EE Web application, just add the spring Contextloaderlistener to your XML file, as shown in the following example:

<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

The 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: Resource rs = ctx. GetResource ("Classpath:config.properties"),

"File:c:/config.properties"

C. Event delivery: By implementing the Applicationcontextaware interface.

54. Commonly used methods of obtaining ApplicationContext

Filesystemxmlapplicationcontext:

Created from a file system or URL-specified XML configuration file, with an array of configuration file names or file names

Classpathxmlapplicationcontext:

Created from the classpath XML configuration file, you can read the configuration file from the jar package

Webapplicationcontextutils:

Reading a configuration file from the Web application's root directory requires a configuration in XML. config, which can be configured with a 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, providing some interface to save code, regardless of the connection (Connection), regardless of the transaction, regardless of the exception, regardless of the shutdown (Con.close () ps.close). You only need to implement the callback class provided by spring. The traditional JDBC execution process is as follows:

A. Create a connection

B, create statement (SQL)

C, execute 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, the others are done for you by spring, and both B and D are implemented by implementing the specified interface, and 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, both in terms of size and overhead. The full spring framework can be published in a jar file of only 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 it easier for users to combine with the framework and make more 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, but the container actively passing the dependency to it when the object is initialized without waiting for the object to be requested.

Facet-oriented--spring provides rich support for aspect-oriented programming, allowing for the development of cohesion through the separation of application business logic with system-level services such as auditing (auditing) and transaction (transacntion) management. The Application object only implements what they should do-complete 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 contains and manages the configuration and lifecycle of the Application object, in this sense it is a container, and 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 can be used 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 number of basic functions (transaction management, persistence framework integration, etc.), leaving the development of application logic to you.

56. What are the benefits of using spring?

Spring can effectively organize your middle-tier objects.

Spring can eliminate excessive use of singleton in many projects.

Spring can eliminate the need to customize files using a wide variety of formats, which can be configured in a consistent way throughout the application and engineering. With spring, you can easily see the JavaBean property of a class. The use of dependency injection helps to accomplish this simplification.

Spring can promote good programming habits through interfaces rather than classes, reducing programming costs 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 can make EJB usage an implementation choice, not an inevitable choice for application architectures. You can choose to use POJOs or local EJBS to implement the business interface without affecting 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, if you only need to deal with a single database, and you don't even need a 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:

L Low-intrusive design with very low code contamination

L independent of various application servers, can truly implement write Once, Run anywhere commitment

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

Spring does not rely entirely on spring, and developers are free to select 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 individually or in conjunction with one or more of the other modules, with the following main functions:

L Spring core container (CORE): Provides the basic functionality of the spring framework. The main component of the core container is Beanfactory, which is the implementation of the factory model. 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's 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 ORM object-relational tools, including JDO, Hibernate, MyBatis, and TopLink. All of these conform to 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 includes enterprise services such as Jndi, EJB, e-mail, and internationalization check and dispatch capabilities.

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, including JSP, Velocity, Tiles, Itext, and Pol.

57, ApplicationContext Interface Get Bean method Introduction:

L Object Getbean (String name) returns a bean by name, and the client needs to type-convert itself;

L T Getbean (String name,class<t> requiredtype) returns a bean based on the name and the specified type, and the client does not need to make the type conversion itself, and if the type conversion fails, the container throws an exception;

L T Getbean (class<t> requiredtype) returns a bean based on the specified type, and the client does not have to make the type conversion itself, and if there are no or more than one bean, 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, or an empty map if no bean object exists.

58. Name of Bean

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

L do not specify an ID, only the fully qualified class name must be configured, 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 ID, must be unique in 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, and name is alias, must be unique in 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 the conflict--><bean id= "bean3" name= "Bean3" class= " Cn.javass.spring.chapter2.helloworld.HelloImpl "/></span>
L Specify more than one name, multiple names with ",", ";", "" split, 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, the identifier specified by name is 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", which starts with the first letter, starting with the beginning of the second word, which increases readability.

60. How does the Spring IOC container instantiate beans?

1. Instantiate the bean using the constructor:

Defined using an empty constructor, the class property specified by this method must have a null constructor

<span style= "FONT-SIZE:14PX;" ><bean name= "Bean1" class= "CN.JAVASS.SPRING.CHAPTER2.HELLOIMPL2"/></span>
defined using the parameterized constructor, you can use the < constructor-arg> tag to specify the constructor parameter value, where index is the position, value represents a constant value, or a reference can be specified. Specifies that references use ref to refer to another 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 allows to specify the method parameters, spring The IOC container will call the method specified by this property to get the Bean,

3, using the instance factory method to instantiate the bean, you can not specify the class property in this way, you must use the Factory-bean property to specify the factory Bean,factory-method property to specify the method to instantiate the bean, and using instance factory methods allows you to specify method parameters in the same way that 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.