spring AOP 例子
先看 advice 类
package com.supben.advice;import java.lang.reflect.Method;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.aop.AfterReturningAdvice;import org.springframework.aop.MethodBeforeAdvice;import org.springframework.aop.ThrowsAdvice;/** * 实现spring advice 接口 * * @author shencl * */public class TestAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {private static final Logger log = LoggerFactory.getLogger(TestAdvice.class);/** * before 通知 */public void before(Method method, Object[] args, Object target) throws Throwable {log.info(target.getClass().getSimpleName() + "类的" + method.getName() + "方法,执行TestAdvice的before通知");// 通知要做的业务if (method.getName().startsWith("get")) {log.info("只有方法名是以get开始的方法,才会执行到这句话....");}}/** * after 通知 */public void afterReturning(Object arg0, Method method, Object[] arg2, Object target) throws Throwable {log.info(target.getClass().getSimpleName() + "类的" + method.getName() + "方法,执行TestAdvice的after通知");}/** * 异常通知 */public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {log.info(target.getClass().getSimpleName() + "类的" + method.getName() + "方法,执行TestAdvice的throwing通知");}}
<?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" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><context:annotation-config /><!-- 扫描com.supben 下所有的包--><context:component-scan base-package="com.supben" /><bean id="testAdvice" /><aop:config> <aop:advisor pointcut="execution(* *..service.*Service.*(..))"advice-ref="testAdvice" /></aop:config></beans>
package com.supben.service;public interface FirstService {public void get();public void exception();}
package com.supben.service.impl;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Service;import com.supben.service.FirstService;@Service("firstService")public class FirstServiceImpl implements FirstService {private static final Logger log = LoggerFactory.getLogger(FirstServiceImpl.class);public void get() {log.info("方法执行ing.....");}public void exception() {throw new RuntimeException("测试异常");}}
package com.supben.test;import junit.framework.TestCase;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.supben.service.FirstService;import com.supben.spring.SpringContextUtil;public class ServiceTest extends TestCase {/** * 装载spring 配置文件 */static {new ClassPathXmlApplicationContext("application.xml");}@Testpublic void testGet() {FirstService service = SpringContextUtil.getBean("firstService");service.get();}@Testpublic void testGet2() {FirstService service = SpringContextUtil.getBean("firstService");service.exception();}}