SSM-spring business layer framework

Spring framework

Spring is a layered full-stack lightweight open source framework for Java SE/EE applications.

With IoC (Inverse Of Control) and AOP (Aspect Oriented

Programming: aspect-oriented programming) as the core, provides many enterprise-level application technologies such as the presentation layer SpringMVC, the persistence layer Spring JDBC, and business layer transaction management. It can also integrate many well-known third-party frameworks and libraries in the open source world, and gradually become the use The most open source framework for Java EE enterprise applications.

Advantages of Spring framework

1 Convenient decoupling to simplify development

Through the IoC container provided by Spring, the dependencies between objects can be controlled by Spring to avoid excessive program coupling caused by hard coding.

Users no longer need to write code for very low-level requirements such as singleton pattern classes and property file analysis, and can focus more on upper-level applications.

2 AOP programming support

Through Spring's AOP function, it is convenient to carry out aspect-oriented programming, and many functions that are not easy to implement with traditional OOP can be easily handled by AOP.

3 Declarative transaction support

It can free us from the monotonous and boring transaction management code, and flexibly manage the transaction in a declarative way to improve development efficiency and quality.

4 Convenient program testing  

Almost all testing work can be performed in a non-container-dependent programming method. Testing is no longer an expensive operation, but something that can be done at hand.

5 Convenient integration of various excellent frameworks

Spring can reduce the difficulty of using various frameworks and provide direct support for various excellent frameworks (Struts, Hibernate, Hessian, Quartz, etc.).

6Reduce the difficulty of using JavaEE API

Spring has a thin encapsulation layer for JavaEE APIs (such as JDBC, JavaMail, remote calls, etc.), which greatly reduces the difficulty of using these APIs.

7 Spring's source code is a classic learning example

Spring's source code design is exquisite, clear in structure, and original ingenuity. It reflects the master's flexible use of Java design patterns and advanced knowledge of Java technology.

Its source code is not intended to be a best practice example of Java technology.

Program coupling

Coupling refers to the dependencies between programs, including: dependencies between classes and dependencies between methods. In actual development: we should do: do not depend on compile time, only depend on runtime.

***The program emphasizes low coupling and high cohesion. That is, the various elements in the same module should be highly close, but the interdependence between the various modules is not so close.

Decoupling method

Why decoupling? It is to improve the flexibility and scalability of our program.

The idea of ​​decoupling:

1. Use reflection to create objects, and avoid using the new keyword.

2. Obtain the fully qualified class name of the object to be created by reading the configuration file.

Factory mode combined with configuration file decoupling

In actual development, we can configure all three-layer objects using configuration files. When starting the server application to load, let a method in a class read the configuration file to create these objects and store them together. In the next use, just take it and use it directly. Then, the class that reads the configuration file, creates and obtains the three-layer object is the factory.

Spring's three classic ideas

1. IOC

Concept: Inversion Of Control translates to "inversion of control"

Essence: the object is handed over to Spring, not new by itself

Role: to reduce the coupling of computer programs (releasing the dependencies in the code)

Principle: factory mode + reflection + xml configuration file

2. OF

Concept: Dependency Injection. Translated into "dependency injection", it is the specific implementation of the core IOC of the spring framework.
Essence: set a value for an object

Role: There is a dependency between the correspondences, we no longer need to manually set the value, the Spring framework will help us solve it

Principle: reflection setting value

3. AOP

Concept: The full name is Aspect Oriented Programming, namely: Aspect Oriented Programming

It is to extract the repetitive code of our program, when it needs to be executed, use the technology of dynamic proxy to enhance our existing methods without modifying the source code.

Function: During the running of the program, do not modify the source code to enhance the existing method

Advantages: reduce repetitive code; improve development efficiency; convenient maintenance

实现方式:使用动态代理技术,在 Spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式

-------AOP核心概念-------

目标类(target): 要被增强的类

代理类(proxy): 使用动态代理产生目标类的代理

切入点(pointcut): 目标类中需要增强的方法,这些方法都称为切入点  

通知(advice): 增强类中定义的方法,这些方法用于增强目标方法

切面(aspect): 切入点+通知

连接点(joinpoint): 目标类中的所有方法    连接点包含切入点

织入(weaving): 将通知方法加到目标方法中的过程  spring aop 整个过程就是织入

引入(introduction): 在目标类引入新的属性或者新的方法

