Five, open source framework ~ interview ~ spring

1. Spring basics and IOC_ multiple choice questions

1. The following error description about spring is: (C)

A Spring supports pluggable transaction managers that make transaction demarcation easier without having to deal with the underlying issues.

B Spring's common abstraction layer for transaction management also includes JTA strategies and a JDBC DataSource.

C Like JTA or EJB CMT, Spring's transaction support is dependent on the Java EE environment.

D Spring transaction semantics are applied to POJOs via AOP, configured via XML or Java SE 5 annotations.

 

2. Which of the following options is not the way of interface injection in Spring? (D)

A interface injection  

B Constructor Injection  

C set value injection  

D pointer injection

 

3. Which of the following is incorrect about the IoC description in Spring features: (A)

A IoC means that the relationship between programs is directly manipulated by program code.

B The so-called "inversion of control" refers to the transfer of control rights from the application code to the external container, the transfer of control rights,

C IoC moves the responsibility of control creation into the framework; and decouples it from the application code

D When using Spring's IoC container, you only need to point out the objects that the component needs, and Spring's IoC container will provide it with XML configuration data at runtime.

 

 2. Spring basics and IOC_Short answer questions

1. Please briefly describe the working mechanism of Spring?

Reference answer: Spring's working mechanism can be described from the following points: (1) spring mvc asks all requests to be submitted to DispatcherServlet, which entrusts other modules of the application system to be responsible for the real processing of requests. (2) DispatcherServlet queries one or more HandlerMappings to find the Controller that handles the request. (3) DispatcherServlet please submit the request to the target Controller (4) After the Controller performs business logic processing, it will return a ModelAndView (5) Dispathcher queries one or more ViewResolver view resolvers and finds the view object specified by the ModelAndView object (6) View object Responsible for rendering back to the client.

 

2. Please answer why you use Spring's working mechanism?

Reference answer: Spring can effectively organize your middle-tier objects, regardless of whether you choose to use EJB. If you only use strus or other framework for j2ee's API features, spring is committed to solving the remaining problems. Spring can eliminate the excessive use of singletons that are common in many projects. This is a big problem, it reduces the testability and object orientation of the system. By reducing the cost of programming to interfaces rather than classes to almost nothing, spring promotes good habit formation.

 

3. Please briefly describe what Spring is?

Reference answer: Spring is a lightweight container, a lightweight implementation of the J2EE specification, and a "one-stop" solution for enterprise applications. The core of which is the bean factory, which is used to construct the Model we need. Spring is non-intrusive, and the objects in Spring's application do not depend on Spring's specific classes.

 

4. Briefly describe the composition of spring?

Reference answer: Spring is mainly composed of the following six parts ① Spring Core: core container, BeanFactory provides components life cycle management, component creation, assembly, destruction and other functions. SpringContext: ApplicationContext, which extends the core container and provides functions such as event processing and internationalization. It provides the functions of some enterprise-level services, and provides support for JNDI, EJB, and RMI. ② Spring AOP: Provides aspect support. ③ Spring DAO: Provide transaction support, JDBC, DAO support. ④ Spring ORM: Encapsulate or support popular O/R Mapping. ⑤ Spring Web: Provides Web application context, and provides functional support for Web development, such as requests, forms, exceptions, etc. ⑥ Spring Web MVC: full-featured MVC framework, equivalent to Struts.

 

5. Briefly describe what functions the Spring container provides?

Reference answer: The Spring container provides the management of objects, such as the Spring container is responsible for generating, assembling, and destroying components. In addition, the Spring container also provides support for persistence and support for transactions. In addition, the Spring container provides functions such as internationalization.

 

6. In Spring, there are several ways of bean injection, what is each?

Reference answer: In Spring, there are two ways to inject beans, namely Setter injection and Constructor injection.

 

7. Briefly describe: Spring bean scope?

Reference answer: Before spring 2.0, there were only two types of bean scopes: singleton (singleton) and non-singleton (also called prototype). After Spring 2.0, three types of session, request and global session were added for web applications. Program context beans. Therefore, Spring 2.0 now has five types of beans by default. Of course, Spring 2.0 refactored the design of bean types, and designed flexible bean type support. In theory, there can be countless types of beans. Users can add new bean types according to their own needs to meet the needs of practical application requirements.

 

