Spring_AOP

目录

1、概念

2、专业术语,概念

3、编码

①前置通知

②后置通知

③环绕通知

④异常通知

⑤过滤通知


1、概念

AOP:面向切面编程。

在很多时候我们都会将很多有相同代码的方法或者类进行向上抽取,节省代码提高开发效率。

其实AOP也是如此,那么为什么叫做面向切面编程呢?我们很多方法在执行前都要开启事务结束后

要关闭事务,我们是将一个方法里面的前后给提取出去,最后我们仅关注业务代码!!!

最终执行的方法在业务代码执行前后都会执行事务,可以把最终执行的方法看做代理帮我们增强的

方法,代理帮我们把方法切开然后把通知(开启事务,关闭事务这些...)放到连接点到目标上!

这里的代码向上提取方法是非常像是将方法切开,所以能称作面向切面!!

2、专业术语,概念

连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出

目标(Target):被通知(被代理)的对象(完成具体的业务逻辑)

通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)

代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知)。只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的

切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。 (也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)

适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut) 

3、编码

①前置通知

业务层接口

IBookBiz.java

package com.zwf.biz;
 
public interface IBookBiz {
	/**
     * 购买书籍
     */
	public boolean buy(String userName, String bookName, Double price);
 
	/**
     * 发表书评
     */
	public void comment(String userName, String comments);
}

实现类(目标对象)

package com.zwf.biz.impl;
 
import com.zwf.Advice.PriceException;
import com.zwf.biz.IBookBiz;
 
public class BookBizImpl implements IBookBiz {
 
	public BookBizImpl() {
		super();
	}
 
	public boolean buy(String userName, String bookName, Double price) {
		// 通过控制台的输出方式模拟购书
		if (null == price || price <= 0) {
			throw new PriceException("book price exception");
		}
		System.out.println(userName + " buy " + bookName + ", spend " + price);
		return true;
	}
 
	public void comment(String userName, String comments) {
		// 通过控制台的输出方式模拟发表书评
		System.out.println(userName + " say:" + comments);
	}
 
}

前置通知实现类

 package com.zwf.Advice;
 
import java.lang.reflect.Method;
import java.util.Arrays;
 
import org.springframework.aop.MethodBeforeAdvice;
/**
 *前置通知
 *调用项目中的某一接口实现类的方式时,将类名.方法名,携带的参数,作为日志存储到数据库。
 * @author T440s
 *
 */
public class methodBeforeAdvice implements MethodBeforeAdvice {
	
	/**
     * method: 被触发的目标对象的方法
     *   args: 方法参数
     * target:目标对象
     */
	public void before(Method method, Object[] args, Object target) throws Throwable {
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(args);
		String msg = "【系统日志】:正在调用->"+targetName+"."+methodName+",携带的参数:"+params;
		System.out.println(msg);
 
	}
 
}

配置:

<!-- 目标对象 -->
	<bean id="BookBiz" class="com.zwf.biz.impl.BookBizImpl"></bean>
	<!-- 前置通知 -->
	<bean id="myMethodBeforeAdvice" class="com.zwf.Advice.methodBeforeAdvice"></bean>
	<!-- 生成代理(目标对象+通知) -->
	<bean class="org.springframework.aop.framework.ProxyFactoryBean"
		id="FactoryBean">
		<property name="target" ref="BookBiz"></property>
		<!-- 代理工厂生产的代理需要实现的接口列表 -->
		<property name="proxyInterfaces">
			<list>
				<value>com.zwf.biz.IBookBiz</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>myMethodBeforeAdvice</value>
			</list>
		</property>
	</bean>

测试:

package com.zwf.text;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.zwf.biz.IBookBiz;
 
 
/**
 *  Spring方式的调用
 * @author T440s
 *
 */
public class text {
public static void main(String[] args) throws Exception {
	ApplicationContext  applicationContext  = new ClassPathXmlApplicationContext("/spring-context.xml");//解析Spring-context.xml获得模型
	IBookBiz bean = (IBookBiz) applicationContext.getBean("FactoryBean");
	bean.buy("java编程思想", "程序员",45);
	bean.comment("java编程思想", "详细全面");
}
}

