spring face questions Highlights: Notes + MVC + data access, with answers

annotation

What are the important Spring annotation you used?

  • @Controller  - for Spring MVC controller class project.
  • @Service  - for the service class.
  • @RequestMapping  - URI mapping for controller configuration handler method.
  • @ResponseBody  - Object for transmitting a response, is used to send data as XML or JSON response.
  • @PathVariable  - dynamic values used to map from the URI parameter to the handler method.
  • @Autowired  - for automatically fitted in the spring bean dependency.
  • @Qualifier  - @Autowired annotation used to avoid confusion when multiple instances bean types.
  • @Scope  - used to configure the range of spring bean.
  • @Configuration , @ComponentScan  and  @Bean  - a java-based configuration.
  • @Aspect , @Before , @After , @Around , @Pointcut  - section for programming (AOP).

How to Start Notes assembled in the spring?

By default, Spring annotation unopened container assembly. Therefore, to use annotation-based assembly, we have <context: annotation-config /> element is enabled by configuring it in the Spring configuration file.

What is the difference @Component, @Controller, @Repository, @Service?

  • @Component: This will be marked as class java bean. It is a common stereotype any Spring-managed components. The scan mechanism of the spring assembly can now be picked up and pulled into the application environment.
  • @Controller: This will mark a class as a Spring Web MVC controllers. It will be marked automatically imported into the Bean IoC container.
  • @Service: This comment is specialized components annotations. It does not provide any other act of @Component comment. You can use the service layer class @Service instead @Component, because it specifies the intent in a better way.
  • @Repository: This is a specialization annotation having similar purpose and function annotation @Component. It provides additional benefits for DAO. It DAO import IoC container, and unchecked exception are eligible to convert Spring DataAccessException.

@Required annotation what's the use?

@Required applied to bean property setter methods. This annotation indicates that only the attribute values ​​must be explicitly defined in the bean when configuring or using automated assembly filled affected bean properties. If the property has not been affected bean filling, the container will throw BeanInitializationException.

Example:

public class Employee {
 private String name;
 @Required
 public void setName(String name){
 this.name=name;
 }
 public string getName(){
 return name;
 }
}

@Autowired annotation what's the use?

@Autowired can be controlled more accurately how the automatic assembly and where. This annotation is used in the setter method, constructor, having the property name or method of any one or more parameters of automatic assembly bean. By default, it is the type of drive of the injection.

public class Employee {
 private String name;
 @Autowired
 public void setName(String name) {
 this.name=name;
 }
 public string getName(){
 return name;
 }
}

@Qualifier annotation what's the use?

When you create more of the same type of bean and want to use only attribute in which the assembly of a bean, you can use annotations and @Qualifier @Autowired to disambiguate by specifying exactly which bean should be assembled.

For example, here we are two classes, Employee and EmpAccount. In EmpAccount using @Qualifier id is specified to be equipped emp1 the bean.

Employee.java

public class Employee {
 private String name;
 @Autowired
 public void setName(String name) {
 this.name=name;
 }
 public string getName() {
 return name;
 }
}

EmpAccount.java

public class EmpAccount {
 private Employee emp;
 @Autowired
 @Qualifier(emp1)
 public void showName() {
 System.out.println(“Employee name : ”+emp.getName);
 }
}

@RequestMapping annotation what's the use?

@RequestMapping annotation for a particular method of mapping HTTP request to the controller corresponding to the processing request to a specific class / method. This annotation can be applied at two levels:

  • URL mapping request: class level
  • Method level: Mapping URL and HTTP request methods

data access

spring DAO what's the use?

Spring DAO such JDBC, Hibernate or JDO data access technologies such as easier to work with in a uniform manner. This enables the user to easily switch between the persistence technology. It also allows you to write code, without regard to the capture of each of the different technical anomalies.

Spring DAO enumerated exceptions thrown.

spring face questions Highlights: Notes + MVC + data access, with answers

Which classes exist API in spring JDBC?

  • JdbcTemplate
  • SimpleJdbcTemplate
  • NamedParameterJdbcTemplate
  • SimpleJdbcInsert
  • SimpleJdbcCall

Spring Hibernate using access methods are what?

We can use two ways to access Hibernate Spring:

  1. Hibernate use templates and callbacks Inversion of Control
  2. Extended HibernateDAOSupport and apply AOP interceptors node

Cited spring supports transaction management type

Spring supports two types of transaction management:

  1. Programmatic transaction management: During this process, management services in programming help. It provides great flexibility for you, but it is very difficult to maintain them.
  2. Declarative transaction management: Here, the management and business affairs separate code. Use only annotation or XML-based configuration management services.

What ORM framework support spring

  • Hibernate
  • iBatis
  • JPA
  • Jdo
  • OJB

AOP

What is AOP?

AOP (Aspect-Oriented Programming), i.e.,  aspect-oriented programming , it OOP (Object-Oriented Programming, Object-Oriented Programming) complement each other, provided with OOP different perspective abstract software configuration. In OOP, we class (class) as our basic unit, the basic unit is AOP  Aspect (section)

The AOP Aspect, Advice, Pointcut, JointPoint and Advice What are the parameters?

