Java EE--Spring:aop(AspectJ)

两种方式

目标类

package com.mfs.chapter04;

import org.springframework.stereotype.Component;

@Component("userDao")
public class UserDAO {

	public int add(){
		System.out.println("添加用户");
//		制造异常
		int a = 2/0;
		return 0;
	}
	public int delete(int id) {
		System.out.println("删除用户");
		return 1;
	}
}

方式一:基于xml配置文件配置切面

切面类

package com.mfs.chapter04;


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;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

public class MyAspect {

	
	public void before() {
		System.out.println("before");
	}
	
	public void afterReturning(Object obj) {
		System.out.println("afterRunning"+obj);
	}
	

	public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		System.out.println("around前");
		Object obj = proceedingJoinPoint.proceed();
		System.out.println("around后");
		return obj;
	}
	
	public void after() {
		System.out.println("after");
	}

	public void afterThrowing(Throwable e) {
		System.out.println("afterThrowing"+e);
		
	}
}

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:aop="http://www.springframework.org/schema/aop"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.3.xsd ">

    <!-- 扫描注解 -->
	<context:component-scan
		base-package="com.mfs.chapter04"></context:component-scan>

	<!-- 切面类 -->
	<beans:bean id="myAspect" class="com.mfs.chapter04.MyAspect"></beans:bean>
	<aop:config>
		<!-- 切面配置 -->
		<aop:aspect ref="myAspect">
		配置切入点
			<aop:pointcut
				expression="execution(public * com.mfs.chapter04.UserDAO.*(..))"
				id="myPointCut" />
				<!-- 在切入点之前增强 -->
			<aop:before method="before" pointcut-ref="myPointCut" />
			<!--  在切入点的方法成功执行后增强,执行中断则不会增强,比如中间有异常抛出,obj用来获取切入点返回值对象 -->
			<aop:after-returning method="afterReturning"
				pointcut-ref="myPointCut" returning="obj"/>
				<!--  在切入点执行后增强,即便切入点中断 -->
			<aop:after method="after" pointcut-ref="myPointCut" />
			<!--  在切入点前后增强 -->
			<aop:around method="around" pointcut-ref="myPointCut"/>
			<!--  在切入点抛出异常后执行,e用来获取抛出的异常对象 -->
			<aop:after-throwing method="afterThrowing"
				pointcut-ref="myPointCut" throwing="e" />
		</aop:aspect>
	</aop:config> 
</beans>

方式二: 注解方式配置切面

切面类

package com.mfs.chapter04;


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;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {

	@Pointcut("execution(public * com.mfs.chapter04.UserDAO.*(..))")
	private void myPointCut() {}
	
	@Before("myPointCut()")
	public void before() {
		System.out.println("before");
	}
	@AfterReturning(value="myPointCut()",returning="obj")    //obj必须和通知方法中的参数名一致!!!
	public void afterReturning(Object obj) {
		System.out.println("afterRunning"+obj);
	}
	
	@Around("myPointCut()")
	public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		System.out.println("around前");
		Object obj = proceedingJoinPoint.proceed();
		System.out.println("around后");
		return obj;
	}
	@After("myPointCut()")
	public void after() {
		System.out.println("after");
	}
	@AfterThrowing(value="myPointCut()",throwing="e") //e必须和通知方法中的参数名一致!!!
	public void afterThrowing(Throwable e) {
		System.out.println("afterThrowing"+e);
		
	}
}

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:aop="http://www.springframework.org/schema/aop"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.3.xsd ">

    <!-- 扫描注解 -->
	<context:component-scan
		base-package="com.mfs.chapter04"></context:component-scan>
	<!-- 自动代理 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

测试类

package com.mfs.chapter04;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/mfs/chapter04/applicationContext.xml")
public class Test01 {
	@Resource(name="userDao")
	UserDAO userDao;
	@Test
	public void test() {
		userDao.delete(3);
		System.out.println("****************************************");
		userDao.add();
		
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

M FS

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

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

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

打赏作者

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

抵扣说明:

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

余额充值