8. Please describe the advantages of setting injection?

Reference answer: Advantages of setting injection: (1) It is more similar to the traditional JavaBean writing, and it is easier for program developers to understand and accept. Setting dependencies through setter methods is more intuitive and natural. (2) For complex dependencies, if constructor injection is used, the constructor will be too bloated and difficult to read. When Spring creates a bean instance, it needs to instantiate all instances of its dependencies at the same time, which leads to performance degradation, and the use of set value injection can solve these problems. (3) Especially when some attributes are optional, the multi-parameter constructor is more cumbersome.

 

9. Please describe the advantages of construct injection?

Reference answer: Advantages of construction injection: (1) The injection order of dependencies can be determined in the constructor, and the priority injection of dependencies is given priority. (2) Construct injection is more useful for beans whose dependencies do not need to change. Because there is no setter method, all dependencies are set in the constructor, so there is no need to worry about the subsequent code destroying the dependencies. (3) The dependencies can only be set in the constructor, and only the creator of the components can change the dependencies of the components. For the caller of the component, the internal dependencies of the component are completely transparent, which is more in line with the principle of high cohesion.

 

10. Name the three ways a bean factory can create a bean?

Reference answer: There are three ways for a bean factory to create a bean:

(1) Direct construction requires a default constructor and corresponding setters/getters methods.

It should be noted that we can pass parameters for getter/setter methods (with the preperties tag), and we can also pass parameters for constructors

(2) Use static factory methods, such as:

<bean id="exampleBean"class="examples.ExampleBean2"

factory-method="createInstance"/>

This requires that the examples.ExampleBean2 class has a static method createInstance

(3) Non-static factory method

<bean id="myFactoryBean" class="..."> </bean>

<bean id="exampleBean"

 factory-bean="myFactoryBean" factory-method="createInstance"/>

Here, there must be no class tag, factory-bean is BeanFactory, factory-method is its non-static method, myFactoryBean can be managed and configured by the container.

 

11. Please write the method of bean's life cycle?

Reference answer: (1) Specify the initialization method by setting the init-method attribute of the bean. His limitation is that the method cannot accept any parameters, and the method can be static. (2) Implement the afterPrioertiesSet() method of the InitializingBean interface. (3) Destroying objects can be achieved through destroyableBean's destroy.

 

12. Please briefly describe your understanding of IOC?

Reference answer: IOC is Inversion of Control, which is the inversion of control. Ioc moves the responsibility of control creation into the framework and separates it from the application code. When using the Ioc container, you need to point out what objects the component needs in the It is provided to it by the runtime container, which does this by looking at the object's parameter table, and possibly based on configuration data such as xml.

 

13. Please answer: What is the greatest benefit of IoC?

Reference answer: The biggest benefit of IoC is that it reduces the coupling of objects and achieves loose coupling of applications. Because the object generation is defined in XML, it becomes very simple when we need to change an implementation subclass (usually such objects are implemented in a certain interface), just modify the XML.

 

14. Briefly describe the type of IoC?

Reference answer: IOC can be divided into three types of injection, namely constructor injection, property injection and interface injection. Spring mainly supports constructor injection and property injection.

 

15. What is the difference between dependency injection and traditional programming in Spring?

Reference answer: In the traditional programming process, it is usually up to the caller to create an instance of the callee. But in Spring, the work of creating the callee is no longer done by the caller, so it is called inversion of control; the work of creating an instance of the callee is usually done by the Spring container, and then injected into the caller, so it is also called a dependency injection. The main role of dependency injection is to play the role of decoupling.

 

3. .AOP_Short Answer

1. What are the types of notifications in Spring?

Reference answer: Spring's advice types are (1) MethodBeforeAdvice (2) AfterReturningAdvice (3) MethodInterceptor (4) ThrowsAdvice

 

2. What is the difference between the target object implementing the interface and the target object not implementing the interface?

Reference answer: The main differences between the target object implementing the interface and the target object not implementing the interface are as follows:

(1) If the target object implements the interface, the dynamic proxy mechanism of JDK is used by default to implement AOP

(2) If the target object implements the interface, you can force spring to use CGLIB to implement the proxy

(3) If the target object does not implement the interface, CGLIB must be used to implement the proxy, and spring will automatically switch between CGLIB and JDK dynamic proxy

 