spring face questions Highlights: Notes + MVC + data access, with answers

  • Aspect  - Aspect is a realization of cross-cutting issues of class, such as transaction management. Aspects of the general category can be configured, and then configure the Spring Bean configuration file, or we can use Spring AspectJ annotation support the use of @Aspect the class declaration as Aspect.
  • The Advice  - the Advice is the action to take for a particular JoinPoint. In programming, which is to achieve a method of performing matching JoinPoint having specific pointcuts in the application. You can Advice regarded Spring interceptor (Interceptor) or Servlet Filter (filter).
  • The Arguments the Advice  - we can pass parameters in the advice process. We can use args (in pointcut) expression to be applied to any method that matches the parameter mode. If we use it, then we need to use the same name in the advice method of determining the parameters of type.
  • Pointcut - Pointcut 是与 JoinPoint 匹配的正则表达式,用于确定是否需要执行 Advice。Pointcut 使用与 JoinPoint 匹配的不同类型的表达式。Spring 框架使用 AspectJ Pointcut 表达式语言来确定将应用通知方法的 JoinPoint。
  • JoinPoint - JoinPoint 是应用程序中的特定点,例如方法执行,异常处理,更改对象变量值等。在 Spring AOP 中,JoinPoint 始终是方法的执行器。

什么是通知(Advice)?

特定 JoinPoint 处的 Aspect 所采取的动作称为 Advice。Spring AOP 使用一个 Advice 作为拦截器,在 JoinPoint “周围”维护一系列的拦截器。

有哪些类型的通知(Advice)?

  • Before - 这些类型的 Advice 在 joinpoint 方法之前执行,并使用 @Before 注解标记进行配置。
  • After Returning - 这些类型的 Advice 在连接点方法正常执行后执行,并使用@AfterReturning 注解标记进行配置。
  • After Throwing - 这些类型的 Advice 仅在 joinpoint 方法通过抛出异常退出并使用 @AfterThrowing 注解标记配置时执行。
  • After (finally) - 这些类型的 Advice 在连接点方法之后执行,无论方法退出是正常还是异常返回,并使用 @After 注解标记进行配置。
  • Around - 这些类型的 Advice 在连接点之前和之后执行,并使用 @Around 注解标记进行配置。

指出在 spring aop 中 concern 和 cross-cutting concern 的不同之处。

concern 是我们想要在应用程序的特定模块中定义的行为。它可以定义为我们想要实现的功能。

cross-cutting concern 是一个适用于整个应用的行为,这会影响整个应用程序。例如,日志记录,安全性和数据传输是应用程序几乎每个模块都需要关注的问题,因此它们是跨领域的问题。

AOP 有哪些实现方式?

实现 AOP 的技术,主要分为两大类:

  • 静态代理 - 指使用 AOP 框架提供的命令进行编译,从而在编译阶段就可生成 AOP 代理类,因此也称为编译时增强;
  • 编译时编织(特殊编译器实现)
  • 类加载时编织(特殊的类加载器实现)。
  • 动态代理 - 在运行时在内存中“临时”生成 AOP 动态代理类,因此也被称为运行时增强。
  • JDK 动态代理
  • CGLIB

Spring AOP and AspectJ AOP 有什么区别?

Spring AOP 基于动态代理方式实现;AspectJ 基于静态代理方式实现。Spring AOP 仅支持方法级别的 PointCut;提供了完全的 AOP 支持,它还支持属性级别的 PointCut。

如何理解 Spring 中的代理?

将 Advice 应用于目标对象后创建的对象称为代理。在客户端对象的情况下,目标对象和代理对象是相同的。

Advice + Target Object = Proxy

什么是编织(Weaving)?

为了创建一个 advice 对象而链接一个 aspect 和其它应用类型或对象,称为编织(Weaving)。在 Spring AOP 中,编织在运行时执行。请参考下图:

spring face questions Highlights: Notes + MVC + data access, with answers

MVC

Spring MVC 框架有什么用?

Spring Web MVC 框架提供 模型-视图-控制器 架构和随时可用的组件,用于开发灵活且松散耦合的 Web 应用程序。MVC 模式有助于分离应用程序的不同方面,如输入逻辑,业务逻辑和 UI 逻辑,同时在所有这些元素之间提供松散耦合。

描述一下 DispatcherServlet 的工作流程

DispatcherServlet 的工作流程可以用一幅图来说明:

spring face questions Highlights: Notes + MVC + data access, with answers

  1. 向服务器发送 HTTP 请求,请求被前端控制器 DispatcherServlet 捕获。
  2. DispatcherServlet 根据 -servlet.xml 中的配置对请求的 URL 进行解析,得到请求资源标识符(URI)。然后根据该 URI,调用 HandlerMapping 获得该 Handler 配置的所有相关的对象(包括 Handler 对象以及 Handler 对象对应的拦截器),最后以HandlerExecutionChain 对象的形式返回。
  3. DispatcherServlet 根据获得的Handler,选择一个合适的 HandlerAdapter。(附注:如果成功获得HandlerAdapter后,此时将开始执行拦截器的 preHandler(…)方法)。
  4. 提取Request中的模型数据,填充Handler入参,开始执行Handler(Controller)。在填充Handler的入参过程中,根据你的配置,Spring 将帮你做一些额外的工作:
  • HttpMessageConveter:将请求消息(如 Json、xml 等数据)转换成一个对象,将对象转换为指定的响应信息。
  • 数据转换:对请求消息进行数据转换。如String转换成Integer、Double等。
  • 数据根式化:对请求消息进行数据格式化。如将字符串转换成格式化数字或格式化日期等。
  • Data Validation: validation of the data (the length, format, etc.), stores the verification result BindingResult or Error.
  1. After Handler (Controller) execution is completed, a return to the DispatcherServlet ModelAndView object;
  2. The return ModelAndView selecting a suitable ViewResolver (Spring must be registered to the container ViewResolver) returns to the DispatcherServlet.
  3. ViewResolver combination of Model and View, to render the view.
  4. The view is responsible for rendering the results back to the client.

Like this article forwarding and friends little attention, thanks for the support!

Guess you like

Origin blog.csdn.net/qwe123147369/article/details/92183711