Spring AOP
面向切面编程(动态代理)
动态代理实现:
-
先定义接口类
package bh.shy.spring.aop; public interface ArithmeticCalculator { public int add(int i,int j); public int sub(int i,int j); public int mul(int i,int j); public int div(int i,int j); }
-
定义实现类
package bh.shy.spring.aop; public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; }
}
-
定义代理对象
package bh.shy.spring.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; //代理类 public class ArithmeticCalculatorLoggingProxy{ //1.被代理的对象,目标对象 private ArithmeticCalculator target; //通过构造器的方式讲目标对象传入 public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target){ this.target = target; } //获取代理对象 public ArithmeticCalculator getLoggingProxy(){ /** * loader:ClassLoader类加载器 * interfaces:目标类的所有接口,目的是获取接口类中的所有方法 * h:InvocationHandler */ //定义代理对象 ArithmeticCalculator proxy; ClassLoader loader = target.getClass().getClassLoader(); Class[] interfaces = target.getClass().getInterfaces(); InvocationHandler h = new InvocationHandler() { /** * proxy:代理对象 在invoke方法中,一般不会用 * method:正在调用的方法 * args:调用方法传入的参数 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); System.out.println("方法开始 this is method name :"+methodName+"..."+Arrays.asList(args)); Object result = method.invoke(target, args); System.out.println("方法结束"+Arrays.asList(args)); return result; } }; proxy = (ArithmeticCalculator)Proxy.newProxyInstance(loader, interfaces, h); return proxy; } }
-
测试
package bh.shy.spring.aop; public class Main { public static void main(String[] args) { ArithmeticCalculator target = new ArithmeticCalculatorLogginImpl(); //获取代理 ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy(); int result = proxy.add(1, 1); } }
AOP 简介
-
AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充.
-
AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点.
-
在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
-
AOP 的好处:
-
每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级
-
业务模块更简洁, 只包含核心业务代码.
AOP思想
AOP 术语
-
切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
-
通知(Advice): 切面必须要完成的工作
-
目标(Target): 被通知的对象
-
代理(Proxy): 向目标对象应用通知之后创建的对象
-
连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。例如 ArithmethicCalculator#add() 方法执行前的连接点,执行点为 ArithmethicCalculator#add(); 方位为该方法执行前的位置
-
切点(pointcut):每个类都拥有多个连接点:例如 ArithmethicCalculator 的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务。AOP 通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。
Spring AOP
-
AspectJ:Java 社区里最完整最流行的 AOP 框架.
-
在 Spring2.0 以上版本中, 可以使用基于 AspectJ 注解或基于 XML 配置的 AOP
在 Spring 中启用 AspectJ 注解支持
-
要在 Spring 应用中使用 AspectJ 注解, 必须在 classpath 下包含 AspectJ 类库: aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar
-
将 aop Schema 添加到 根元素中.
-
要在 Spring IOC 容器中启用 AspectJ 注解支持, 只要在 Bean 配置文件中定义一个空的 XML 元素 aop:aspectj-autoproxy
-
当 Spring IOC 容器侦测到 Bean 配置文件中的 aop:aspectj-autoproxy 元素时, 会自动为与 AspectJ 切面匹配的 Bean 创建代理.
用 AspectJ 注解声明切面
-
要在 Spring 中声明 AspectJ 切面, 只需要在 IOC 容器中将切面声明为 Bean 实例. 当在 Spring IOC 容器中初始化 AspectJ 切面之后, Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 Bean 创建代理.
-
在 AspectJ 注解中, 切面只是一个带有 @Aspect 注解的 Java 类.
-
通知是标注有某种注解的简单的 Java 方法.
-
AspectJ 支持 5 种类型的通知注解:
-
@Before: 前置通知, 在方法执行之前执行
-
@After: 后置通知, 在方法执行之后执行
-
@AfterRunning: 返回通知, 在方法返回结果之后执行
-
@AfterThrowing: 异常通知, 在方法抛出异常之后
-
@Around: 环绕通知, 围绕着方法执行
利用方法签名编写 AspectJ 切入点表达式
-
最典型的切入点表达式时根据方法的签名来匹配各种方法:
-
execution * com.atguigu.spring.ArithmeticCalculator.*(…): 匹配 ArithmeticCalculator 中声明的所有方法,第一个 * 代表任意修饰符及任意返回值. 第二个 * 代表任意方法. … 匹配任意数量的参数. 若目标类与接口与该切面在同一个包中, 可以省略包名.
-
execution public * ArithmeticCalculator.*(…): 匹配 ArithmeticCalculator 接口的所有公有方法.
-
execution public double ArithmeticCalculator.*(…): 匹配 ArithmeticCalculator 中返回 double 类型数值的方法
-
execution public double ArithmeticCalculator.*(double, …): 匹配第一个参数为 double 类型的方法, … 匹配任意数量任意类型的参数
-
execution public double ArithmeticCalculator.*(double, double): 匹配参数类型为 double, double 类型的方法.
合并切入点表达式
让通知访问当前连接点的细节
可以在通知方法中声明一个类型为 JoinPoint 的参数. 然后就能访问链接细节. 如方法名称和参数值.
指定切面的优先级
-
在同一个连接点上应用不止一个切面时, 除非明确指定, 否则它们的优先级是不确定的.
-
切面的优先级可以通过实现 Ordered 接口或利用 @Order 注解指定.
-
实现 Ordered 接口, getOrder() 方法的返回值越小, 优先级越高.
-
若使用 @Order 注解, 序号出现在注解中
JdbcTemplate 简介
-
为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架.
-
作为 Spring JDBC 框架的核心, JDBC 模板的设计目的是为不同类型的 JDBC 操作提供模板方法. 每个模板方法都能控制整个过程, 并允许覆盖过程中的特定任务. 通过这种方式, 可以在尽可能保留灵活性的情况下, 将数据库存取的工作量降到最低.
使用 JdbcTemplate 更新数据库(增删改)
使用 JdbcTemplate 查询数据库
简化 JDBC 模板查询
-
每次使用都创建一个 JdbcTemplate 的新实例, 这种做法效率很低下.
-
JdbcTemplate 类被设计成为线程安全的, 所以可以再 IOC 容器中声明它的单个实例, 并将这个实例注入到所有的 DAO 实例中.
-
JdbcTemplate 也利用了 Java 1.5 的特定(自动装箱, 泛型, 可变长度等)来简化开发