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
    评论
SpringOP可以通过自定义注解的方式实现灰度发布。具体来说,可以通过在需要进行灰度发布的方法上添加一个自定义注解,如@GrayRelease。然后使用Spring AOP的切面功能,在切面中通过拦截被@GrayRelease注解标记的方法,根据特定的条件判断是否执行灰度发布逻辑。 具体实现方案可以参考以下步骤: 1. 创建一个自定义注解,如@GrayRelease,用于标记需要进行灰度发布的方法。 2. 创建一个切面类,通过@Before或@Around等注解,在切面中拦截被@GrayRelease注解标记的方法。 3. 在切面中实现灰度发布的逻辑,可以根据特定的条件来判断是否执行灰度发布,例如根据接口版本号、用户类型等条件。 4. 在Spring配置文件中配置切面类和自定义注解的扫描。 这样,当调用被@GrayRelease注解标记的方法时,切面会拦截并执行相应的灰度发布逻辑。通过使用自定义注解和Spring AOP,可以实现基于Spring的灰度发布功能。引用中的例子,可以参考使用HandlerMapping来实现灰度发布的示例。 需要注意的是,灰度发布是一种高级的功能,在实际应用中,需要根据具体的需求和架构来设计和实现相应的灰度发布方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [spring扩展之基于HandlerMapping实现接口灰度发布的demo](https://blog.csdn.net/leisure_life/article/details/128400044)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Spring AOP实现Redis缓存数据库查询](https://download.csdn.net/download/weixin_38627603/12772291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

半路出家的码农小王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值