Spring Aop注解实现
作者:宁在春
本文我们通过Spring AOP和Java的自定义注解来实现日志的插入功能,非常不错,具有一定的参考借鉴价值,需要的朋友一起看看吧,希望对你有所帮助
Spring-aop-理论知识 Spring-Aop-注解实现 项目结构图
具体步骤:
1、创建maven 项目 导入依赖 创建好项目结构
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.18</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> </dependencies>
2、写一个接口 及 其实现类
/** * @version 1.0 * @author: crush * @date: 2021-03-05 10:26 */ public interface TestDao { public void delete(); }
/** * @version 1.0 * @author: crush * @date: 2021-03-05 10:27 */ @Service public class TestDaoImpl implements TestDao { public void delete() { System.out.println("正在执行的方法-->删除"); } }
3、切面类
/** * @version 1.0 * @author: crush * @date: 2021-03-10 18:04 */ @Aspect @Component public class MyAspectAnnotation { @Pointcut("execution(* com.dao.*.*(..))") private void myPointCut(){ } /** * 前置通知 使用JoinPoint 接口作为参数获得目标对象的信息 * @Before("myPointCut()") == * <aop:after method="after" pointcut-ref="myPointCut"/> **/ @Before("myPointCut()") public void before(JoinPoint jp){ System.out.print("前置通知:模拟权限控制"); System.out.println("目标对象:"+jp.getTarget()+",被增强的方法:"+jp.getSignature().getName()); } @AfterReturning("myPointCut()") public void afterReturning(JoinPoint jp){ System.out.print("后置返回通知:"+"模拟删除临时文件"); System.out.println(",被增强的方法"+jp.getSignature().getName()); } @Around("myPointCut()") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环绕开始:执行目标方法前,模拟开启事务"); Object obj = pjp.proceed(); System.out.println("环绕结束:执行目标方法后,模拟关闭事务"); return obj; } @AfterThrowing(value = "myPointCut()",throwing = "throwable") public void except(Throwable throwable){ System.out.println("异常通知:"+"程序执行异常"+throwable.getMessage()); } @After("myPointCut()") public void after(){ System.out.println("最终通知:释放资源"); } }
4、application.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.dao"/> <context:component-scan base-package="com.aspect"/> <!--启动基于注解的AspectJ支持--> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
测试
@Test public void Test(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class); testDao.delete(); /** * 输出: * 环绕开始:执行目标方法前,模拟开启事务 * 前置通知:模拟权限控制目标对象:com.dao.TestDaoImpl@2bef51f2,被增强的方法:delete * 正在执行的方法-->删除 * 后置返回通知:模拟删除临时文件,被增强的方法delete * 最终通知:释放资源 * 环绕结束:执行目标方法后,模拟关闭事务 */ }
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注脚本之家的更多内容!