Summary of interview questions for spring and springMVC

1. What are the application scenarios, principles and benefits of AOP in Spring ?

Answer: AOP-- Aspect Oriented Programming is aspect-oriented programming; it is used to encapsulate cross-cutting concerns, which can be used in the following scenarios :

Authentication permissions, Caching cache, Context passing content delivery, Error handling, Lazy loading, Debugging debugging, logging, tracing, profiling and monitoring Recording and tracing optimization calibration, Performance optimization, Persistence persistence, Resource pooling, Synchronization Synchronization, Transactions Transactions

Principle: AOP is aspect-oriented programming, which is to add unified functions to the program through dynamic proxy, and focus on solving some common problems.

Advantages: 1. The good isolation and coupling between each step is greatly reduced 
           . 2. The source code is irrelevant, and the function is expanded without modifying the source code. 

2. The role and principle of IOC in Spring? The process of object creation.

Answer: IOC - Inversion of Control . When a role needs the assistance of another role, in the traditional programming process, the caller usually creates an instance object of the callee . But the job of creating the callee in spring is no longer done by the caller, so it is called inversion of control. The work of creating the callee is done by spring , and then injected into the caller for  direct use.

3. Introduce the spring framework

   It is a one-stop ( full-stack ) framework that provides a complete set of solutions from the presentation layer- springMVC to the business layer- spring to the persistence layer- springdata . We can only use the spring framework in the project, which can provide the MVC framework of the presentation layer and the Dao framework of the persistence layer . Its two cores, IoC and AOP , provide support for our program decoupling and code simplicity and easy maintenance.

4. Spring's common annotations for creating objects?

答:@Component@Controller@ Service@ Repository

5. Design patterns used in Sp ring

Answer: Simple Factory, Factory Method, Singleton Pattern, Adapter, Wrapper, Proxy, Observer, Strategy, Template Method

Detailed introduction : Please refer to this Weibo: Develop common design patterns

6. What are the advantages of Spring?

Answer: 1. Reduce the coupling between components and realize the decoupling between the various layers of the software 
2. Can use many services that are easily provided, such as transaction management, message services, etc. 
3. The container provides singleton mode support 
4. The container provides AOP technology, and it is easy to use it to implement functions such as permission interception and runtime monitoring. 
5. The container provides many auxiliary classes, which can speed up application development 
6.Spring provides integrated support for mainstream application frameworks, such as hibernate , JPA , Struts , etc. 
7. Spring is a low-intrusive design, and the pollution of the code is extremely low 
8. Independent of various application servers 
9. Spring 's DI mechanism reduces the complexity of business object replacement 
10. Spring 's high degree of openness does not force the application to completely depend on Spring , and developers can freely choose part or all of spring  

7. What is the difference between the scopes of Spring Beans ?

The beans in the Spring container can be divided into 5 scopes. All scope names are self-explanatory, but to avoid confusion let's explain:

singleton : This bean scope is the default. This scope ensures that no matter how many requests are received, there is only one instance of the bean in each container. The singleton pattern is maintained by the bean factory itself.

prototype : The prototype scope is the opposite of the singleton scope, providing an instance for each bean request.

request : An instance is created within the scope of the request bean for each network request from the client. After the request is complete, the bean is invalidated and recycled by the garbage collector.

Session : Similar to the request scope, ensures that there is an instance of the bean in each session , and the bean will be invalidated after the session expires .

global-sessionglobal-sessionPortlet应用相关。当你的应用部署在Portlet容器中工作时,它包含很多portlet。如果你想要声明让所有的portlet共用全局的存储变量的话,那么这全局变量需要存储在global-session中。

全局作用域与Servlet中的session作用域效果相同。

8.Spring管理事务有几种方式?

答:有两种方式:

1、编程式事务,在代码中硬编码。(不推荐使用)

2、声明式事务,在配置文件中配置(推荐使用)

声明式事务又分为两种:

a、基于XML的声明式事务

