使用注解方式进行aop编程(代码)

UserDao.java接口:

package com.liu.jdk;

public interface UserDao {
	public void addUser();
	public void deleteUser();

}

UserDaoImpl.java实现类/目标类:

package com.liu.jdk;

import org.springframework.stereotype.Repository;

//目标类
@Repository("userDao")
public class UserDaoImpl implements UserDao {

	@Override
	public void addUser() {
		// TODO Auto-generated method stub
		System.out.println("添加方法");
		int i  = 1/0;
	}

	@Override
	public void deleteUser() {
		// TODO Auto-generated method stub
		System.out.println("删除方法");
	}

}

MyAspect.java切面类:

package com.liu.aspect.annotation;

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

//切面类
@Aspect
@Component
public class MyAspect {
@Pointcut(“execution(* com.liu.jdk..(…))”)
//需要一个返回值为void 方法体为空的方法来命名切入点
private void myPointCut() {}

//前置通知
@Before("myPointCut()")
public void before(JoinPoint joinPoint) {
	System.out.println("前置通知开始,模拟检查权限 ");
	System.out.println("目标类:"+joinPoint.getTarget());
	System.out.println("目标方法为:"+joinPoint.getSignature().getName());
}
//后置通知
@AfterReturning("myPointCut()")
public void afterRunning(JoinPoint joinPoint) {
	System.out.println("后置通知开始,模拟记录日志");
	System.out.println("目标方法为:"+joinPoint.getSignature().getName());
}

//环绕通知
@Around("myPointCut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {
	System.out.println("环绕通知开始");
	Object obj = proceedingJoinPoint.proceed();
	System.out.println("环绕通知结束");
	return obj;
}

//异常通知
@AfterThrowing(value = "myPointCut()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Throwable e) {
	System.out.println("异常通知,原因在于:"+e.getMessage());
}

//最终通知
@After("myPointCut()")
public void after() {
	System.out.println("最终通知");
}

}

ApplicationContext.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-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.liu"></context:component-scan>
        <!-- 启动基于注解的声明式AspectJ支持 -->
        <aop:aspectj-autoproxy />
</beans>

Test测试类:

package com.liu.aspect.annotation;

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

import com.liu.jdk.UserDao;

public class Test {
	public static void main(String[] args) {
		String xmlpath = "com/liu/aspect/annotation/ApplicationContext.xml";
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
		
		UserDao userDao = (UserDao) applicationContext.getBean("userDao");
		
		userDao.addUser();
	}

}

运行结果:

环绕通知开始
前置通知开始,模拟检查权限 
目标类:com.liu.jdk.UserDaoImpl@5158b42f
目标方法为:addUser
添加方法
最终通知
异常通知,原因在于:/ by zero
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

想养一只!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值