依赖注入

依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。本质,就是设置值.
我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。
ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。

BeanFactory 和 ApplicationContext 的区别

在 BeanFactory 这棵继承树上的都叫 Bean 工厂,也叫 Spring 容器。

BeanFactory 才是 Spring 容器中的顶层接口。ApplicationContext 是它的子接口。
区别:
创建对象的时间点不一样。
ApplicationContext推荐使用,只要一读取配置文件,默认情况下就会创建对象。

它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
BeanFactory:什么时候使用什么时候创建对象。
它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。

bean 的作用范围和生命周期

单例对象: scope="singleton"  Spring 默认为单例
一个应用只有一个对象的实例。它的作用范围就是整个引用。
生命周期:
对象出生:当应用加载,创建容器时,对象就被创建了。
对象活着:只要容器在,对象一直活着。
对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
多例对象: scope="prototype"
每次访问对象时,都会重新创建对象实例。
生命周期:
对象出生:当使用对象时,创建新的对象实例。
对象活着:只要对象在使用中,就一直活着。
对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。

实例化 Bean 的三种方式

1. 使用默认无参构造函数

<!--在默认情况下:
它会根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败。 -->
<bean id="accountService" class="com.cast.service.impl.AccountServiceImpl"/>

2. Spring 管理静态工厂- 使用静态工厂的方法创建对象

public class StaticFactory {
         public static IAccountService createAccountService(){
                  return new AccountServiceImpl();
         }
}
<!-- 此种方式是:
使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器
id 属性:指定 bean 的 id,用于从容器中获取
class 属性:指定静态工厂的全限定类名
factory-method 属性:指定生产对象的静态方法-->
<bean  id="accountService"  class="com.cast.factory.StaticFactory"  factory-method="createAccountService"></bean>

3. spring 管理实例工厂- 使用实例工厂的方法创建对象

public class InstanceFactory {
         public IAccountService createAccountService(){
                  return new AccountServiceImpl();
         }
}
<!-- 此种方式是:
先把工厂的创建交给 spring 来管理。然后在使用工厂的 bean 来调用里面的方法
factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。-->
<bean  id="instancFactory"  class="com.cast.factory.InstanceFactory"></bean>
<bean  id="accountService"  factory-bean="instancFactory"  factory-method="createAccountService"></bean>

通知

前置通知 before   目标方法被调用之前,就执行该前置通知方法

后置通知 after-returning  目标方法 return 返回之后,就执行该返回通知方法

最终通知 after    目标方法被调用完之后,不关心返回结果,就执行该最终通知方法

环绕通知 around   包裹了目标方法,在目标方法之前和在目标方法之后整个过程,经常使用ProceedJoinPoint.proceed()来执行

异常通知 after-throwing  当目标方法在执行异常的时候,就会执行该异常通知方法

事务

Automicity 原子性: 即事务要么被全部执行,要么被全部不执行。如果事务下的子事务全部提交成功,则所有数据库操作被提交,否则,应进行事务回滚。

Consistency 一致性: 即状态转换必须是由一种正确的状态转换到另外一种正确的状态。

Isolation 隔离性: 即相互间必须不能被影响。

Durabillity 持久性: 即事务提交后将被永久保存,即便出现其他故障,事务处理结果也应得到保存。

事务的隔离级别

Read uncommitted: 读未提交,就是一个事务可以读取另一个未提交事务的数据。   --- 脏读

Read committed: 读已提交,顾名思义,就是一个事务要等另一个事务提交后才能读取数据。   --- 不可重复读

Repeatable read: 重复读,就是在开始读取数据(事务开启)时,不再允许修改操作。    --- 幻读

Serializable: 最高的事务隔离级别,在该级别下,事务串行化顺序执行,可以避免脏读、不可重复读与幻读。但是这种事务隔离级别效率低下,比较耗数据库性能,一般不使用。

事务的传播行为

REQUIRED: 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)   增删改
SUPPORTS: 支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)   查找
MANDATORY: 使用当前的事务,如果当前没有事务,就抛出异常
REQUERS_NEW: 新建事务,如果当前在事务中,把当前事务挂起。
NOT_SUPPORTED: 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
NEVER: 以非事务方式运行,如果当前存在事务,抛出异常
NESTED: 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作。

传播行为:业务层的方法处理事务的问题

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42629433/article/details/83450429