Spring配置实现AOP

execution(* com.igeekhome.c30.aop.*.*(..))  

第一个*表示任意返回值 ,第二个*表示任意类, 第三个*表示任意方法,(...) 表示任意参数;

applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" 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.xsd
http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
">

	<!-- 配置DAO -->
	<bean id="userDAO" class="com.igeek.crm.dao.impl.UserDAOImpl" />

	<!-- 配置service -->
	<bean id="userService" class="com.igeek.crm.service.impl.UserServiceImpl">
		<!-- 注入userDAO -->
		<property name="userDAO" ref="userDAO" />
	</bean>
	
	
	<!-- 配置切面 -->
	<bean id="myAspect" class="com.igeek.crm.aspect.MyAspect"/>
	
	<!-- 配置AOP -->
	<aop:config>
		<!-- 配置一个切入点 -->
		<!-- 
			expression 切入点的表达式,表达式有很多种
				bean(*Service) 表示有的bean的ID 是Service结尾的Bean都切
			id : 给切入点表达式起的名字
		 -->
		<aop:pointcut expression="bean(*Service)" id="serviceMethod"/>
		<!-- 
			切入切面的配置
				ref:配置要引用的切面bean
		 -->
		<aop:aspect ref="myAspect">
			<!-- 配置前置切面
				method:切面方法为引用的切面类中的方法名
				pointcut:配置引用的切入点
			 -->
			<aop:before method="firstBefor" pointcut-ref="serviceMethod"/>
			<!-- 配置后置通知 -->
			<aop:after-returning returning="returnVal" method="afterReturing" pointcut-ref="serviceMethod"/>
			<!-- 配置环绕通知 -->
			<aop:around method="around" pointcut-ref="serviceMethod"/>
		</aop:aspect>
	</aop:config>
	
</beans>

自定义切面 MyAspect类实现功能增强:

package com.igeek.crm.aspect;

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

/**
 * 
 * TODO
 *
 * 2018年10月10日下午7:03:11
 */
public class MyAspect {
	
	/**
	 * 环绕通知
	 * @param proceedingJoinPoint
	 * @return
	 * @throws Throwable
	 */
	public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		Object result = null;
		System.out.println("---环绕通知开始---");
		result = proceedingJoinPoint.proceed();//执行被代理对象的方法
		System.out.println("---环绕通知结束---");
		return result;
	}
	
	/**
	 * 后置通知
	 * @param joinPoint 
	 * @param returnVal 拦截方法的返回值
	 */
	public void afterReturing(JoinPoint joinPoint,Object returnVal){
		System.out.println("---------后置通知-----得到返回值:"+returnVal+"-----");
	}
	
	
	//第一个前置通知
	public void firstBefor(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("拦截的方法名称:"+methodName);
		String className = joinPoint.getTarget().getClass().getName();
		System.out.println("拦截对象的类名:"+className);
		System.out.println("代理对象:"+joinPoint.getThis().getClass().getName());
		System.out.println("第一个前置通知......");
	}
}

================================================================================

例2:

package com.igeekhome.c30.spring_aop_xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class CalculatorAspect {

	public void beforeMethod(JoinPoint joinPoint){
		System.out.println("CalculatorAspect1--------->"+joinPoint.getSignature().getName()+" method begin with "+Arrays.toString( joinPoint.getArgs()));
	}
	
	public void afterMethod(JoinPoint joinPoint){
		System.out.println("CalculatorAspect1--------->"+joinPoint.getSignature().getName()+" method end");
	}
}
package com.igeekhome.c30.spring_aop_xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class CalculatorAspect2 {

	public void beforeMethod(JoinPoint joinPoint){
		System.out.println("CalculatorAspect2--------->"+joinPoint.getSignature().getName()+" method begin with "+Arrays.toString( joinPoint.getArgs()));
	}
	
	public void afterMethod(JoinPoint joinPoint){
		System.out.println("CalculatorAspect2--------->"+joinPoint.getSignature().getName()+" method end");
	}
}
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<bean id="calculator" class="com.igeekhome.c30.spring_aop_xml.CalculatorImpl"></bean>
	
	<!-- 使用AOP肯定需要定义切面 -->
	<bean id="aspect" class="com.igeekhome.c30.spring_aop_xml.CalculatorAspect"></bean>
	<bean id="aspect2" class="com.igeekhome.c30.spring_aop_xml.CalculatorAspect2"></bean>
	
	<!-- 配置AOP -->
	<aop:config>
		<aop:pointcut expression="execution(* com.igeekhome.c30.spring_aop_xml.*.*(..))" id="pointcut"/>
		<aop:aspect ref="aspect" order="2">
			<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
			<aop:after method="afterMethod" pointcut-ref="pointcut"/>
		</aop:aspect>
		
		<aop:aspect ref="aspect2" order="1">
			<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
			<aop:after method="afterMethod" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>
</beans>

 expression配置:

任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))

pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了MyInterface接口的所有类,如果MyInterface不是接口,限定MyInterface单个类.
this(com.test.spring.aop.pointcutexp.MyInterface)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.

带有@MyTypeAnnotation标注的所有类的任意方法.
@within(com.elong.annotation.MyTypeAnnotation)
@target(com.elong.annotation.MyTypeAnnotation)
带有@MyTypeAnnotation标注的任意方法.
@annotation(com.elong.annotation.MyTypeAnnotation)
***> @within和@target针对类的注解,@annotation是针对方法的注解

参数带有@MyMethodAnnotation标注的方法.
@args(com.elong.annotation.MyMethodAnnotation)
参数为String类型(运行是决定)的方法.
args(String)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_无往而不胜_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值