spring的aop

转自http://pandonix.iteye.com/blog/336873/

package com.lam.spring;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;


/**
 * need aspectjrt.jar, aspectjweaver.jar
 *@Title:ServiceAspect.java
 *@Description:TODO
 *@Author:Administrator
 *@Date:2015年4月14日 下午10:30:26
 *@Version:1.0
 */
public class ServiceAspect {
	
	public void doAfter(JoinPoint jp){
		System.out.println("after method:" 
				+ jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
	}
	
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
		long startTime = System.currentTimeMillis();
		Object ret = pjp.proceed();
		long endTime = System.currentTimeMillis();
		System.out.println("process time:" + (endTime - startTime));
		return ret;
	}
	
	public void doBefore(JoinPoint jp){
		System.out.println("before method:" 
				+ jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
	}
	
	public void doThrowing(JoinPoint jp, Throwable th){
		System.out.println("method:" 
				+ jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() 
				+ " throw Throwable. " + th.getMessage());
	}

}


package com.lam.spring;
/**
 *@Title:AService.java
 *@Description:TODO
 *@Author:Administrator
 *@Date:2015年4月14日 下午10:22:37
 *@Version:1.0
 */
public interface AService {
	
	public void fooA(String msg);
	
	public void barA();

}

/**
 *@Title:AServiceImpl.java
 *@Description:TODO
 *@Author:Administrator
 *@Date:2015年4月14日 下午10:23:34
 *@Version:1.0
 */
public class AServiceImpl implements AService{

	@Override
	public void fooA(String msg) {
		System.out.println("AServiceImpl.fooA, msg" + msg);
	}

	@Override
	public void barA() {
		System.out.println("AServiceImpl.barA");
	}

}

package com.lam.spring;
/**
 *@Title:BServiceImpl.java
 *@Description:TODO
 *@Author:Administrator
 *@Date:2015年4月14日 下午10:24:39
 *@Version:1.0
 */
public class BServiceImpl {
	
	public void barB(String msg, int type){
		System.out.println("BServiceImpl.barB, msg:" + msg + ", type:" + type);
		if(type == -1){
			throw new IllegalArgumentException("type:" + type);
		}
	}
	
	public void fooB(){
		System.out.println("AServiceImpl.fooB");
	}

}

public class AopTest {

	public static void main(String[] args) {
		ApplicationContext appCtx = new ClassPathXmlApplicationContext(
				"file:F:\\...\\applicationContext.xml");
        AService aService = (AService) appCtx.getBean("aService");
        BServiceImpl bService = (BServiceImpl) appCtx.getBean("bService");
        
        aService.barA();
        aService.fooA("I am arguments A.");

        bService.barB("I am arguments B.", -1);
        bService.fooB();
        
	}

}

spring的配置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-3.0.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  >  
    <aop:config>  
        <aop:aspect id="ServiceAspect" ref="serviceAspect">  
            <!--配置com.lam.spring包下所有类或接口的所有方法-->  
            <aop:pointcut id="businessService" expression="execution(* com.lam.spring.*.*(..))" />  
            <aop:before pointcut-ref="businessService" method="doBefore"/>  
            <aop:after pointcut-ref="businessService" method="doAfter"/>  
            <aop:around pointcut-ref="businessService" method="doAround"/>
            <!-- th是切面处理类ServiceAspect的doThrowing方法的第二个参数,名字要一样 -->  
            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="th"/>  
        </aop:aspect>  
    </aop:config>  
      
    <bean id="serviceAspect" class="com.lam.spring.ServiceAspect" />  
    <bean id="aService" class="com.lam.spring.AServiceImpl"></bean>  
    <bean id="bService" class="com.lam.spring.BServiceImpl"></bean>  
  
</beans>
...


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值