Spring框架之面向切面编程AOP(2.0)

面向切面编程AOP之四大通知

实现前置通知的参数传递:

在这里插入图片描述
1,定义MemberServiceImpl类:

package cn.mldn.service.impl;
import org.springframework.stereotype.Service;
import cn.mldn.service.IMemberService;
@Service
public class MemberServiceImpl implements IMemberService {
	@Override
	public boolean delete(String mid) throws Exception {
		System.out.println("********* 【MemberService】数据删除处理。");
		return false;
	}
}

2,修改applicationContext.xml类:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.1.xsd">
	<context:annotation-config />
	<context:component-scan base-package="cn.mldn" />
	<!-- 如果要想让切面控制有效,那么必须将此类的对象交由Spring进行管理 -->
	<bean id="serviceProxy" class="cn.mldn.aop.ServiceProxy"/>
	<!-- 要求进入到AOP的配置,重点配置切入点,以及确定切入的处理操作方法 -->
	<aop:config>
		<aop:pointcut expression="execution(* cn.mldn.service..*.*(..)) and args(id)" id="pointCut"/>
		<aop:aspect ref="serviceProxy">	<!-- 进行切面的配置,如果是切面控制应该有一个控制的类 -->
			<!-- 不同的切面处理操作可以使用同一个切入点,或者每一个操作来定义属于自己的切入点 -->
			<aop:before method="beforeMethod" pointcut-ref="pointCut" arg-names="id"/>
			<aop:after method="afterMethod" pointcut="execution(* cn.mldn.service..*.*(..))"/>
		</aop:aspect>
	</aop:config>
</beans>

3,编写测试类:

package cn.mldn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.mldn.service.IMemberService;
public class TestMember {
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		IMemberService ms = ctx.getBean("memberServiceImpl",
				IMemberService.class);
		ms.delete("mldn");
	}
}

4,运行结果
在这里插入图片描述

实现返回通知:

1,定义ServiceProxy 类:

package cn.mldn.aop;
public class ServiceProxy {
	public void beforeMethod(Object obj) {
		System.out.println("####### 【ServiceProxy】public void beforeMethod(){},参数内容:" + obj);
	} 
	public void afterMethod() {
		System.out.println("####### 【ServiceProxy】public void afterMethod(){}");
	}
	public void returnMethod(Object val) {	// 处理返回值
		System.out.println("####### 【ServiceProxy】public void returnMethod(){},返回值:" + val);
	}
} 

2,修改applicationContext.xml类:

<context:annotation-config />
	<context:component-scan base-package="cn.mldn" />
	<!-- 如果要想让切面控制有效,那么必须将此类的对象交由Spring进行管理 -->
	<bean id="serviceProxy" class="cn.mldn.aop.ServiceProxy"/>
	<!-- 要求进入到AOP的配置,重点配置切入点,以及确定切入的处理操作方法 -->
	<aop:config>
		<aop:pointcut expression="execution(* cn.mldn.service..*.*(..)) and args(id)" id="pointCut"/>
		<aop:aspect ref="serviceProxy">	<!-- 进行切面的配置,如果是切面控制应该有一个控制的类 -->
			<!-- 不同的切面处理操作可以使用同一个切入点,或者每一个操作来定义属于自己的切入点 -->
			<aop:before method="beforeMethod" pointcut-ref="pointCut" arg-names="id"/>
			<aop:after method="afterMethod" pointcut="execution(* cn.mldn.service..*.*(..))"/>
			<aop:after-returning method="returnMethod" pointcut="execution(* cn.mldn.service..*.*(..))" returning="v" arg-names="v"/>
		</aop:aspect>
	</aop:config>

3,编写测试类:

package cn.mldn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.mldn.service.IMemberService;
public class TestMember {
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		IMemberService ms = ctx.getBean("memberServiceImpl",
				IMemberService.class);
		ms.delete("mldn");
	}
}

4,运行结果
在这里插入图片描述

实现异常通知:

1,定义MemberServiceImpl类:

package cn.mldn.service.impl;
import org.springframework.stereotype.Service;
import cn.mldn.service.IMemberService;
@Service
public class MemberServiceImpl implements IMemberService {
	@Override
	public boolean delete(String mid) throws Exception {
//		System.out.println("********* 【MemberService】数据删除处理。");
		throw new Exception("我就是要抛异常,你怎么着?") ;
//		return false;
	}
}

2,定义ServiceProxy 类:

package cn.mldn.aop;
public class ServiceProxy {
	public void beforeMethod(Object obj) {
		System.out.println("####### 【ServiceProxy】public void beforeMethod(){},参数内容:" + obj);
	} 
	public void afterMethod() {
		System.out.println("####### 【ServiceProxy】public void afterMethod(){}");
	}
	public void returnMethod(Object val) {	// 处理返回值
		System.out.println("####### 【ServiceProxy】public void returnMethod(){},返回值:" + val);
	}
	public void throwMethod(Exception e) {	// 对异常进行处理
		System.out.println("####### 【ServiceProxy】public void throwMethod(){},异常信息:" + e);
	} 
} 

3,修改applicationContext.xml类:

