spring Aspectj入门

建议先看一下:
1.代理模式 https://blog.csdn.net/dengjili/article/details/82802614
2.spring aop入门 和 代理模式的衔接 https://blog.csdn.net/dengjili/article/details/82804163

在Aspectj 需要引入包

aspectjweaver
aspectjrt

下载相关代码,可运行

相关代码
https://github.com/dengjili/springaop

注解方式

实际拦截类

package priv.dengjl.aspectj.impl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class ServiceAspectj {
	
	@Before("execution(* priv.dengjl.springaop.service.*.printName(..))")
	public void logBefore(JoinPoint point) {

		System.out.println("Before() 正在运行!");
		System.out.println("当前方法 : " + point.getSignature().getName());
	}
	

	@After("execution(* priv.dengjl.springaop.service.*.printURL(..))")
	public void beforeAdvice() {
		System.out.println("after() 正在运行!");
	}
	
	@AfterThrowing(pointcut = "execution(* priv.dengjl.springaop.service.*.printThrowException(..))", throwing= "error")
	public void afterThrowing(Throwable error) {
		System.out.println("异常返回信息:" + error.getMessage());
	}
	
	@AfterReturning(pointcut = "execution(* priv.dengjl.springaop.service.*.returnValue(..))", returning="result")
	public void afterReturning(JoinPoint joinPoint,Object result) {
		System.out.println("返回值信息:" + result);
	}
	
	@Around("execution(* priv.dengjl.springaop.service.*.testAroud(..))")
	public void testAround(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("前");
		joinPoint.proceed();
		System.out.println("后");
	}
}

对应配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

	<aop:aspectj-autoproxy />

	<!-- 基础bean -->
	<bean id="testService" class="priv.dengjl.springaop.service.TestService">
		<property name="name" value="呵呵哒" />
		<property name="url" value="www.mmp.com" />
	</bean>

	<!-- 开启aspectj方式 -->
	<aop:aspectj-autoproxy />
	
	<!-- Aspect -->
	<bean id="testAspect" class="priv.dengjl.aspectj.impl.ServiceAspectj" />

</beans>

测试类

package priv.dengjl.aspectj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import priv.dengjl.springaop.service.TestService;

public class App {

	public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
				new String[] { "applicationContextAspectj.xml" });

    	TestService cust = (TestService) appContext.getBean("testService");

		System.out.println("*************************");
		cust.printName();
		System.out.println("*************************");
		cust.printURL();
		System.out.println("*************************");
		try {
			cust.printThrowException();
		} catch (Exception e) {
			System.out.println("有异常发生");
			System.out.println("*************************");
		}
		cust.returnValue();
		
		cust.testAroud();
	}

}

测试结果

*************************
Before() 正在运行!
当前方法 : printName
名称 : 呵呵哒
*************************
网址 : www.mmp.com
after() 正在运行!
*************************
异常返回信息:1234
有异常发生
*************************
返回值信息:abc
前
11111111111144444444444
后

xml配置方法

实际拦截类

package priv.dengjl.aspectj.impl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class ServiceAspectjXml {
	
	public void logBefore(JoinPoint point) {

		System.out.println("Before() 正在运行!");
		System.out.println("当前方法 : " + point.getSignature().getName());
	}
	

	public void afterAdvice() {
		System.out.println("after() 正在运行!");
	}
	
	public void afterThrowing(Throwable error) {
		System.out.println("异常返回信息:" + error.getMessage());
	}
	
	public void afterReturning(JoinPoint joinPoint,Object result) {
		System.out.println("返回值信息:" + result);
	}
	
	public void testAround(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("前");
		joinPoint.proceed();
		System.out.println("后");
	}
}

