xml实现AOP切面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml实现AOP切面相关的知识,希望对你有一定的参考价值。
一、applicationContext.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="personService" class="com.lovo.u34.service.impl.PersonServiceImpl" ></bean> <bean id="myInterceptor" class="com.lovo.u34.service.MyInterceptor"></bean> <aop:config> <aop:pointcut expression="execution (* com.lovo.u34.service.impl.PersonServiceImpl.save(String))" id="personService1"/> <aop:aspect id="inter" ref="myInterceptor"> <aop:before method="doAccessCheck" pointcut-ref="personService1"/> <aop:after method="doAfter" pointcut-ref="personService1"/> <aop:after-returning method="doAfterCheck" pointcut-ref="personService1"/> </aop:aspect> </aop:config> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
二、PersonServiceImpl
public class PersonServiceImpl implements PersonService{ @Override public void save(String name) { System.out.println("我是save方法"+name); //throw new RuntimeException("我爱例外"); } @Override public void update(String name, Integer personid) { System.out.println("我是update方法"); } @Override public String getPersonName(Integer personid) { System.out.println("我是getPersonName方法"); return "xxx"; } }
三、MyInterceptor
public class MyInterceptor { public void doAccessCheck(){ System.out.println("before前置方法"+"---"); } public void doAfter(){ System.out.println("after后置方法"); } public void doAfterCheck(){ System.out.println("afterReturning方法正常执行完毕之后执行"+"----"); } public void doAfterThrow(){ System.out.println("例外通知"+"----"); } public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("进入环绕通知"); Object object = pjp.proceed();//执行该方法 System.out.println("退出环绕方法"); return object; } }
以上是关于xml实现AOP切面的主要内容,如果未能解决你的问题,请参考以下文章