spring学习--------AOP的实现
AOP
AOP:aspect oriented programming,翻译过来为“面向切面编程”。
spring AOP就是将一些公共的功能,比如日志,安全等和业务代码结合,当执行业务代码时,把公共代码嵌入到其中。实现公共代码的可重复利用,使得我们的业务代码更加纯粹,我们便可以只专注于业务代码。AOP的本质是动态代理模式。
AOP的实现方式一:通过springAPI来实现
springAPI中实现接口MethodBeforeAdvice该拦截器会在调用方法前执行;实现接口AfterReturningAdvice该拦截器会在调用方法后执行;实现接口MethodInterceptor该拦截器会在调用方法前后都执行,实现环绕结果。
public interface UserService {public void add();public void update();public void delete();public void search();
}
public class UserServiceImpl implements UserService {@Overridepublic void add() {System.out.println("增加用户");}@Overridepublic void update() {System.out.println("修改用户信息");}@Overridepublic void delete() {System.out.println("删除用户信息");}@Overridepublic void search() {System.out.println("查询用户信息");}
}
public class Log implements MethodBeforeAdvice{public void log(){System.out.println("进入执行方法");}/*** @param method 被调用的方法对象* @param args 被调用的方法的参数* @param target 被调用的方法的目标对象* @throws Throwable*/@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行");}
}
public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");UserService userService = (UserService) ac.getBean("userService");userService.delete();}
}
beans.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"></bean><bean id="log" class="cn.sxt.log.Log"></bean><aop:config><!-- pointcut切入点 expression表达式--><aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"></aop:pointcut><!-- 指出需要使用哪些公共的业务 --><aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor></aop:config></beans>
项目目录结构如下:
执行结果如下:
AOP的实现方式二:自定义类来实现
将上面的Log类和配置文件做修改,主要是修改配置文件,代码如下。
public class Log{public void before(){System.out.println("方法执行前");}public void after(){System.out.println("方法执行后");}
}
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"></bean><bean id="log" class="cn.sxt.log.Log"></bean><aop:config><!-- 配置信息如下 --><aop:aspect ref="log"><aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"></aop:pointcut><aop:before method="before" pointcut-ref="pointcut"></aop:before><aop:after method="after" pointcut-ref="pointcut"></aop:after></aop:aspect></aop:config>
</beans>
执行结果如下所示:
AOP的实现方式三:使用注解来实现
还可以使用注解来实现AOP,我们修改Log类和beans.xml配置文件,在Log类前加上@Aspect的注解,在方法前加上@Before和@After的注解,我们再新增一个方法,实现一下环绕方法。beans.xml配置文件也不需要</aop:config>的配置,添加aop:aspectj-autoproxy/即可,aop:aspectj-autoproxy/声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。如下所示:
@Aspect
public class Log{@Before("execution(* cn.sxt.service.impl.*.*(..))")public void before(){System.out.println("方法执行前");}@After("execution(* cn.sxt.service.impl.*.*(..))")public void after(){System.out.println("方法执行后");}@Around("execution(* cn.sxt.service.impl.*.*(..))")public Object aroud(ProceedingJoinPoint jp) throws Throwable{System.out.println("环绕前");System.out.println("签名:"+jp.getSignature());//执行目标方法Object result = jp.proceed();System.out.println("环绕后");return result;}
}
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"></bean><bean id="log" class="cn.sxt.log.Log"></bean><aop:aspectj-autoproxy/></beans>
执行结果如下:
由执行结果我们可以看出,环绕方法在方法执行前执行,执行目标方法后执行环绕后,最后执行After。其中jp.getSignature()为方法签名。
以上为【2天学会spring框架-邹波】视频中部分内容加自己整理的一些内容,侵删。
spring学习--------AOP的实现相关推荐
- [原创]java WEB学习笔记107:Spring学习---AOP切面的优先级,重用切点表达式
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- spring学习--AOP五个通知
五个通知 1)前置通知 :函数执行前增强 2)后置通知 :函数执行后增强,除非异常,最后执行 3)环绕通知 :函数执行的前后都增强 4)异常通知 :出现异常的时候执行 5)最终通知:不管出不出现异常 ...
- spring学习--AOP术语
a)连接点:类里面哪些方法可以被增强,这些方法称为连接点 b)切入点:实际被真正增强的方法称为切入点 c)通知(增强):实际增强的逻辑部分称为通知,且分为以下五种类型: 1)前置通知 :函 ...
- Spring学习(八)AOP详解
本文借鉴:Spring学习 一.一个例子 在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让关注点代 ...
- Spring学习(三)Spring AOP 简介
本文借鉴:Spring学习(特此感谢!) 一.简介 定义 aop就是面向切面编程,在数据库事务中切面编程被广泛使用. 在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 核心业务:比如登陆 ...
- Spring 学习笔记(二)Spring AOP
前言 容器和AOP是Spring的两大核心.本文将来学习Spring AOP. AOP是什么? AOP在计算机科学领域还是相对年轻的概念,由Xerox PARC公司发明.Gregor Kiczales ...
- [Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP
上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(三):Spring实现JDBC 下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(五):SpringA ...
- Spring 学习笔记----->AOP
Spring 学习笔记----->AOP 代理模式 为什么学代理模式? 因为这就是Spring Aop的底层 代理模式的分类: 静态代理 动态代理 静态代理 生活用的例子: 房东 public ...
- Spring 学习 (三)大话AOP
一直想着怎么去通俗的讲解AOP,这两篇博客给了我启发 (https://blog.csdn.net/qukaiwei/article/details/50367761),(https://blog.c ...
最新文章
- 【Rollo的Python之路】Scrapy Selector选择器的学习
- 使用 Spot 低成本运行 Job 任务
- 开启和关闭wifi的代码段
- poj2406 Power Strings
- tcp测试监听工具_linux 下两款网络性能测试工具介绍
- centos安装软件【google浏览器,QQ】【拷贝旧的文件源作为备份】【软件源更换为清华源】
- Python 程序员如何防止数据被修改?
- python入门100例题-这 100 道 Python 题,拿去刷!!!
- 综述|线结构光中心提取算法研究
- Monterey Cache Cleaner 17 for mac(苹果系统维护软件)
- 博士德服务器帐套维护密码忘记,T+忘记账套主管、admin密码
- nRF52840学习-初识1-1
- Python数据挖掘——文本分析
- 群友(淡泊、明志)总结android面试题
- 文件包含漏洞(完整版)
- 安卓应用开发 MyWeChat(二)
- linux关闭8080端口,Linux修改iptables,取消8080的访问限制
- BZOJ 1565 [NOI2009]植物大战僵尸
- 自定义广告联盟接入解决方案,适用所有广告商接入。
- 小陈谈JAVA(数组到底是怎样的,三分钟让你彻底认识数组)