静态实现(AspectJ)

 本方式实现主要在于配置
包:
aspectjrt.jar
asm-2.2.3.jar
asm-commons-2.2.3.jar
asm-util-2.2.3.jar
spring.jar
cglib-nodep-2.1_3.jar
log4j-1.2.11.jar
commons-logging-1.0.4.jar
配置:AOP.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-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    <aop:config proxy-target-class="true">
        <aop:aspect ref="aspect">
           <aop:before
           pointcut="target(aopimp5.WaiterImp) and execution(* sayHello(..))"
           method="pointCutFun" />
        </aop:aspect>
    </aop:config>
    <bean id="aspect" class="aopimp5.TestAspect" />
    <bean id="waiter" class="aopimp5.WaiterImp" />
</beans>

1.
public interface Waiter {
    void sayHello(String str);
}
2.
public class WaiterImp implements Waiter {
    public void sayHello(String str) {
        System.out.println("+++++++++++ Hello "+str+" ++++++++++++=");
    }
}
3.
public class TestAspect {
    public void pointCutFun(){
        System.out.println("====== this is point cut =============");
    }
}
4.
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/AOP.xml"); //只能用ApplicationContext方式
        Waiter w = (Waiter)ctx.getBean("waiter");
        w.sayHello("Bill");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AspectJ是一种静态代理框架,它可以在编译期间将切面代码织入到目标对象的字节码中。相对于Spring AOP来说,AspectJ静态代理方式具有更好的性能,但需要特定的编译器进行处理。 AspectJ的演变过程可以分为三个方面,首先是对静态代理框架的分析和解释,然后是增加AspectJ案例,最后是对AspectJ字节码的分析。 AspectJ可以通过在编译期间生成AOP代理对象,将切面代码直接织入目标对象的字节码中,从而实现静态代理功能。这种方式可以在运行时不需要额外的代理对象,因此具有较好的性能表现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [AOP实现原理 —— 静态代理 和 动态代理( Spring AOP)](https://blog.csdn.net/xiaojin21cen/article/details/79487769)[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_1"}}] [.reference_item style="max-width: 50%"] - *3* [Android+反射+代理+静态代理+AspectJ](https://download.csdn.net/download/u013620306/86727235)[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_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值