静态AOP使用示例

加载时织入指的是在虚拟机载入字节码文件时动态织入AspectJ切面.
spring框架的值添加微AspectJ LTW在动态织入过程中提供更细粒度的控制.
在java5+的代理能使用一个叫“Vanilla”的AspectJ LTW,这需要在启动JVM的时候将某个JVM参数设置为开.这种JVM范围的设置在一些情况下或许不错,但是通常情况下显得粗粒度了.
Spring的LTW能让你在per-ClassLoader的基础上打开LTW,这显然更加细粒度,而且不用修改服务的启动脚本,只需要简单修改应用上下文的一个或几个文件就能使用LTW,而不需要依靠管理者部署配置.
下面就来看看静态AOP的实现步骤:
1、Spring全局配置文件的修改,加入LTW开关

<?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"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
"
>
	<aop:aspectj-autoproxy />
    <bean id="test" class="com.ipluto.demo.aspect.TestBean"/>
    <bean class="com.ipluto.demo.aspect.AspectJTest"/>
    <context:load-time-weaver/>
</beans>

2、加入aop.xml,在class目录下的META-INF文件夹下

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <include within="com.ipluto.demo.aspect.*"/>
    </weaver>

    <aspects>
        <aspect name="com.ipluto.demo.aspect.AspectJTest"/>
    </aspects>
</aspectj>

主要是告诉AspectJ需要对哪个包进行织入,并使用哪些增强器
3、加入启动参数.

java -javaagent:/users/xxx/projects/aop/com/itplto/demo/aspect/xxxx.jar com.itplto.demo.aspect.TestMain

在这里插入图片描述
4、测试

public class TestMain {
    public static void main(String[] args) {
        ApplicationContext bf = new ClassPathXmlApplicationContext("beans.xml");
        TestBean bean = (TestBean)bf.getBean("test");
        bean.test();
    }
}

5、结果和动态Aop的结果相同.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值