jsp asp php哪个做网站重庆高端网站建设公司
news/
2025/9/26 5:45:02/
文章来源:
jsp asp php哪个做网站,重庆高端网站建设公司,做化工回收上什么网站,网络营销实验网站建设心得AOP
概念
AOP#xff1a;全称是Aspect Oriented Programming即#xff1a;面向切面编程。 简单的说它就是把我们程序重复的代码抽取出来#xff0c;在需要执行的时候#xff0c;使用动态代理的技术#xff0c;在不修改源码的基础上#xff0c;对程序进行增强#xff…AOP
概念
AOP全称是Aspect Oriented Programming即面向切面编程。 简单的说它就是把我们程序重复的代码抽取出来在需要执行的时候使用动态代理的技术在不修改源码的基础上对程序进行增强权限校验,日志记录,性能监控,事务控制.
AOP相关术语 连接点joinpoint 被拦截到的点因为Spring只支持方法类型的连接点所以在Spring中连接点指的就是被拦截到的方法。 其实就是所有可能会被增强的方法 切入点pointcut掌握 切入点是指我们要对哪些连接点进行拦截的定义 就是我们选择部分所要增强的方法 通知advice掌握 所谓通知指的就是指拦截到连接点之后要执行的代码通知分为前置、后置、异常、最终、环绕通知五类 一般是用来扩展日志功能或事务控制等 切面aspect掌握 是切入点和通知的结合 就是讲增强通知应用到切入点的过程 引介introduction 是一种特殊的通知在不修改代码的前提下引介可以在运行期为类动态地添加一些方法或字段 目标对象Target 要代理的目标对象要增强的类 织入weave 将增强应用到目标的过程将advice应用到target的过程 代理Proxy 一个类被AOP织入增强之后就产生一个代理类
Spring的AOP配置
1.创建工程 1.1.pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.by/groupIdartifactIdSpring_AOP_Xml/artifactIdversion1.0-SNAPSHOT/versiondependencies!-- Spring常用依赖 --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.1.8.RELEASE/version/dependency!--支持切点表达式 --dependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion5.1.8.RELEASE/version/dependency/dependencies
/project1.2.dao
/*** 持久层实现类*/
public class UserDaoImpl implements UserDao {Overridepublic void addUser(){System.out.println(insert into tb_user......);}
}1.3.service
/*** 业务层实现类*/
public class UserServiceImpl implements UserService {private UserDao userDao;public void addUser(){userDao.addUser();}
}1.4.applicationContext.xml
?xml version1.0 encodingUTF-8?
!--注意添加约束--
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://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.xsdbean iduserDao classcom.by.dao.UserDaoImpl/beanbean iduserService classcom.by.service.UserServiceImplproperty nameuserDao refuserDao/property/bean
/beans1.5.web
/*** 模拟表现层*/
public class Client {public static void main(String[] args) {ApplicationContext ac new ClassPathXmlApplicationContext(applicationContext.xml);//使用对象UserService userService ac.getBean(userService,UserService.class);System.out.println(userService.getClass());userService.addUser();}
}2.增强 创建增强类 package com.by.advice;import org.aspectj.lang.ProceedingJoinPoint;import java.util.Date;public class MyLogAdvice {//前置通知public void before(){System.out.println(前置通知);}//后置通知【try】public void afterReturning(){System.out.println(后置通知);}//异常通知【catch】public void afterThrowing(){System.out.println(异常通知);}//最终通知【finally】public void after(){System.out.println(最终通知);}//环绕通知public void around(ProceedingJoinPoint joinPoint){try {System.out.println(方法执行前的环绕通知);joinPoint.proceed();System.out.println(方法执行后的环绕通知);} catch (Throwable throwable) {throwable.printStackTrace();}}
}配置增强类 !--增强--
bean idmyLogger classcom.by.advice.MyLogger/bean3.切点 切点表达式 表达式语法 execution([修饰符] 返回值类型 包名.类名.方法名(参数)) 例如 execution(* com.by.service.UserService.add(..)) execution(* com.by.service.UserService.*(..)) execution(* com.by.service.*.*(..)) 配置切点 aop:config!--切点--aop:pointcut idpointcut expressionexecution(* com.by.service.*.*(..))/
/aop:config4.切面 增强的类型 aop:before用于配置前置通知aop:after-returning用于配置后置【try】通知它和异常通知只能有一个执行aop:after-throwing用于配置异常【catch】通知它和后置通知只能执行一个aop:after用于配置最终【finally】通知aop:around用于配置环绕通知 配置切面 !--切面--
aop:aspect refmyLogger!-- 用于配置前置通知指定增强的方法在切入点方法之前执行 method:用于指定通知类中的增强方法名称ponitcut-ref用于指定切入点--aop:before methodbefore pointcut-refpointcut/aop:after-returning methodafterReturning pointcut-refpointcut/aop:after-throwing methodafterThrowing pointcut-refpointcut/aop:after methodafter pointcut-refpointcut/aop:around methodaround pointcut-refpointcut/
/aop:aspect###5.测试
测试service实现接口时的类型 测试service不实现接口时的类型
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/917890.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!