使用注解开发AspectJ,已在java代码中加入@Aspect,为什么在配置中再次定义切面
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启AspectJ的注解开发,自动代理=====================-->
<aop:aspectj-autoproxy/>
<!--目标类===================-->
<bean id="productDao" class="com.imooc.aspectJ.demo1.ProductDao"/>
<!--定义切面-->
<bean class="com.imooc.aspectJ.demo1.MyAspectAnno"/>
</beans>
@Aspect public class MyAspectAnno { @Before(value="myPointcut1()") public void before(JoinPoint joinPoint){ System.out.println("前置通知=================="+joinPoint); } @AfterReturning(value="myPointcut2()",returning = "result") public void afterReturing(Object result){ System.out.println("后置通知=================="+result); } @Around(value="myPointcut3()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕前通知================"); Object obj = joinPoint.proceed(); // 执行目标方法 System.out.println("环绕后通知================"); return obj; } @AfterThrowing(value="myPointcut4()",throwing = "e") public void afterThrowing(Throwable e){ System.out.println("异常抛出通知=============="+e.getMessage()); } @After(value="myPointcut5()") public void after(){ System.out.println("最终通知=================="); } @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))") private void myPointcut1(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))") private void myPointcut2(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))") private void myPointcut3(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))") private void myPointcut4(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.findAll(..))") private void myPointcut5(){} }
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星