Good programmers to learn Java Spring routes share a common interview questions

This chapter content face questions from good programmers Java Tutorial for everyone finishing share, I want to help.

  Q: SpringIOC principles set forth

  A: The work to create an object, initialization, destruction, etc. to the Spring container to complete. We can IOC mode of operation of container seen as a sublimation of the factory model, you can put IOC container seen as a factory, this factory to produce the object definitions given in the configuration file, and then use programming language programming reflection to form the corresponding object class name given in the configuration file. From the implementation point of view, IOC is put before the factory method to write the dead object code generation, by changing the configuration file to define, that is, the plant generates both independent and objects separated by, the purpose is to improve the flexibility and maintainability.

  Q: SpringAOP principle

  A: 1) object-oriented design is no way to solve the problem of duplicate code

  2) SpringAOP implantation technique using dynamic agent-enhanced code at runtime, aspectj is woven into the compiler in the form of code to implement crosscutting Agent Technology

  3) SpringAOP uses two proxy mechanism, one is based on JDK dynamic proxy , one is based on CGLib dynamic proxies

  4) After JDK1.3 java provides a dynamic proxy technology , operational developers to create an interface at runtime proxy instance

  5) The dynamic proxy jdk relates java.lang.reflect package two classes ProxyInvcoationHandler

  6) InvcoationHandler is an interface, you can subscribe to the introduction of the cross-section through the interface logic , and the code of the target class invoked through reflection , the dynamic cross talk and business logic braided together

  7) Proxy using InvocationHandler dynamically create a symbol of an interface instance , to generate the proxy target object class

  8) cglib with very bottom of the byte code technology, can be created as a subclass of the class , and a method using interception technology to intercept calls to all methods of the parent class in the subclass , and woven into the transverse logic homeopathic

  Q: Explain the concept of AOP

  A: section : Aspect, the connection point: Joinpoint, enhanced : the Advice, entry points : Pointcut, audiences : the Target, Agent : the Proxy, wherein enhancing comprises : pre-enhancement , post-enhanced , returns will increase , surround enhancement , throws after an abnormal enhancement

  Q: SpringAOP be based in two ways

  A: One is more convenient and powerful way of notes , using annotations to configure SpringAOP generally divided into two steps , the first step in the xml file declarations activate the automatic scanning component functions , at the same time activate the auto proxy , kind it is quite satisfactory xml configuration

  Q: the Spring propagation mechanisms Affairs

  Answer: 1) REQUIRED (default) : supports the use of the current transaction , if the transaction does not currently exist , create a new business.

  2) SUPPORTS: supports the use of the current transaction, if the transaction does not currently exist , not using transactions.

  3) MANDATORY: Chinese translation is mandatory, supports the use of the current transaction , if the transaction does not currently exist , then throws Exception .

  4) REQUIRES_NEW: create a new transaction, if the current transaction exists , the current transaction pending.

  5) NOT_SUPPORTED: no transaction execution, if the current transaction exists , the current transaction pending.

  6)NEVER:无事务执行,如果当前有事务则抛出Exception

  7)NESTED:嵌套事务,如果当前事务存在,那么在嵌套的事务中执行。如果当前事务不存在,则表现跟REQUIRED一样。

  问:Spring的事务实现方式

  答:1)编程式事务管理对基于POJO的应用来说是唯一选择。我们需要在代码中调用beginTransaction()commit()rollback()等事务管理相关的方法,这就是编程式事务管理。

  2)基于TransactionProxyFactoryBean的声明式事务管理

  3)基于@Transactional的声明式事务管理

  4)基于AspectjAOP配置事务

  问:Spring通过单实例化Bean简化多线程问题

  答:由于Spring的事务管理器是通过线程相关的ThreadLocal来保存数据访问基础设施(也即Connection实例)web容器本身就是多线程的,web容器为一个http请求创建一个独立的线程(实际大多数采用线程池),所以bean也是运行在多线程的环境下,在大多数情况下,Springbean都是单例的,单例的好处就是线程无关性,不存在多线程并发问题,Spring是通过ThreadLocal将有状态的变量本地线程化,达到另一个层面上的线程无关。

  问:SpringMVC工作原理

  答:SpringMVC框架围绕dispactcherServlet这个核心展开,dispatcherServletSpringMVC的总导演,总策划,他负责拦截请求并将器分派给响应的处理器处理。SpringMVC框架包括注解驱动控制器,请求及响应的信息处理,表单标签绑定,视图解析,本地化解析,上传文件解析,异常处理。

  SpringMVC通过一个前端servlet接收所有的请求,并将具体工作委托给其他组件进行处理

  1)整个过程开始于客户端发送一个HTTP请求,如果匹配web.xml的映射路径,则进行处理

  2)DispatcherServlet根据HandlerMapping找到对应的Handler,将处理权交给Handler

  3)HandlerAdapter这个适配器对各种Hander方法进行调用

  4)处理完了之后返回一个ModelAndViewDispatcherServelt

  5)Handler返回的ModelAndView()只是一个逻辑视图并不是一个正式的视图,DispatcherSevlet通过ViewResolver将逻辑视图转化为真正的视图View

  6)根据ModelAndView对模型数据进行视图渲染

  7)最终客户端得到相应消息,可能是一个普通的HTML页面,也可能是一个XML或者JSON

  问:SpringMVC和Struts2的区别

  答:1)拦截机制的不同

  Struts2是类级别的拦截,每次请求就会创建一个Action,和Spring整合时Struts2ActionBean注入作用域是原型模式prototype,然后通过settergetterrequest数据注入到属性。Struts2中,一个Action对应一个requestresponse上下文,在接收参数时,可以通过属性接收,这说明属性参数是让多个方法共享的。Struts2Action的一个方法可以对应一个url,而其类属性却被所有方法共享,这也就无法用注解或其他方式标识其所属方法了,只能设计为多例。

  SpringMVC是方法级别的拦截,一个方法对应一个Request上下文,所以方法直接基本上是独立的,独享requestresponse数据。而每个方法同时又何一个url对应,参数的传递是直接注入到方法中的,是方法所独有的。处理结果通过ModeMap返回给框架。在Spring整合时,SpringMVCControllerBean默认单例模式Singleton,所以默认对所有的请求,只会创建一个Controller,有应为没有共享的属性,所以是线程安全的,如果要改变默认的作用域,需要添加@Scope注解修改。

  Struts2有自己的拦截Interceptor机制,SpringMVC这是用的是独立的AOP方式,这样导致Struts2的配置文件量还是比SpringMVC大。

  2)底层框架的不同

  Struts2采用Filter(StrutsPrepareAndExecuteFilter)实现,SpringMVC(DispatcherServlet)则采用Servlet实现。Filter在容器启动之后即初始化;服务停止以后坠毁,晚于ServletServlet在是在调用时初始化,先于Filter调用,服务停止后销毁。

  3)性能方面

  Struts2是类级别的拦截,每次请求对应实例一个新的Action,需要加载所有的属性值注入,SpringMVC实现了零配置,由于SpringMVC基于方法的拦截,有加载一次单例模式bean注入。所以,SpringMVC开发效率和性能高于Struts2

  4)配置方面

SpringMVCSpring是无缝的。从这个项目的管理和安全上也比Struts2高。

 


Guess you like

Origin blog.51cto.com/14573321/2442803