Spring AOP 使用
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><context:component-scan base-package="com.mhm"/><!-- aop使用注解时,必须配 --><aop:aspectj-autoproxy/></beans>
package com.mhm.aop;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Component@Aspectpublic class MyAOP {@SuppressWarnings("unused")@Pointcut("execution(* com.mhm.mng.impl..*.*(..))")//AOP表达式语言//执行业务方法时拦截,*表示返回类型,//..表示对impl下的子包中的类也拦截//*表示所有类//*表示所有方法//(..)表示方法的参数为随意private void anyMethod() {}//声明一个切入点@Before("anyMethod() && args(name)")//定义前置通知//args(name)表示拦截的方法必须有一个参数,配合下面的,表示参数为String类型public void doBefore(String name) {System.out.println("前置通知:" + name);}@AfterReturning(pointcut="anyMethod()", returning="revalue")//定义后置通知public void doAfterReturning(String revalue) {System.out.println("后置通知");}@After("anyMethod()")//定义最终通知public void doAfter(){System.out.println("最终通知");}@AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义异常通知public void doAfterThrowing(Exception ex){System.out.println("异常通知: " + ex);}@Around("anyMethod()")//环绕通知public Object doAround(ProceedingJoinPoint pjp) throws Throwable {//环绕通知最适合做权限验证//次return必须写System.out.println("进环绕通知");Object obj = pjp.proceed();System.out.println("出环绕通知"); //出通知为最后输出,最终通知在出环绕通知的前面return obj;}}
package com.mhm.dao;public interface PersonDao {/** * 得到名字 * @param name */public String getName(int id);/** * 保存 */public void save(String name);/** * 更新 */public void update(int id);}
package com.mhm.dao.impl;import org.springframework.stereotype.Repository;import com.mhm.dao.PersonDao;@Repositorypublic class PersonDaoImpl implements PersonDao {public String getName(int id){System.out.println("我是dao的getName:" + id);return "getNameById: " + id;}public void save(String name){throw new RuntimeException("this is a exception");//System.out.println("我是dao的save(),保存姓名:" + name);}public void update(int id){System.out.println("我是dao的update()");}}
package com.mhm.mng.impl;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.mhm.dao.PersonDao;import com.mhm.mng.PersonMng;@Servicepublic class PersonMngImpl implements PersonMng {@Autowiredprivate PersonDao personDao;@SuppressWarnings("unused")@PostConstructprivate void init(){System.out.println("我是PersonMngImpl中的init()");}@SuppressWarnings("unused")@PreDestroy//要使用此注解,在客户端必须使用AbstractApplicationContextprivate void destroy(){System.out.println("我是PersonMngImpl中的destroy()");}public String getName(int id){//System.out.println("我是mng的getName:" + id);return personDao.getName(id);}public void save(String name){//System.out.println("我是save()");personDao.save(name);}public void update(int id){personDao.update(id);}}
package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.mhm.mng.PersonMng;public class SpringTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Testpublic void springtest(){//ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//PersonMng personMng = (PersonMng)ctx.getBean("personMngImpl");//personMng.save();AbstractApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");PersonMng personMng = (PersonMng)actx.getBean("personMngImpl");personMng.save("mahongmin");actx.close();}}