宁波海曙网站建设,公司网站免备案,建设银行住房租赁代表品牌是什么,用ps做网站方法概念解释#xff1a;#xff08;理解基本概念方可快速入手#xff09; 连接点#xff08;joinpoint#xff09; 被拦截到的点#xff0c;因为Spring只支持方法类型的连接点#xff0c;所以在Spring中连接点指的就是被拦截到的方法。 切入点#xff08;pointcut#x… 概念解释理解基本概念方可快速入手 连接点joinpoint 被拦截到的点因为Spring只支持方法类型的连接点所以在Spring中连接点指的就是被拦截到的方法。 切入点pointcut 切入点是指我们要对哪些连接点进行拦截的定义 通知advice 所谓通知指的就是指拦截到连接点之后要执行的代码通知分为前置、后置、异常、最终、环绕通知五类 切面aspect 是切入点和通知的结合 通知顺序 前置通知aop:before 后置通知aop:after-returning【try】 最终通知aop:after【finally】 异常通知aop:after-throwing【catch】 环绕通知aop:around try{ ... return aop:after-returning }catch(Exception e){ ... aop:after-throwing }finally{ ... aop:after } 切点表达式 格式execution([修饰符] 返回值 报名.类名.方法名(参数)) egexecution(* com.by.service.*.*(..)) 实例演示 pom.xml: dependencies!--ioc--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion6.0.12/version/dependency!--支持切点表达式AOP --dependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion5.1.8.RELEASE/version/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-log4j12/artifactIdversion1.7.19/version/dependency/dependencies UserDaoImpl: package com.by.dao;public class UserDaoImpl implements UserDao {Overridepublic void addUser(){System.out.println(insert into tb_user......);}
} UserServiceImpl: package com.by.service;import com.by.dao.UserDao;public class UserServiceImpl implements UserService {private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao userDao;}Overridepublic void addUser(){userDao.addUser();System.out.println(8/0);}
} MyLogActive:(增强类) /** Copyright (c) 2020, 2024, All rights reserved.**/
package com.by.advice;import org.aspectj.lang.ProceedingJoinPoint;/*** pProject: Spring - MyLogAdvice/p* pPowered by scl On 2024-01-05 15:04:11/p* p描述p** author 孙臣龙 [1846080280qq.com]* version 1.0* since 17*/
public class MyLogAdvice {public void after() {System.out.println(最终通知、、、);}public void before() {System.out.println(前置通知、。、);}public void afterReturn(){System.out.println(后置通知);}public void afterThrowing(){System.out.println(异常通知);}public void around(ProceedingJoinPoint joinPoint) {try {System.out.println(前环绕通知。。。);joinPoint.proceed();System.out.println(后环绕通知。。。);} catch (Throwable e) {throw new RuntimeException(e);}}
} 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!--增强--bean idmyLogAdvice classcom.by.advice.MyLogAdvice/bean!--aop--aop:config!--切点--aop:pointcut idpointcut expressionexecution(* com.by.service.*.*(..))/!--切面--aop:aspect refmyLogAdviceaop:before methodbefore pointcut-refpointcut/aop:beforeaop:after methodafter pointcut-refpointcut/aop:afteraop:around methodaround pointcut-refpointcut/aop:aroundaop:after-returning methodafterReturn pointcut-refpointcut/aop:after-returningaop:after-throwing methodafterThrowing pointcut-refpointcut/aop:after-throwing/aop:aspect/aop:config/beans 没增强前结果展示 增强之后结果展示