Spring纯POJO切面实现AOP

8 篇文章 0 订阅

1.导包

aopalliance.jar

aspectjweaver.jar


2.编写Controller

package com.chen.web;

@Controller
@RequestMapping("/list")
public class MyController {

       @RequestMapping("test.html")
	public String test(HttpServletRequest req, HttpServletResponse res){
		return "../NewFile";
	}
}


3.创建横切处理类

package com.chen.aspect;

public class TestHelper {
	public void printTime(){
		System.out.println("current time :"+System.currentTimeMillis());
	}
}

4.创建AOP配置文件spring-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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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="myController" class="com.chen.web.MyController"/>
    <bean id="timeHandler" class="com.chen.aspect.TestHelper"/>
    
    <aop:config>
        <aop:aspect id="time" ref="timeHandler">
            <aop:pointcut expression="execution(* com.chen.web.MyController.test(..))" id="addAllMethod"/>
            <aop:before method="printTime" pointcut-ref="addAllMethod"/>
            <aop:after method="printTime" pointcut-ref="addAllMethod"/>
        </aop:aspect>
    </aop:config>
</beans>

5.部署tomcat,并测试


execution表达式解释

execution(* com.chen.aspect..*.*(..))

符号 含义
execution()表达式的主体;
第一个”*“符号 表示返回值的类型任意;
com.chen.aspectAOP所切的服务的包名,即,我们的业务部分
包名后面的”..“ 表示当前包及子包
第二个”*“ 表示类名,*即所有类。此处可以自定义,下文有举例
.*(..) 表示任何方法名,括号表示参数,两个点表示任何参数类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值