这里用接口声明,而不是具体实现类声明,代理对象不是目标类,这里拿到的是代理,通过代理

来执行代理后的方法。

②后置通知

package com.zwf.Advice;
 
import java.lang.reflect.Method;
import java.util.Arrays;
 
import org.springframework.aop.AfterReturningAdvice;
/**
 * 后置通知
 * @author T440s
 *
 */
public class aftereturniAdvice implements  AfterReturningAdvice{
 
	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(args);
		String msg = "【返利通知:返利3元】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params + ";目标对象所调用的方法的返回值:"
				+ returnValue;
		System.out.println(msg);
		
	}
 
}

配置

<!-- 目标对象 -->
	<bean id="BookBiz" class="com.zwf.biz.impl.BookBizImpl"></bean>
	<!-- 前置通知 -->
	<bean id="myMethodBeforeAdvice" class="com.zwf.Advice.methodBeforeAdvice"></bean>
	<!-- 后置通知 -->
	<bean id="myAfterReturningAdvice"  class="com.zwf.Advice.aftereturniAdvice"></bean>
	<!-- 生成代理(目标对象+通知) -->
	<bean class="org.springframework.aop.framework.ProxyFactoryBean"
		id="FactoryBean">
		<property name="target" ref="BookBiz"></property>
		<!-- 代理工厂生产的代理需要实现的接口列表 -->
		<property name="proxyInterfaces">
			<list>
				<value>com.zwf.biz.IBookBiz</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>myMethodBeforeAdvice</value>
				<value>myAfterReturningAdvice</value>
			</list>
		</property>
	</bean>

③环绕通知

package com.zwf.Advice;
 
import java.lang.reflect.Method;
import java.util.Arrays;
 
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
 * 环绕通知
 * @author T440s
 *
 */
public class methodInterceptor implements MethodInterceptor  {
 
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object target = invocation.getThis();
		 Method method = invocation.getMethod();
		Object[] args = invocation.getArguments();
		// a.jsp window.open(b.jsp)
		// b.jsp xxx->返回object->b.jsp window.close();window.getArguments;
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(args);
		String msg = "【环绕通知】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params;
		System.out.println(msg);
		Object returnValue = invocation.proceed();
		String msg2 = "【环绕通知】:目标对象所调用的方法的返回值:" + returnValue;
		System.out.println(msg2);
		return returnValue;
	}
 
}

配置

<!-- 目标对象 -->
	<bean id="BookBiz" class="com.zwf.biz.impl.BookBizImpl"></bean>
	<!-- 前置通知 -->
	<bean id="myMethodBeforeAdvice" class="com.zwf.Advice.methodBeforeAdvice"></bean>
	<!-- 后置通知 -->
	<bean id="myAfterReturningAdvice"  class="com.zwf.Advice.aftereturniAdvice"></bean>
	<!-- 环绕通知 -->
	<bean  id="myMethodInterceptor"  class="com.zwf.Advice.methodInterceptor"></bean>
	<!-- 生成代理(目标对象+通知) -->
	<bean class="org.springframework.aop.framework.ProxyFactoryBean"
		id="FactoryBean">
		<property name="target" ref="BookBiz"></property>
		<!-- 代理工厂生产的代理需要实现的接口列表 -->
		<property name="proxyInterfaces">
			<list>
				<value>com.zwf.biz.IBookBiz</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>myMethodBeforeAdvice</value>
				<value>myAfterReturningAdvice</value>
				<value>myMethodInterceptor</value>
			</list>
		</property>
	</bean>

④异常通知

有些业务涉及到了很几张表的数据改变,如果在中途操作一张表时报错,那么之前所改变的数据需要恢复到业务开始前,即回滚。

价钱异常处理类

package com.zwf.Advice;
 
 
public class PriceException extends RuntimeException {
 
	public PriceException() {
		super();
	}
 
