There are three core components in the Spring framework: Core, Context, and Beans. One of the core components is beans, and spring provides the core function of Bean Factory.
One of the core issues that spring solves is the transition of dependency between objects into a configuration file, which is spring's dependency injection mechanism. This injection mechanism is managed in the IOC container.
The Bean component is under Spring's Org.springframework.beans package. This package mainly solves the following functions: Bean definition, bean creation, and parsing of the bean. The only thing that matters to spring users is the creation of beans, and the other two are done by the spring internal mechanism. The Spring Bean was created using a typical factory model, and his top-level interface was beanfactory.
Beanfactory has three subcategories: Listablebeanfactory, Hierarchicalbeanfactory, and Autowirecapablebeanfactory. But from this we can find that the final default implementation class is Defaultlistablebeanfactory, and he implements all the interfaces. So why define so many levels of interfaces? Refer to the source code and description of these interfaces found that each interface has his use of the occasion, it is mainly to distinguish between Spring in the process of the object during the transfer and conversion process, the object of the data access restrictions. For example, the Listablebeanfactory interface indicates that these beans are a list, and hierarchicalbeanfactory indicates that the beans are inherited, that is, each bean may have a parent bean. The Autowirecapablebeanfactory interface defines the automatic assembly rules for the Bean. These four interfaces collectively define the collection of beans, the relationships between beans, and the bean behavior.
The Bean definition is a complete description of all the information in the <bean/> node that you define in the Spring configuration file, including the various child nodes. When spring successfully parses a <bean/> node that you define, within spring, he is transformed into a Beandefinition object. All subsequent operations are done on this object. The parsing process of the Bean is very complex and the function is very thin, because there are many places that need to be expanded, and there must be sufficient flexibility to cope with possible changes. The parsing of the Bean is mainly the parsing of the Spring configuration file.
What are the seven modules of the spring framework, and what are the main functions of each module?
Seven modules, as follows:
1. The Spring Core:core package is the most fundamental part of the framework, providing IOC and dependency injection characteristics. The basic concept here is beanfactory, which provides a classic implementation of the factory pattern to eliminate the need for procedural singleton patterns and really allows you to isolate dependencies and configurations from the program logic.
2.Spring Context: The context package, built on the core package, provides a framework for object access, some like the Jndi registrar. The context wrapper package is characterized by the beans package and adds support for internationalization (i18n), event propagation, the way resources are loaded, and the transparent creation of the context, such as through a servlet container.
3. Spring Dao:dao (Data Access Object) provides a JDBC abstraction layer that eliminates lengthy JDBC encoding and resolves database vendor-specific error codes. Also, the JDBC package provides a better declarative transaction management approach than programmatic, not only for specific interfaces, but also for all pojos (plain old Java objects).
The 4.Spring Orm:orm package provides an integrated layer of commonly used "object/relationship" mapping APIs. These include JPA, JDO, Hibernate, and IBatis. With the ORM package, you can mix the "object/relationship" mappings with all of the spring-provided features, such as the simple declarative transaction management mentioned earlier.
The AOP package for 5.Spring aop:spring provides an aspect-oriented programming implementation that conforms to the AOP Alliance specification, allowing you to define, for example, method interceptors (method-interceptors) and Pointcuts (pointcuts), logically, Thereby weakening the functional coupling of the code, clearly separated away. Also, with the Source-level metadata feature, you can incorporate various behavioral information into your code.
Web Packages in 6.Spring web:spring provide the underlying integration features for web development, such as multi-party file uploads, and the use of servlet listeners for IOC container initialization and web-based applicationcontext. When using spring with webwork or struts, this package enables spring to be combined with other frameworks.
The MVC wrapper package in 7.Spring Web Mvc:spring provides a model-view-controller (MVC) Implementation of the Web application. The spring MVC Framework does not simply provide a traditional implementation, it provides a clear separation model between the domain model code and the Web form. Also, other features of the spring framework can be used.
- What are the three core ideas in the spring framework
DI (Dependency injection), IOC (inversion of Control), AOP (plane-oriented programming)
- The IOC concept and how the IOC operates in the spring container.
Ioc:inversion of control, inversion of controls. In Java development, IOC means that the class you have designed is given to the system to control, not within your class, which is called control inversion, that is, the instance of the called class is created by the original invocation class control, and the destruction is now transformed into a container managed by spring.
- How the Spring container manages the bean's life cycle (such as the Bean's initialization method, the Bean's destruction method)
Created: <bean name= "" class= "" Extra Properties >
Initialize: Configure Init-method/Implementation Interface Initializingbean
Call: Context.getbean (), making a call to the method
Destroy: Configure destroy-method/to implement Disposablebean interface
- The concept of Di and the injection in spring framework are several ways. What problems must be noticed when using constructs to inject objects, and the sequence of sequential processes that are executed when the value injection and the construction injection exist simultaneously
Injection method:
Interface Injection
attribute injection [Set/get of attributes]
Construction injection [construction method injection]
When using a constructor dependency injection, spring guarantees that all objects dependent on all objects are instantiated before instantiating the object. When using the Set method dependency injection, spring instantiates the object first and then instantiates all dependent objects.
When the setpoint injection is present at the same time as the construction injection, the set injection is performed before the construction injection.
- When using Di injection, the property represents what it means, if the property refers to another bean, how to inject, if the reference is a string, how to set.
When using Di injection, the property represents the attribute of the injected class, and if the other bean is applied with a ref attribute to indicate the name of the referenced bean, if it is a reference string, use the Value property. Such as:
<property name= "Userdao" ref= "name of the referenced bean"/>
<property name= "username" value = "string"/>
- There are several ways to get a connection pool in the spring framework. When Jndi and DBCP exist at the same time, the problem will not occur, if not at the same time, please explain why
Four different ways, as follows:
1:DBCP Data source
The DBCP class package in <SPRING_HOME>/LIB/JAKARTA-COMMONS/COMMONS-DBCP.JAR,DBCP is a database connection pool that relies on the Jakarta Commons-pool object pooling mechanism. Therefore, you must also include <spring_home>/lib/jakarta-commons/commons-pool.jar under the classpath. The following is a configuration fragment that uses DBCP to configure an Oracle data source:
<BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"Destroy-method= "Close" ><property name= "Driverclassname"value= "Oracle.jdbc.driver.OracleDriver "/>< Propertyname= "url"value= "Jdbc:oracle:thin: @localhost: 1521:orcl"/>< Propertyname= "username"value= "root"/>< Propertyname= "Password"value= "1234″/></Bean>
2:C3P0 Data source
C3P0 is an open source JDBC data source implementation project that is published in the Lib directory with Hibernate and implements the connection and statement pools for JDBC3 and JDBC2 extension specification descriptions. The C3p0 class package is located in <spring_home>/lib/c3p0/c3p0-0.9.0.4.jar. Here is an Oracle data source configured using C3P0:
<BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"Destroy-method= "Close" ><property name= "Driverclassname"value= "Oracle.jdbc.driver.OracleDriver "/>< Propertyname= "url"value= "Jdbc:oracle:thin: @localhost: 1521:orcl"/>< Propertyname= "username"value= "root"/>< Propertyname= "Password"value= "1234″/></Bean>
3. Spring's data source implementation class (Drivermanagerdatasource)
Spring itself also provides a simple data source implementation class Drivermanagerdatasource, which is located in the Org.springframework.jdbc.datasource package. This class implements the Javax.sql.DataSource interface, but it does not provide a mechanism for pooling connections, and it simply creates a new connection each time a call to getconnection () gets a new connection. Therefore, this data source class is better suited for use in unit testing or simple standalone applications because it does not require additional dependency classes.
<BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource"Destroy-method= "Close" ><property name= "Driverclassname"value= "Oracle.jdbc.driver.OracleDriver "/>< Propertyname= "url"value= "Jdbc:oracle:thin: @localhost: 1521:orcl"/>< Propertyname= "username"value= "root"/>< Propertyname= "Password"value= "1234″/></Bean>
4. Get the Jndi data source
If the application is configured on a high-performance application server, such as WebLogic or WebSphere, we may prefer to use the data source provided by the application server itself. The data source for the application server is used by Jndi open callers, and spring specifically provides the Jndiobjectfactorybean class that references the Jndi resource. The following is a simple configuration:
<id= "DataSource"class= " Org.springframework.jndi.JndiObjectFactoryBean "><property namevalue=" Java:comp/env/jdbc/oracle "/></bean>
Specifies the referenced Jndi data source name by Jndiname.
- What objects must be injected into the template when using spring's jdbctemplate to manipulate the data, and the template should not manually close the database connection
Injecting DataSource data source objects
Do not manually close the database connection, JdbcTemplate will help us close the database connection
- JdbcTemplate's queryForList method gets the list collection, which is why each object in the list collection is of the data type, and how the El expression is used in the JSP page.
Each object is a JAVA.UTIL.MAP type of data, and the EL expression uses <c:foreach> values
- What are the benefits of the concept of AOP and the use of AOP mechanisms? There are several ways to implement AOP in Java programming
The concept of AOP is aspected oriented programming aspect programming.
Benefit: AOP breaks the program down into various aspects or concerns. This makes it possible to modularize, quite horizontally on the slitting. It solves crosscutting (crosscut) issues such as transactions, security, logging, and other crosscutting concerns that are not well addressed by OOP and procedural methods.
There are several ways to implement AOP:
1. In Spring 1.2, AOP is implemented through Proxyfactorybean, i.e. through dynamic proxies, aspect must inherit Methodbeforeadvice,methodafteradvice, etc.
2. Spring 2.0 AOP needs to be changed to the FBI class, and it does not need to implement some interfaces
3. Three using annotations (@AspectJ) for AOP
- There are several transaction processing in the spring framework. Please explain the difference between the two separately
The transaction management that spring provides can be divided into two categories: programmatic and declarative.
Programming, more flexible, but the code is large, there are more duplicate code;
Declarative is more flexible than programmatic.
- What is the total interface for a programmatic transaction? What are the core class objects of spring transactions that need to be used with programmatic transactions
Total interface
Platformtransactionmanager interface,
Core class:
- Transactiondefinition//Transaction Property Definition
- Transcationstatus//Represents the current transaction, which can be committed and rolled back.
- Platformtransactionmanager Sub-classes of the core interface
- Spring's declarative transactions can generate proxy interfaces for ordinary classes, can you use Try/catch in your code to catch exceptions, and if not, explain why
No. Spring's declarative transactions generate proxies for the implementation class. You cannot use Try/catch in your code, because exceptions are caught in the code, and the spring container cannot catch exceptions.
- When using Transactionproxyfactorybean to generate transactional physics for a bean, the settings for which property values are required:
<Beanname= "Basetransactionproxy"class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" ><!– The transaction manager for the transaction agent Bean –>< Propertyname= "TransactionManager"ref= "TransactionManager" ></property><!– Set transaction Properties –>< Propertyname= "Transactionattributes" ><props><!– All methods adopt required's transaction strategy-><propKey= "*" >PROPAGATION_REQUIRED</prop></props></ Property><!– Set the target for the transaction agent Bean bean–>< Propertyname= "Target"ref= "Userdao" ></property></Bean><!– Target bean–><Beanname= "Userdao"class= "Com.dao.userDao" ><property name= "DataSource"ref= "DataSource"/></ Property></Bean>
Spring Framework interview related issues