日期:2014-05-19  浏览次数:20590 次

spring的aop错误
    我用的是spring2.5开发的aop,可是做拦截的时候报错,代码如下:
package cn.itcast.service;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 
 * 切面
 *
 */
@Aspect
public class MyInterceptor {
    @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
    private void anyMethod() {}
    
    @Before("anyMethod()")
    public void doAccessCheck(){
     System.out.println("前置通知");
    }
}


package cn.itcast.service;

public interface PersonService {
    public void save(String name);
    public void update(String name,Integer id);
    public String getPersonName(Integer id);
}

package cn.itcast.service.impl;

import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {

public void save(String name) {
System.out.println("我是save()方法");
}

public void update(String name, Integer id) {
System.out.println("我是update()方法");
}

public String getPersonName(Integer id) {
    System.out.println("我是 getPersonName()方法");
return "xxxx";
}

}

package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringAOPTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Test public void interceptorTest(){
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)cxt.getBean("personService");
personService.save("xxxxx");
}

}


<?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-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <aop:aspectj-autoproxy/>
    <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>
    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
</beans>