spring Aop基础

一:为什么需要AOP

1.当我们需求是:在程序执行期间追踪正在发生的活动,也就是打印日志。 

2.创建一个接口ArtithmeticCalculator并实现

package com.dhx.spring.aop.helloword;

public interface ArtithmeticCalculator {
	 //加法运算
	 int add(int i,int j);
	 //减法运算
	 int sub(int i,int j);
	 //乘法运算
	 int mul(int i,int j);
	 //除法运算
	 int div(int i,int j);

}
package com.dhx.spring.aop.helloword;

public class ArtithmeticCalculatorLoggingImpl implements ArtithmeticCalculator {

	@Override
	public int add(int i, int j) {
		System.out.println("the method add begin with["+i+","+j+"]");
		
		int result=i+j;
		System.out.println("the method add ends with"+result);
		
		return result;
	}

	@Override
	public int sub(int i, int j) {
		System.out.println("the method sub begin with["+i+","+j+"]");
		
		int result=i-j;
		System.out.println("the method sub ends with"+result);
		
		return result;
	}

	@Override
	public int mul(int i, int j) {
		System.out.println("the method mul begin with["+i+","+j+"]");
		
		int result=i*j;
		System.out.println("the method mul ends with"+result);
		
		return result;
	}

	@Override
	public int div(int i, int j) {
		System.out.println("the method div begin with["+i+","+j+"]");
		
		int result=i/j;
		System.out.println("the method div ends with"+result);
		
		return result;
	}

}

3.虽然这样也能实现我们的需求,但是还是有很多缺点

二:如何解决上述问题——使用动态代理

1.代理设计模式的原理:使用一个代理将对象包装起来,然后用该代理对象取代原始对象,任何对原始对象的调用都有通过代理,代理对象决定是否以及何时将方法调用到原始对象上。

2.创建 ArtithmeticCalculatorImpl类实现ArtithmeticCalculator接口

  1. 代码混乱:越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀,每个方法在处理核心业务逻辑的同时还必须兼顾其他多个关注点。
  2. 代码分散:以日志需求为例,只是为了满足这个单一需求,就不得不在多个模块(方法)里重复相同的日志代码,如果日志需求发生变化,必须修改所有模块。
package com.dhx.spring.aop.helloword;

public class ArtithmeticCalculatorImpl implements ArtithmeticCalculator {

	@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;
	}

}

3.创建一个代理类ArtithmeticCalculatorLoggingProxy

package com.dhx.spring.aop.helloword;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class ArtithmeticCalculatorLoggingProxy {
	
	//要代理的对象
	ArtithmeticCalculator target;
	
	public ArtithmeticCalculatorLoggingProxy(ArtithmeticCalculator target) {
		this.target = target;
	}

	public ArtithmeticCalculator getLogiingProxy() {
		ArtithmeticCalculator proxy=null;
		
		//代理对象由哪一个类加载器负责加载
		ClassLoader loader=target.getClass().getClassLoader();
		
		//代理对象的类型,即其中有哪些方法
		Class[] interfaces=new  Class[] {ArtithmeticCalculator.class};
		//当调用代理对象其中的方法时,该执行的代码
		InvocationHandler  h=new InvocationHandler() {
			/*
			 * proxy:正在返回的那个代理对象,一般情况下,在invoke方法中都不使用该对象
			 * method:正在被调用的方法。
			 * args:调用方法时,传入的参数
			 */
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				//方法名
				String menthodName=method.getName();
				//日志
				System.out.println("guigu->the method "+menthodName+"begins with"+Arrays.asList(args));
				//执行方法
				Object result =method.invoke(target, args);
				//日志
				System.out.println("guigu->the method "+menthodName+"begins with "+result);		
				return result;
			}
		};
		
		proxy=(ArtithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
		
		return proxy;
	}

}

4.测试

package com.dhx.spring.aop.helloword;

public class Main {

	public static void main(String[] args) {
			
		
		ArtithmeticCalculator target=new ArtithmeticCalculatorImpl();
		
		ArtithmeticCalculator proxy=(ArtithmeticCalculator) new ArtithmeticCalculatorLoggingProxy(target).getLogiingProxy();
		
		
		int result = proxy.add(2, 3);
		System.out.println(result);
		
		result=proxy.sub(9, 6);
		System.out.println(result);
	}

}

三:AOP简介

1.AOP(Aspect -Oriented Programming,面向切面编程):是一种新的方法论,是对传统OOP(Object-Oriented Programming,面向对象编程)的补充。

2.AOP的主要编程对象是切面(aspect),而切面是模块化横切关注点。

3.在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能在哪里,以什么方式应用,并且不必修改受影响的类,这样一来横切关注点就被模块化到特殊的对象(切面)里。

4.AOP的好处

  • 每个事物逻辑位于一个位置,代码不分散,便于维护和升级
  • 业务模块更简洁,只包含核心业务代码。

五:AOP术语

 1.切面(aspect):横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象

2.通知(Advice):切面要完成的工作。

3.目标:被通知的对象。

4.代理(Proxy):向目标对象应用通知之后创建的对象。

5.连接点(JoinPoint):程序执行的某个特定位置,如类某个方法调用前,调用后,方法抛出异常等,连接点有两个信息确定。方法表示的程序执行点,相对点表示的方位,例如ArtithmeticCalculator#add()方法执行前的连接点执行点为ArtithmeticCalculator#add(),方位为该方法执行前的位置

6.切点(pointcut):每个类都拥有多个连接点,例如ArtithmeticCalculator的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务,AOP通过切点定位到特定的连接点,类比:连接点相等于数据库中的记录,切点相当于查询条件,切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过org.springframework.aop.Pointcut接口进行描述,它使用类和方法作为连接点的查询条件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值