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

使用注解方式和XML配置方式完成AOP编程_第1张图片

第一种方式:基于注解

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>

使用注解方式和XML配置方式完成AOP编程_第2张图片

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);
    }
}

你可能感兴趣的:(Spring,java,spring)