Spring AOP实现Demo

引入依赖

 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.15</version>
        </dependency>

方式一:xml配置

目录结构

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

    <bean id="aopTank" class="designpattern.aop.v1.AopTank"/>
    <bean id="aopMethod" class="designpattern.aop.v1.AopMethod"/>

    <aop:config>
        <aop:aspect id="time" ref="aopMethod">
            <aop:pointcut id="onmove" expression="execution(void designpattern.aop.v1.AopTank.*(..))"/>
            <aop:before method="before" pointcut-ref="onmove"/>
            <aop:after method="after" pointcut-ref="onmove"/>
        </aop:aspect>
    </aop:config>

</beans>

AopMethod类:需要在切入类添加的方法

package designpattern.aop.v1;

/**
 * @Auther: IceBear
 * @Date: 2022/2/15 19:48
 * @Description:
 */
public class AopMethod {
    public void before() {
        System.out.println("before...");
    }

    public void after() {
        System.out.println("after...");
    }
}

AopTank类:需要切入的类

package designpattern.aop.v1;

/**
 * @Auther: IceBear
 * @Date: 2022/2/15 19:47
 * @Description:
 */
public class AopTank {
    public void move() {
        System.out.println("tank is moving...");
    }
    public void voice() {
        System.out.println("tank is cacacaca...");
    }

}

Main方法调用入口

package designpattern.aop.v1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Auther: IceBear
 * @Date: 2022/2/15 19:28
 * @Description:
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("app.xml");
        AopTank tank = (AopTank) context.getBean("aopTank");
        tank.move();
        tank.voice();
    }
}

运行结果

方式二:注解配置

目录结构

 

引入依赖:

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

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

    <!--需要引入aspectjweaver 依赖-->
    <aop:aspectj-autoproxy/>

    <bean id="aopTank" class="designpattern.aop.v2.AopTank"/>
    <bean id="aopMethod" class="designpattern.aop.v2.AopMethod"/>
</beans>

 AopMethod类

package designpattern.aop.v2;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @Auther: IceBear
 * @Date: 2022/2/15 19:48
 * @Description:
 */
@Aspect
public class AopMethod {
    @Before("execution(void designpattern.aop.v2.AopTank.*(..))")
    public void before() {
        System.out.println("before..." + System.currentTimeMillis());
    }

    @After("execution(void designpattern.aop.v2.AopTank.*(..))")
    public void after() {
        System.out.println("after..." + System.currentTimeMillis());
    }
}

 AopTank类与方式一相同

package designpattern.aop.v2;

/**
 * @Auther: IceBear
 * @Date: 2022/2/15 19:47
 * @Description:
 */
public class AopTank {
    public void move() {
        System.out.println("tank is moving...");
    }
    public void voice() {
        System.out.println("tank is cacacaca...");
    }

}

Main入口

package designpattern.aop.v2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Auther: IceBear
 * @Date: 2022/2/15 19:28
 * @Description:
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("app_auto.xml");
        AopTank tank = (AopTank) context.getBean("aopTank");
        tank.move();
        tank.voice();
    }
}

 运行结果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半路出家的码农小王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值