b、基于注解的声明式事务

9.spring中自动装配的方式有哪些?

答:1、 No:即不启用自动装配。

2、 byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printerbean,然后使用Seter方法为其注入。

3、 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printerbean,使用Seter方法为其注入。

4、 constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Seter方法注入,而是使用构造子注入。

5、 autodetect:在byTypeconstructor之间自动的选择注入方式。

6、 default:由上级标签<beans>default-autowire属性确定。

10.spring中的核心类有那些,各有什么作用?

答:BeanFactory:产生一个新的实例,可以实现单例模式

BeanWrapper:提供统一的getset方法

ApplicationContext:提供框架的实现,包括BeanFactory的所有功能

11.Bean的调用方式有哪些?

答:有三种方式可以得到Bean并进行调用: 
1、使用BeanWrapper 
HelloWorld hw=new HelloWorld(); 
BeanWrapper bw=new BeanWrapperImpl(hw); 
bw.setPropertyvalue(msg,HelloWorld); 
system.out.println(bw.getPropertyCalue(msg)); 
2、使用BeanFactory 
InputStream is=new FileInputStream(config.xml); 
XmlBeanFactory factory=new XmlBeanFactory(is); 
HelloWorld hw=(HelloWorld) factory.getBean(HelloWorld); 
system.out.println(hw.getMsg()); 
3、使用ApplicationConttext 
ApplicationContext actx=new FleSystemXmlApplicationContext(config.xml); 
HelloWorld hw=(HelloWorld) actx.getBean(HelloWorld); 
System.out.println(hw.getMsg());

12.么是IOC,什么又是DI,他们有什么区别?

答:依赖注入DI是一个程序设计模式和架构模型, 一些时候也称作控制反转,尽管在技术上来讲,依赖注入是一个IOC的特殊实现,依赖注入是指一个对象应用另外一个对象来提供一个特殊的能力,例如:把一个 数据库连接已参数的形式传到一个对象的结构方法里面而不是在那个对象内部自行创建一个连接。控制反转和依赖注入的基本思想就是把类的依赖从类内部转化到外 部以减少依赖

应用控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用,传递给它。也可以说,依赖被注入到对象中。所 以,控制反转是,关于一个对象如何获取他所依赖的对象的引用,这个责任的反转。

13.spring有两种代理方式:

若目标对象实现了若干接口,spring使用JDKjava.lang.reflect.Proxy类代理。

      优点:因为有接口,所以使系统更加松耦合

      缺点:为每一个目标类创建接口

若目标对象没有实现任何接口,spring使用CGLIB库生成目标对象的子类。

      优点:因为代理类与目标类是继承关系,所以不需要有接口的存在。

      缺点:因为没有使用接口,所以系统的耦合性没有使用JDK的动态代理好。

14.springMVC的流程?

答:1.用户发送请求至前端控制器DispatcherServlet

2.DispatcherServlet收到请求调用HandlerMapping处理器映射器。

3.处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet

4.DispatcherServlet通过HandlerAdapter处理器适配器调用处理器

5.执行处理器(Controller,也叫后端控制器)

6.Controller执行完成返回ModelAndView

7.HandlerAdaptercontroller执行结果ModelAndView返回给DispatcherServlet

8.DispatcherServletModelAndView传给ViewReslover视图解析器

9.ViewReslover解析后返回具体View

10.DispatcherServletView进行渲染视图(即将模型数据填充至视图中)。

11.DispatcherServlet响应用户

15.Springmvc的优点

答:1.它是基于组件技术的.全部的应用对象,无论控制器和视图,还是业务对象之类的都是 java组件.并且和Spring提供的其他基础结构紧密集成.

2.不依赖于Servlet API(目标虽是如此,但是在实现的时候确实是依赖于Servlet)

3. 可以任意使用各种视图技术,而不仅仅局限于JSP

4 . 支持各种请求资源的映射策略

5 .它应是易于扩展的

Guess you like

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