spring AOP---【小白系列】0基础到熟练应用spring框架(三)
来源:互联网 发布:linux fm驱动 编辑:程序博客网 时间:2024/06/06 04:51
aop概念:
AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善,实际上是一种思想,对一个方法在不改变源码的情况下进行的增强。
aop的核心概念:
连接点(joinpoint):目标对象中可以被增强的方法 叫做连接点
切点(pointcut):连接点中真正被增强方法叫做切点
增强/通知(advice):增强的功能的方法
切面(aspect):切点+增强=切面
织入(weave):将切点与增强结合的过程叫做织入
引介(introduction):动态的为目标对象添加字段,方法...
aop案例
1.导包spring-aop.jar
spring-aspects.jar
aopalliance.jar
aspectJ.jar2.连接点编写
public class Target {public void show1(){System.out.println("show1 running ...");}public void show2(){System.out.println("show2 running ...");}public void show3(){System.out.println("show3 running ...");}public void show4(){System.out.println("show4 running ...");}
3.增强编写
public class MyAspect {public void before(){System.out.println("before running .. ");}}
4.在applicationContext.xml中织入
<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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --><context:component-scan base-package="com.itheima"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--被增强 --><bean id="target" class="com.itheima.aop.Target"></bean><!--增强 --><bean id="myaspect" class="com.itheima.aop.MyAspect"></bean><aop:config><aop:aspect ref="myaspect"><aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.show1())"/></aop:aspect></aop:config></beans>5.测试
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class AopTest { @Autowiredprivate Target target =new Target();@Testpublic void test(){target.show1();}}
细节一:增强类型
细节二:切点表达式的写法
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.itheima.*.*(..))"/><aop:after-returning method="afterReturing" pointcut="execution(* com.itheima.aop.Tatget.*(..))"/>
细节三:切点表达式抽取
<aop:pointcut expression="execution(* com.itheima.*.*(..)" id="mypointcut"/>
细节四:切面内部 可以配置多个切点表达式和 多个增强的组合
AOP注解方式开发
开发步骤
1.导包,同上xml方式
2.编写增强
3.编写连接点
4.注解织入
5.测试
因为我们要在java文件中写bean,那我们就要开启组件扫描,其实也可以开启全注解,个人觉得开发还是混着用比较舒服
首先在applicationContext.xml中配置组件扫描
<context:component-scan base-package="com.itheima"></context:component-scan>其次配置aop自动代理(使java中能识别aop注解)
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
配置连接点
@Component("target")public class Target {public void show1(){System.out.println("show1 running ...");}}
配置增强
@Component("myAspect")@Aspectpublic class MyAspect {@Before("execution(* com.itheima.aop.*.*(..))")public void before(){System.out.println("before running .. ");}}
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class AopTest { @Autowiredprivate Target target =new Target();@Testpublic void test(){target.show1();}}
ok以上就是spring aop开发笔记
阅读全文
0 0
- spring AOP---【小白系列】0基础到熟练应用spring框架(三)
- spring IoC注解开发---【小白系列】0基础到熟练应用spring框架(二)
- spring环境搭建&spring快速入门---【小白系列】0基础到熟练应用spring框架(一)
- struts2获取作用域对象---【小白系列】0基础到熟练应用struts2框架(三)
- hibernate增删改查案例教程---【小白系列】0基础到熟练应用hibernate框架(三)
- hibernate实体类的三种状态---【小白系列】0基础到熟练应用hibernate框架(八)
- hibernate环境搭建教程---【小白系列】0基础到熟练应用hibernate框架(一)
- HibernateUtils工具---【小白系列】0基础到熟练应用hibernate框架(四)
- hibernate主键生成策略---【小白系列】0基础到熟练应用hibernate框架(七)
- hibernate一级缓存---【小白系列】0基础到熟练应用hibernate框架(九)
- hibernate多对多关系---【小白系列】0基础到熟练应用hibernate框架(十三)
- hibernate事务---【小白系列】0基础到熟练应用hibernate框架(十)
- hibernatJPA---【小白系列】0基础到熟练应用hibernate框架(十五)
- struts2环境搭建---【小白系列】0基础到熟练应用struts2框架(一)
- Struts2结果视图配置---【小白系列】0基础到熟练应用struts2框架(二)
- struts2值栈&struts ognl表达式---【小白系列】0基础到熟练应用struts2框架(四)
- struts2参数封装---【小白系列】0基础到熟练应用struts2框架(五)
- struts2 interceptor拦截器---【小白系列】0基础到熟练应用struts2框架(六)
- 2017暑假集训 div1 线段树(1)
- [bzoj3282]Tree Link-Cut-Tree
- 程序、任务、进程和线程的联系与区别
- 继承的相关知识
- 使用Java 8 的日期和时间Api
- spring AOP---【小白系列】0基础到熟练应用spring框架(三)
- 洛谷P1008 三连击
- hdu5701中位数计数(百度之星)
- 考试排名
- Java集合总结(二)
- 黑马商城项目(三)之一
- 伪类
- 3838. 【NOIP2014模拟9.14】Super Big Stupid Cross
- 从控制台输入字符 统计该字符"e"所出现的个数