# mojo-spring **Repository Path**: mojo-jj/mojo-spring ## Basic Information - **Project Name**: mojo-spring - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-18 - **Last Updated**: 2022-09-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #Spring 回顾 # 1、Spring > 一款轻量级的控制反转(IoC)和面向切面(AOP)的框架 优点 - 一款轻量级的java web开发框架(容器) - 控制反转(Inversion of Control)和面向切面( Aspect Oriented Programming) - 支持事务处理,兼容集成其他第三方库 # 2、IoC - 控制反转:将对象的创建,管理,转配交给IoC容器 - 实现方式:依赖注入(需要提供set方法或者构造函数) ~~~ @Test public void shouldAnswerWithTrue() { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); User user = (User) context.getBean("user"); System.out.println(user); } ~~~ ## IoC创建对象的方式 > **Constructor-based Dependency Injection** 基于构造函数依赖注入 1. 基于构造器参数列表类型创建(不建议使用) ~~~xml ~~~ 2. 基于构造器参数列表下标创建 ~~~xml ~~~ 3. 基于构造器参数名称创建 ~~~xml ~~~ > ##### Setter-based Dependency Injection 基于set方法依赖注入 ~~~xml ~~~ # 3、Spring配置 ## 3.1、import导入 作用:将多个xml合并为一个xml,对应注解@Import的功能 ## 3.2、 # 4、依赖注入 ## 4.1、构造器注入:参考目录2 ## 4.2、set方法注入(**部分xml**) ~~~ 篮球 足球 电影 ~~~ ## 4.3、 扩展注入 ​ 4.3.1、p-namespace: ​ 4.3.2、c-namespace: p和c分别是property和constructor的简称,这两种扩展注入可以理解为是一种语法糖,减少了代码量。同时,这两种扩展注入需要引入 XSD schema。 # 5、 Bean Scopes bean作用域 | Scope | Description | | :----------------------------------------------------------- | :----------------------------------------------------------- | | [singleton](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes-singleton) | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. | | [prototype](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes-prototype) | Scopes a single bean definition to any number of object instances. | | [request](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes-request) | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring `ApplicationContext`. | | [session](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes-session) | Scopes a single bean definition to the lifecycle of an HTTP `Session`. Only valid in the context of a web-aware Spring `ApplicationContext`. | | [application](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes-application) | Scopes a single bean definition to the lifecycle of a `ServletContext`. Only valid in the context of a web-aware Spring `ApplicationContext`. | | [websocket](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#websocket-stomp-websocket-scope) | Scopes a single bean definition to the lifecycle of a `WebSocket`. Only valid in the context of a web-aware Spring `ApplicationContext`. | ~~~ ~~~ # 6、自动装配 //todo # 7、注解开发 # 8、代理模式 代理模式是spring的AOP的核心实现模式 ## 8.1、静态代理 代理模式中可以分为四个角色: - 抽象角色:接口或抽象类,里面有核心的业务 ~~~ /** * 租房-代理模式中的抽象角色 * * @author Mojo * copyright (C), 2013-2021, 广州九尾信息科技有限公司 */ public interface Rent { //核心操作 void rent(); } ~~~ - 真实角色:被代理的角色 ~~~ /** * 房东-代理模式中的真实角色 * * @author Mojo * copyright (C), 2013-2021, 广州九尾信息科技有限公司 */public class Landlord implements Rent { @Override public void rent() { System.out.println("房东要出租房屋!"); }} ~~~ - 代理角色:代理上面的真实角色,同时可以做一些附属操作 ~~~ /** * 中介-代理模式中的代理角色 * * @author Mojo * copyright (C), 2013-2021, 广州九尾信息科技有限公司 */public class Proxy implements Rent { private Landlord landlord; public Proxy(Landlord landlord) { this.landlord = landlord; } @Override public void rent() { landlord.rent(); contract(); } //代理角色可做一些附属操作 public void contract() { System.out.println("中介帮忙签合同"); }} ~~~ - 客户角色(调用方): ~~~ /** * 客户端-代理模式中的调用方 * * @author Mojo * copyright (C), 2013-2021, 广州九尾信息科技有限公司 */public class Client { public static void main(String[] args) { Landlord landlord = new Landlord(); Proxy MyProxy = new Proxy(landlord); MyProxy.rent(); }} ~~~ 优点: 缺点:一个真实角色就会产生一个代理角色,代码量成倍增加 ## 8.2、动态代理 动态代理分类: 1、基于jdk的动态代理 ~~~ public class ProxyInvocationHandler implements InvocationHandler { //真实角色 private Object t; public void setT(Object t) { this.t = t; } //生成获取代理类(代理角色) public Object getProxy() { return Proxy.newProxyInstance(this.getClass().getClassLoader(), //获取真实角色的接口数组 t.getClass().getInterfaces(), this); } @Override public Object invoke(Object MyProxy, Method method, Object[] args) throws Throwable { String name = method.getName(); log(name); return method.invoke(t, args); } public void log(String msg) { System.out.println("使用" + msg + "方法"); }} ~~~ 调用 ~~~ public class Client { public static void main(String[] args) { //真实角色 Rent landlord = new Landlord(); ProxyInvocationHandler invocationHandler = new ProxyInvocationHandler(); invocationHandler.setT(landlord); //获取代理角色,基于jdk的动态代理必须用接口来接受 Rent MyProxy = (Rent) invocationHandler.getProxy(); MyProxy.rent(); }} ~~~ # 9、AOP ## 9.1、实现方式一:原生spring api方式 ~~~ public class Log implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(method.getName() + "方法执行前日志"); }} ~~~ ~~~ public class AfterLog implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行了" + method.getName() + "方法,方法返回类型为" + method.getReturnType() + ",返回" + returnValue); }} ~~~ ~~~ ~~~ ![](C:\Users\masqu\AppData\Roaming\Typora\typora-user-images\image-20210423002338014.png) ## 9.2 # 10、事务管理 ## 10.1、事务 数据库事务是一个被视为单一工作单元的操作序列。它要么全部完整执行,要不全部不执行。 ## 10.2、事务的属性 事务有四大属性 - 原子性:事务是最小的执行单元 - 一致性: - 隔离性: - 持久性:当一个事务执行成功后,执行结果是永久性的。 ## 10.3、spring事务传播行为 TransactionDefinition.PROPAGATION_REQUIRED TransactionDefinition.PROPAGATION_SUPPORTS TransactionDefinition.PROPAGATION_MANDATORY TransactionDefinition.PROPAGATION_REQUIRES_NEW TransactionDefinition.PROPAGATION_NOT_SUPPORTED TransactionDefinition.PROPAGATION_NEVER TransactionDefinition.PROPAGATION_NESTED ## 10.4 sprig事务管理类型 - 编程式事务 1. 编程式事务需要事务管理器 transactionManager 2. 定义transactionDefinition 2. 通过transactionManager,transactionDefinition获取transactionStatus - 声明式事务