使用注解开发AspectJ,已在java代码中加入@Aspect,为什么在配置中再次定义切面

使用注解开发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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@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

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕小脸 2020-07-05 18:37:25

同学你好,并没有重复。在类上添加注解@Aspect是为了声明该类是个切面类。

而在xml中定义切面类是为了对切面类进行管理,从而对目标类进行代理

http://img1.sycdn.imooc.com//climg/5f01a12d0987e7d405330106.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快。

好帮手慕小脸 2020-07-05 12:08:21

 同学你好,在xml中定义切面是为了对切面类进行管理。

http://img1.sycdn.imooc.com//climg/5f0151eb093714e105100098.jpg

如果这里不对切面类进行管理,它无法给目标类进行代理的

同学可以注释掉试试。注释前:

http://img1.sycdn.imooc.com//climg/5f01521c09ee539c07690256.jpg注释后:

http://img1.sycdn.imooc.com//climg/5f01522b098c879303060132.jpg

如果不配置切面类,我们就不知道该如何给目标类在哪里做增强,做怎样的增强

如果我的回答解决了你的问题,请采纳,祝学习愉快.

  • 提问者 chen_muke #1
    我们不是已经在java中加入@Aspect注解了吗?这个注解的意思不就是定义切面吗?我的意思这是不是重复了,或者注解的含义具体值的是什么?
    2020-07-05 17:29:50
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码