3. Please describe the difference between JDK dynamic proxy and CGLI proxy?

Reference answer: JDK's dynamic proxy can only proxy the target class that implements the interface, and the class that does not implement the interface cannot use the JDK's dynamic proxy CGLIB is to implement the proxy for the class, when the class that does not implement the interface needs to be proxyed The proxy needs to be implemented through CGLIB. Its principle is to generate a subclass for the specified target class and override the method to achieve enhancement. However, because inheritance is used, the finall class cannot be inherited. The two need to be mixed in some special occasions 

 

4. Briefly describe what is the role of ProxyFactoryBean?

Reference answer: The role of ProxyFactoryBean is to apply the aspect to the target object according to the configuration information to generate a dynamic proxy object.

 

5. Describe the principle of automatic proxy in Spring?

Reference answer: When Spring generates a proxy object, by default, it will use the interface of the proxied object to generate the proxy object. If the proxy object does not implement the interface, Spring will use CGLIB to generate a proxy object, which is a subclass of the proxy object.

5. Write down what are the three elements that need to be specified to create a proxy object?

Reference answer: The three elements that need to be specified to create a proxy object are: target: set the target object (only one); proxyInterfaces: set the proxy interface (the interface implemented by the target object); interceptorNames: set the name of the interceptor ( List of individual advice or advisor beans)

 

6. What are the two ways to write a proxy?

Reference answer: The two ways of proxy are: static proxy and dynamic proxy, in which static proxy writes proxy classes for each concrete class; write a proxy class for one interface. The dynamic proxy writes an InvocationHandler for one aspect, and then borrows the Proxy class in the JDK reflection package to dynamically generate corresponding proxies for various interfaces

 

7. Please briefly describe: What is AOP?

Reference answer: Extract the cross-business logic in the program and call it an aspect. The process of dynamically weaving these aspects into the target object and then generating a proxy object.

 

8. Briefly describe the core of AOP?

Reference answer: AOP core mainly includes the following: (1) Aspect (section), (2) Joinpoint (connection point), (3) Advice (advice), (4) Pointcut (pointcut), (5) Introduction (introduction). ), (6) Weaving (weaving), (7) Target (target object), (8) Proxy (proxy object)

 

9. Please describe the meaning of AOP transaction?

Reference answer: The usual way of transaction management in Spring is to use AOP (slice-oriented programming) to encapsulate transaction control for ordinary java classes. It is implemented by dynamic proxy. Since the interface is instantiated lazily, spring is in this section. Through the interceptor in time, load the transaction slice.

 

Fourth, Spring's support for persistence _ short answer questions

1. Please describe Spring's strategy for persistence support?

Reference answer: Spring has adopted a good support for the persistence layer. These support strategies mainly include: (1) Spring "does not invent the repeated wheel" for the persistence layer, that is, it does not re-implement a new persistence layer scheme, and the existing persistence layer The solution is encapsulated, which is more convenient to use. (2) Adopt DAO mode. (3) Provide a large number of template classes to simplify programming (HibernateDaoSupport, JdbcTemplate, etc.) (4) Redesign a complete set of exception architecture: ① Rich types, refine exception types. ② All are runtime exceptions (RuntimeException).

 

2. How does Spring simplify transaction configuration?

Reference answer: pring simplifies transaction configuration in two ways: the first way is to use TransactionProxyFactoryBean to create a transaction proxy (usually the transaction proxy uses the Service layer as the target bean) to configure hibernate's transaction manager, using the HibernateTransactionManager class, which implements the PlatformTransactionManager interface , a specific implementation of persistent connections for hibernate. The second method uses automatic proxy creation to simplify transaction configuration. When using BeanNameAutoProxyCreator and DefaultAdvisorAutoProxyCreator to create proxies, it is not necessary to create transaction proxies. The key lies in the incoming interceptor. If a transaction interceptor is passed in, transaction proxies will be automatically generated.

 

3. Please briefly describe the transaction mechanism of Spring?

Reference answer: Spring has rich support for transactions. In addition to programmatically handling transactions, Spring also supports declarative transactions. Second, Spring uses transaction service proxies and transaction managers (such as HibernateTransactionManager) to support transaction services. In addition, Spring adds a nested transaction to the boundary of the transaction.

 

4. Please answer: What is the difference between getCurrentSession() and openSession() methods in Spring API?

