使用注解方式和XML配置方式完成AOP编程

在这里插入图片描述

第一种方式:基于注解

beanx10.xml

<?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:context="http://www.springframework.org/schema/context"
       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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--扫描指定包-->
    <context:component-scan
            base-package="com.elf.spring.aop.homework"/>

    <!--启用基于注解的AOP功能-->
    <aop:aspectj-autoproxy/>
</beans>

在这里插入图片描述

package com.elf.spring.aop.homework;
/**
 * @author 45
 * @version 1.0
 */
public interface Cal {
    public int cal1(int n);
    public int cal2(int n);
}

package com.elf.spring.aop.homework;
import org.springframework.stereotype.Component;
/**
 * @author 45
 * @version 1.0
 */
@Component //将Cal对象作为组件,注入到Spring容器
public class MyCal implements Cal {
    @Override
    public int cal1(int n) {
        int res = 1;
        for (int i = 1; i <= n; i++) {
            res += i;
        }
        System.out.println("cal1 执行结果=" + res);
        return res;
    }
    @Override
    public int cal2(int n) {
        int res = 1;
        for (int i = 1; i <= n; i++) {
            res *= i;
        }
        System.out.println("cal2 执行结果=" + res);
        return res;
    }
}

package com.elf.spring.aop.homework;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
 * @author 45
 * @version 1.0
 */
@Aspect //MyCalAOP 是一个切面类
@Component //MyCalAOP/对象 作为组件注入到spring容器
public class MyCalAOP {

    //前置通知
    //这里注意,如果目标类和切面类,在同一个包,可以省略包名
    //因为cal1和cal2方法,都要去输出开始执行时间,因此使用MyCal.*
    @Before(value = "execution(public int MyCal.*(int))")
    public void calStart(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName() + " 执行, 开始执行时间=" + System.currentTimeMillis());

    }

    //返回通知
    //这里注意,如果目标类和切面类,在同一个包,可以省略包名
    //因为cal1和cal2方法,都要去输出开始执行时间,因此使用MyCal.*
    @AfterReturning(value = "execution(public int MyCal.*(int))")
    public void calEnd(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName() + " 执行, 结束时间=" + System.currentTimeMillis());
    }
}

package com.elf.spring.aop.homework;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author 45
 * @version 1.0
 */
public class TestMyCalAOP {
    @Test
    public void testMyCalByAnnotation() {
        //得到spring容器
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans10.xml");
        Cal cal = ioc.getBean(Cal.class);
        cal.cal1(10);
        System.out.println("===========");
        cal.cal2(5);
    }
}

第二种方式:基于XML

<?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 https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置MyCalAOP-bean  id就用类名首字母小写-->
    <bean class="com.elf.spring.aop.homework.xml.MyCalAOP" id="myCalAOP" />
    <!--配置MyCal-bean-->
    <bean class="com.elf.spring.aop.homework.xml.MyCal" id="myCal"/>
    <!--配置切面类-->
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="myPointCut" expression="execution(public int com.elf.spring.aop.homework.xml.MyCal.*(int))"/>
        <!--配置前置,返回-->
        <aop:aspect ref="myCalAOP" order="10">
            <aop:before method="calStart" pointcut-ref="myPointCut"/>
            <aop:after-returning method="calEnd" pointcut-ref="myPointCut"/>
        </aop:aspect>
    </aop:config>
</beans>
package com.elf.spring.aop.homework.xml;
/**
 * @author 45
 * @version 1.0
 * 复制粘贴的时候也要注意包名
 */
public interface Cal {
    public int cal1(int n);
    public int cal2(int n);
}

package com.elf.spring.aop.homework.xml;
/**
 * @author 45
 * @version 1.0
 */
public class MyCal implements Cal {
    @Override
    public int cal1(int n) {
        int res = 1;
        for (int i = 1; i <= n; i++) {
            res += i;
        }
        System.out.println("cal1 执行结果=" + res);
        return res;
    }

    @Override
    public int cal2(int n) {
        int res = 1;
        for (int i = 1; i <= n; i++) {
            res *= i;
        }
        System.out.println("cal2 执行结果=" + res);
        return res;
    }
}

package com.elf.spring.aop.homework.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
/**
 * @author 45
 * @version 1.0
 */
public class MyCalAOP {

    //前置通知

    public void calStart(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName() + " 基于XML配置- 执行, 开始执行时间=" + System.currentTimeMillis());

    }

    //返回通知
    public void calEnd(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName() + " 基于XML配置- 执行, 结束时间=" + System.currentTimeMillis());

    }
}

package com.elf.spring.aop.homework.xml;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author 45
 * @version 1.0
 */
public class TestMyCalAOP {
    @Test
    public void testMyCalByXML() {
        //得到spring容器
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans11.xml");
        Cal cal = ioc.getBean(Cal.class);
        cal.cal1(10);
        System.out.println("===========");
        cal.cal2(5);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值