public interface UserService {
public void add();
}
业务类:
@Service(“userService”)
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println(“add…”);
}
}
客户端:
public class App {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(“spring/applicationContext-service.xml”);
UserService userService = (UserService) ac.getBean(“userService”);
userService.add();
}
}
1.advisor:
applicationContext-service.xml
<context:component-scan base-package=“com.zhangyan.service”/>
aop:config
<aop:pointcut expression=“execution(* com.zhangyan.service.*.*(…))” id=“myPointCut”/>
<aop:advisor advice-ref=“myAdvice” pointcut-ref=“myPointCut”/>
</aop:config>
MyAroundAdvice :
public class MyAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println(“start…”);
Object val = methodInvocation.proceed();
System.out.println(“end…”);
return val;
}
}
当调用App客户端类打印结果:
start…
add…
end…
2.Aspect
applicationContext-service.xml
<context:component-scan base-package=“com.zhangyan.service”/>
aop:config
<aop:aspect id=“myAspect” ref=“consumeAspect”>
<aop:pointcut expression=“execution(* com.zhangyan.service.*.*(…))” id=“myPointCut”/>
<aop:around method=“doAround” pointcut-ref=“myPointCut” />
</aop:aspect>
</aop:config>
ConsumeAdvice通知类:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class ConsumeAdvice {
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(“start…”);
Object retVal = pjp.proceed();
System.out.println(“end…”);
return retVal;
}
}
当执行完App客户端后, 执行结果:
start…
add…
end…
总结:
可以很明显看到两种实现方式,
如果使用advisor, 那么定义通知类时候, 如果是环绕通知, 通知类必须实现MethodInterceptor, 当然还有其他接口 : BeforeAdvice , ThrowsAdvice, AfterReturningAdvice 等接口.
如果使用aspect, 那么配置<’aop:around method=”doAround” pointcut-ref=”myPointCut” />, 相当于声明使用环绕通知, 那么通知类不用实现任何接口, 通知类中方法名称和配置中method相同. 通知类中方法参数应该是ProceedingJoinPoint, 就ok .
所以advisor是Spring为了简化操作, 定义这种方式, 当然也有局限, 只能有一个Pointcut和一个advice .
在advice中可以定义多个Point和advice .
配置:
aop:config
<aop:aspect id=“myAspect” ref=“consumeAspect”>
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数软件测试工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上软件测试开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
助,可以添加V获取:vip1024b (备注软件测试)**
[外链图片转存中…(img-0CH8PhyD-1712988582518)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!