Reference answer: There are two main differences between the getCurrentSession() and openSession() methods: (1) The session created with getCurrentSession() will be bound to the current thread, while the session created with openSession() will not. (2) The session created by getCurrentSession() will be automatically closed after commit or rollback, while the openSession method needs to be closed manually.

 

5. Please describe the steps to use Hibernate transaction in Spring?

Reference answer: The steps to use Hibernate transactions in Spring are: (1) Configure the data source (2) Configure the sessionfactory (3) Configure the transaction manager (4) Create a transaction service proxy

 

6. Please describe declarative transaction processing in Spring?

Reference answer: Spring declarative transactions free us from complex transaction processing. So that we no longer need to deal with obtaining connections, closing connections, transaction commits, and rollbacks. No longer do we need to deal with lots of try...catch...finally code in transaction related methods. When we use Spring declarative transactions, a very important concept is transaction attributes. Transaction properties typically consist of the transaction's propagation behavior, the transaction's isolation level, the transaction's timeout value, and the transaction read-only flag. When we divide the transaction, we need to define the transaction, that is, configure the properties of the transaction.

 

7. Please describe Spring's transaction propagation properties and isolation levels?

Reference answer : When using Spring, most of its declarative transactions will be used. Simply configure some rules in the configuration file, and use Spring's AOP function to easily solve transaction problems; this involves a transaction. Propagation of the propagation property problem, it is defined in the TransactionDefinition interface for use by PlatfromTransactionManager, which is the core interface of spring transaction management.

Five different transaction isolation levels are defined in the TransactionDefinition interface. ISOLATION_DEFAULT This is the default isolation level of PlatfromTransactionManager, which uses the default transaction isolation level of the database. The other four correspond to the isolation level of JDBC, and ISOLATION_READ_UNCOMMITTED is the lowest isolation level of transactions. level, which allows another transaction to see the uncommitted data of this transaction. This isolation level produces dirty reads, non-repeatable reads and phantom reads.

 

五、Spring+Struts+Hibernate

1. Spring provides good support for a variety of ORM frameworks, briefly describe the method of using Hibernate in Spring, and combine transaction management?

Reference answer: Hibernate is the best ORM framework, and Spring provides good support for it. When using Hibernate in Spring, you should: (1) Write a hibernate mapping file for each bean, configure datasource, hibernateDaoTemplate, sessionFactory, and put Datasourse and mapping files are injected into sessionFactory (2) Each dao inherits a class HibernateDaoSupport provided in the spring container, and injects hibernateDaoTemplate for each dao (3) The method of getHibernateDaoTemplate() is used in the dao. In Spring, Biz that needs transaction control can be injected into transactionProxy, and transactionAttribute can be configured for the biz method

 

2. Please compare Spring framework with Struts?

Reference answer: Struts is just an MVC framework (Framework) for the rapid development of Java Web applications. The focus of Struts implementation is C (Controller), including Actionservlet/RequestProcessor and our custom Action, and also provides a series of tags (Custom Tag) for V (View). Spring is a lightweight container, the core of which is the bean factory (beanfactory) to construct the Model we need. On this basis, spring provides the implementation of AOP (Aspect Oriented Programming), which is used to provide services such as transactions and security declared in a non-managed environment; the extension of ApplicationContext to the Bean factory is more convenient for us to implement j2ee applications; Dao/ The implementation of ORM facilitates our database development; Web MVC and Spring Web provide a framework for java Web applications or integrate with other popular Web frameworks. A combination of the two is the best way.

 

3. Please describe the method of writing business logic?

Reference answer: Inherit the HibernateDaoSupport class and use HibernateTemplate for persistence. HibernateTemplate is a lightweight encapsulation of Hibernate Session. By default, runtime exceptions will be rolled back (including subclasses that inherit RuntimeException), and ordinary exceptions will not be rolled back. When writing business logic methods, it is best to throw exceptions all the way up and handle them in the presentation layer (struts). Regarding the setting of the transaction boundary, it is usually set to the business layer, not to Dao.

 

4. Why does the business layer choose Spring in the Web layered architecture?

Reference answer: Because the Service layer needs to deal with business logic and cross business logic, deal with transactions, logs, security, etc., and these coincide with Spring's IoC features, AOP, etc.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326124702&siteId=291194637