springAOP对方法进行增强

springAOP对方法进行增强

1.导入AOP所用的的jar包

请添加图片描述

2.创建UserService接口和UserServiceImpl实现类对增删改查方法的封装
  • UserService
public interface UserService {
	//增
	void add();
	//删
	void delete();
	//改
	void update();
	//查
	void select();
}

  • UserServiceImpl
public class UserServiceImpl implements UserService {

	@Override
	public void add() {
	System.out.println("add");
	int i=1/0;
	}

	@Override
	public void delete() {
		System.out.println("delete");

	}

	@Override
	public void update() {
		System.out.println("update");

	}

	@Override
	public void select() {
		System.out.println("select");

	}

}
3.创建MyAdvice类添加增强的方法
package com.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAdvice {

	//before 前置通知 在目标方法前调用
	public void before() {
		System.out.println("before");
	}
	
	//after 最终通知(后置通知)在目标方法后调用,无论是否出现异常都会执行 finally
	public void after() {
		System.out.println("after");
	}
	
	//afterReturning 成功通知(后置通知)在目标方法执行后,并且执行成功,如果方法出现异常则不调用
	public void afterReturning() {
		System.out.println("afterReturning");
	}
	
	//afterThrowing 异常通知(后置通知)在目标方法执行出现异常的时候才会调用
	public void afterThrowing() {
		System.out.println("afterThrowing");
	}
	
	//around 环绕通知 需要我们手动调用目标方法,并且可以设置通知
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("around before");
		Object proceed = pjp.proceed();
		System.out.println("around after");
		return proceed;
	}
}

4.配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	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-4.3.xsd">
	
	<!-- 目标对象 -->
	<bean name="userService" class="com.service.UserServiceImpl"></bean>
	<bean name="MyAdvice" class="com.aop.MyAdvice"></bean>
	<aop:config>
<!--  切入点  expression 切入点表达式 可以配置要增强的方法
								public void com.service.UserServiceImpl.save()
								* com.service.*ServiceImpl.*(..)
							   id 就是唯一标识
			 -->
			<!--切入点表达式类似将其中的方法当作一个切面,后面的增强方法将会调用对应的切面-->
 	<aop:pointcut expression="execution(* com.service.*ServiceImpl.*(..))" id="servicePc"/>
 	<aop:aspect ref="MyAdvice">
 	<!--增强方法-->
 	<aop:before method="before" pointcut-ref="servicePc"/>
 	<aop:after method="after" pointcut-ref="servicePc"/>
 	<aop:after-throwing method="afterThrowing" pointcut-ref="servicePc"/>
 	<aop:after-returning method="afterReturning" pointcut-ref="servicePc"/>
 	<aop:around method="around" pointcut-ref="servicePc"/>
 	</aop:aspect>
	</aop:config>
</beans>
5.创建测试类
package com.Test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class spring_aop_test {
	@Resource(name = "userService")
	UserService us;
	@Test
	public void test01() {
		us.add();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值