配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

	<aop:aspectj-autoproxy />

	<!-- 基础bean -->
	<bean id="testService" class="priv.dengjl.springaop.service.TestService">
		<property name="name" value="呵呵哒" />
		<property name="url" value="www.mmp.com" />
	</bean>

	<!-- 开启aspectj方式 -->
	<aop:aspectj-autoproxy />

	<!-- Aspect -->
	<bean id="testAspect" class="priv.dengjl.aspectj.impl.ServiceAspectjXml" />

	<aop:config>
		<aop:aspect id="myAspect" ref="testAspect">
			<aop:pointcut id="pointCutBefore"
				expression="execution(* priv.dengjl.springaop.service.*.printName(..))" />
			<aop:pointcut id="pointCutAfter"
				expression="execution(* priv.dengjl.springaop.service.*.printURL(..))" />
			<aop:pointcut id="pointCutAfterReturning"
				expression="execution(* priv.dengjl.springaop.service.*.returnValue(..))" />
			<aop:pointcut id="pointCutAfterThrowing"
				expression="execution(* priv.dengjl.springaop.service.*.printThrowException(..))" />
			<aop:pointcut id="pointCutAround"
				expression="execution(* priv.dengjl.springaop.service.*.testAroud(..))" />


			<!-- @Before -->
			<aop:before method="logBefore" pointcut-ref="pointCutBefore" />

			<!-- @After -->
			<aop:after method="afterAdvice" pointcut-ref="pointCutAfter" />

			<!-- @AfterReturning -->
			<aop:after-returning method="afterReturning"
				returning="result" pointcut-ref="pointCutAfterReturning" />
			<!-- @AfterThrowing -->
			<aop:after-throwing method="afterThrowing"
				throwing="error" pointcut-ref="pointCutAfterThrowing" />
			<!-- @Around -->
			<aop:around method="testAround" pointcut-ref="pointCutAround" />

		</aop:aspect>

	</aop:config>

</beans>

测试类

package priv.dengjl.aspectj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import priv.dengjl.springaop.service.TestService;

public class AppXml {

	public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
				new String[] { "applicationContextAspectjXml.xml" });

    	TestService cust = (TestService) appContext.getBean("testService");

		System.out.println("*************************");
		cust.printName();
		System.out.println("*************************");
		cust.printURL();
		System.out.println("*************************");
		try {
			cust.printThrowException();
		} catch (Exception e) {
			System.out.println("有异常发生");
			System.out.println("*************************");
		}
		cust.returnValue();
		
		cust.testAroud();
	}

}

测试结果

*************************
Before() 正在运行!
当前方法 : printName
名称 : 呵呵哒
*************************
网址 : www.mmp.com
after() 正在运行!
*************************
异常返回信息:1234
有异常发生
*************************
返回值信息:abc
前
11111111111144444444444
后

AspectJ配置表达式

AspectJ最强大的地方在于他的切入点表达式:

语法:execution(修饰符 返回值 包.类.方法名(参数) throws异常)

描述取值
修饰符一般省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常)
修饰符public 公共方法
修饰符* 任意
返回值不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常)
返回值void 返回没有值
返回值String 返回值字符串
返回值* 任意
不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常)
priv.dengjl.service  固定包
priv.dengjl.oa.*.service oa包下面子包 (例如:priv.dengjl.oa.flow.service)
priv.dengjl.oa…   oa包下面的所有子包(含自己)
priv.dengjl.oa.*.service… oa包下面任意子包,固定目录service,service目录任意包
不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常)
UserServiceImpl 指定类
*Impl 以Impl结尾
User* 以User开头
* 任意
方法名不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常)
方法名addUser 固定方法
方法名add* 以add开头
方法名*Do 以Do结尾
方法名* 任意
参数不能省略,execution(修饰符 返回值 包.类.方法名(参数) throws异常)
参数() 无参
参数(int) 一个整型
参数(int ,int) 两个
参数(…) 参数任意

throws | 可省略,一般不写。,execution(修饰符 返回值 包.类.方法名(参数) throws异常)

详细的表达式用法,查看专业文档。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值