spring的aop

来源:互联网 发布:java createelement 编辑:程序博客网 时间:2024/06/06 01:37

转自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{@Overridepublic void fooA(String msg) {System.out.println("AServiceImpl.fooA, msg" + msg);}@Overridepublic 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>
...


0 0
原创粉丝点击