<context:annotation-config />
	<context:component-scan base-package="cn.mldn" />
	<!-- 如果要想让切面控制有效,那么必须将此类的对象交由Spring进行管理 -->
	<bean id="serviceProxy" class="cn.mldn.aop.ServiceProxy"/>
	<!-- 要求进入到AOP的配置,重点配置切入点,以及确定切入的处理操作方法 -->
	<aop:config>
		<aop:pointcut expression="execution(* cn.mldn.service..*.*(..)) and args(id)" id="pointCut"/>
		<aop:aspect ref="serviceProxy">	<!-- 进行切面的配置,如果是切面控制应该有一个控制的类 -->
			<!-- 不同的切面处理操作可以使用同一个切入点,或者每一个操作来定义属于自己的切入点 -->
			<aop:before method="beforeMethod" pointcut-ref="pointCut" arg-names="id"/>
			<aop:after method="afterMethod" pointcut="execution(* cn.mldn.service..*.*(..))"/>
			<aop:after-returning method="returnMethod" pointcut="execution(* cn.mldn.service..*.*(..))" returning="v" arg-names="v"/>
			<aop:after-throwing method="throwMethod" pointcut="execution(* cn.mldn.service..*.*(..))" throwing="e" arg-names="e"/>
		</aop:aspect>
	</aop:config>

4,编写测试类:

package cn.mldn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.mldn.service.IMemberService;
public class TestMember {
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		IMemberService ms = ctx.getBean("memberServiceImpl",
				IMemberService.class);
		ms.delete("mldn");
	}
}

5,运行结果:
在这里插入图片描述

实现环绕通知:

1,定义MemberServiceImpl类:

package cn.mldn.service.impl;
import org.springframework.stereotype.Service;
import cn.mldn.service.IMemberService;
@Service
public class MemberServiceImpl implements IMemberService {
	@Override
	public boolean delete(String mid) throws Exception {
		System.out.println("********* 【MemberService】数据删除处理。");
//		throw new Exception("我就是要抛异常,你怎么着?") ;
		return false;
	}
}

2,定义ServiceProxy 类:

package cn.mldn.aop;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
public class ServiceProxy {
	public void beforeMethod(Object obj) {
		System.out.println("####### 【ServiceProxy】public void beforeMethod(){},参数内容:" + obj);
	} 
	public void afterMethod() {
		System.out.println("####### 【ServiceProxy】public void afterMethod(){}");
	}
	public void returnMethod(Object val) {	// 处理返回值
		System.out.println("####### 【ServiceProxy】public void returnMethod(){},返回值:" + val);
	}
	public void throwMethod(Exception e) {	// 对异常进行处理
		System.out.println("####### 【ServiceProxy】public void throwMethod(){},异常信息:" + e);
	} 
	public Object aroundMethod(ProceedingJoinPoint point) throws Throwable {	// 调用具体的执行方法
		System.out.println("@@@@@ 【环绕通知】aroundMethod() - before,参数:" + Arrays.toString(point.getArgs()));	// 取得所有传递过来的参数
		// 此时可以针对于参数接收后处理后再传递的操作
		Object obj = point.proceed(new Object [] {"abc"}) ;	// 自己来处理内容
		System.out.println("@@@@@ 【环绕通知】aroundMethod() - after,返回结果:" + obj);
		return true ;	// 不按照结果返回数据
	}
} 

3,修改applicationContext.xml类:

<context:annotation-config />
	<context:component-scan base-package="cn.mldn" />
	<!-- 如果要想让切面控制有效,那么必须将此类的对象交由Spring进行管理 -->
	<bean id="serviceProxy" class="cn.mldn.aop.ServiceProxy"/>
	<!-- 要求进入到AOP的配置,重点配置切入点,以及确定切入的处理操作方法 -->
	<aop:config>
		<aop:pointcut expression="execution(* cn.mldn.service..*.*(..)) and args(id)" id="pointCut"/>
		<aop:aspect ref="serviceProxy">	<!-- 进行切面的配置,如果是切面控制应该有一个控制的类 -->
			<!-- 不同的切面处理操作可以使用同一个切入点,或者每一个操作来定义属于自己的切入点 -->
			<aop:before method="beforeMethod" pointcut-ref="pointCut" arg-names="id"/>
			<aop:after method="afterMethod" pointcut="execution(* cn.mldn.service..*.*(..))"/>
			<aop:after-returning method="returnMethod" pointcut="execution(* cn.mldn.service..*.*(..))" returning="v" arg-names="v"/>
			<aop:after-throwing method="throwMethod" pointcut="execution(* cn.mldn.service..*.*(..))" throwing="e" arg-names="e"/>
			<aop:around method="aroundMethod" pointcut="execution(* cn.mldn.service..*.*(..))"/>
		</aop:aspect>
	</aop:config>

4,编写测试类:

package cn.mldn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.mldn.service.IMemberService;
public class TestMember {
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		IMemberService ms = ctx.getBean("memberServiceImpl",
				IMemberService.class);
		ms.delete("mldn");
	}
}

5,运行结果:
在这里插入图片描述

总结:

Spring 中提供的AOP总体来讲,就属于一个超级代理类,这个代理类的配置要比直接使用Java动态代理,包括CGLB代理都要简单;
AOP主要是用来做事务控制;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值