基于XML开发AOP应用报错的问题

    技术2022-05-19  26

    基于XML开发AOP应用

    服务层接口PersonService:

    package com.go123.service; public interface PersonService { public abstract String getPersonName(Integer personid); public abstract void save(String name); public abstract void update(String name, Integer personid); } 

    实现PersonServiceImpl:

    package com.go123.service.impl; import com.go123.service.PersonService; public class PersonServiceImpl implements PersonService{ public String getPersonName(Integer personid) { System.out.println("调用getPersonName()方法"); return "xxx"; } public void save(String name) { System.out.println("调用save()方法"+name); } public void update(String name, Integer personid) { System.out.println("调用update()方法"); } }  

    拦截类MyInterceptor 

    package com.go123.aop; import org.aspectj.lang.ProceedingJoinPoint; public class MyInterceptor { //匹配函数参数名称为name,获取输入参数 public void doAccessCheck(String name) { System.out.println("前置通知:"+name); } //result是被拦截方法的返回值,再当做参数传递给doAfterReturning函数,获取返回结果 public void doAfterReturning(String result) { System.out.println("后置通知:"+ result); } public void doAfter() { System.out.println("最终通知"); } public void doAfterThrowing(Exception e) { System.out.println("例外通知:"+ e); } public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { //if(){//判断用户是否在权限 System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); //} return result; } }  

    beans.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:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="myInterceptor" class="com.go123.aop.MyInterceptor"></bean> <bean id="personService" class="com.go123.service.impl.PersonServiceImpl"></bean> <aop:config> <aop:aspect id="myaop" ref="myInterceptor"> <aop:pointcut expression="execution(* com.go123.service.impl.PersonServiceImpl.*(..)) id="mycut"/> <aop:before method="doAccessCheck" pointcut-ref="mycut"/> </aop:aspect> </aop:config> </beans> 

    测试类SpringAOPTest:

    package junt.test; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.go123.service.PersonService; public class SpringAOPTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } /** * 基于xml配置文件实现AOP */ @Test public void interceptorTest(){ ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)cxt.getBean("personService"); personService.save("yy"); } }  

    上述配置后会抛出org.springframework.beans.factory.BeanCreationException: 

    原因是XML配置不提供参数的支持,若切面里的方法有参数,会报错。

    第一种解决方式:

    在拦截类MyInterceptor中去掉doAccessCheck(String name)方法中的参数

    第二种解决方式:

    修改beans.xml中切入点 <aop:pointcut expression="execution(* com.go123.service.impl.PersonServiceImpl.*(..)) and args(name)" id="mycut"/>

     

     


    最新回复(0)