	public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
	}
 
	public PriceException(String message, Throwable cause) {
		super(message, cause);
	}
 
	public PriceException(String message) {
		super(message);
	}
 
	public PriceException(Throwable cause) {
		super(cause);
	}
	
}

通知类

package com.zwf.Advice;
 
import org.springframework.aop.ThrowsAdvice;
 
public class throwsAdvice implements ThrowsAdvice  {
 
	public void afterThrowing( PriceException ex ) {
		/**
		 * 项目实践中这里是数据库数据回滚代码
		 */
		System.out.println("价格输入有误,购买失败,请重新输入!!!");
	}
}

配置

<!-- 目标对象 -->
	<bean id="BookBiz" class="com.zwf.biz.impl.BookBizImpl"></bean>
	<!-- 前置通知 -->
	<bean id="myMethodBeforeAdvice" class="com.zwf.Advice.methodBeforeAdvice"></bean>
	<!-- 后置通知 -->
	<bean id="myAfterReturningAdvice"  class="com.zwf.Advice.aftereturniAdvice"></bean>
	<!-- 环绕通知 -->
	<bean  id="myMethodInterceptor"  class="com.zwf.Advice.methodInterceptor"></bean>
	<!-- 异常通知 -->
	<bean id="myThrowsAdvice" class="com.zwf.Advice.throwsAdvice"></bean>
	<!-- 生成代理(目标对象+通知) -->
	<bean class="org.springframework.aop.framework.ProxyFactoryBean"
		id="FactoryBean">
		<property name="target" ref="BookBiz"></property>
		<!-- 代理工厂生产的代理需要实现的接口列表 -->
		<property name="proxyInterfaces">
			<list>
				<value>com.zwf.biz.IBookBiz</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>myMethodBeforeAdvice</value>
				<value>myAfterReturningAdvice</value>
				<value>myMethodInterceptor</value>
				<value>myThrowsAdvice</value>
			</list>
		</property>
	</bean>

⑤过滤通知

对目标的通知进行过滤,并不是所有的方法都是一样通知!!!比如只有*buy方法才会有后置通知(返利)...

配置

<!-- 目标对象 -->
	<bean id="BookBiz" class="com.zwf.biz.impl.BookBizImpl"></bean>
	<!-- 前置通知 -->
	<bean id="myMethodBeforeAdvice" class="com.zwf.Advice.methodBeforeAdvice"></bean>
	<!-- 后置通知 -->
	<bean id="myAfterReturningAdvice"  class="com.zwf.Advice.aftereturniAdvice"></bean>
	<!-- 环绕通知 -->
	<bean  id="myMethodInterceptor"  class="com.zwf.Advice.methodInterceptor"></bean>
	<!-- 异常通知 -->
	<bean id="myThrowsAdvice" class="com.zwf.Advice.throwsAdvice"></bean>
	<!-- 过滤通知 -->
	<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
		id="myAfterReturningAdvice2">
		<property name="advice" ref="myAfterReturningAdvice"></property>
		<!-- .*buy:[.*]任意字符0~n个,以buy结尾的方法 -->
		<!-- <property name="pattern" value=".*buy"></property> -->
		<property name="patterns">
			<list>
				<value>.*buy</value>
			</list>
		</property>
	</bean>
	<!-- 生成代理(目标对象+通知) -->
	<bean class="org.springframework.aop.framework.ProxyFactoryBean"
		id="FactoryBean">
		<property name="target" ref="BookBiz"></property>
		<!-- 代理工厂生产的代理需要实现的接口列表 -->
		<property name="proxyInterfaces">
			<list>
				<value>com.zwf.biz.IBookBiz</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>myMethodBeforeAdvice</value>
				<!-- 将之前的后置通知替换 -->
				<value>myAfterReturningAdvice2</value>
			<!-- 	<value>myAfterReturningAdvice</value> -->
				<value>myMethodInterceptor</value>
				<value>myThrowsAdvice</value>
			</list>
		</property>
	</bean>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张同学_java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值