关于基于XML技术开发AOP应用报错的问题
小弟初学spring(其实也是JAVA的初学者)。
关于基于XML技术开发AOP应用报错的问题。
applicationContext.xml涉及的代码如下:
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
<bean id="aspetbean" class="cn.itcast.service.MyInterceptor"></bean>
<aop:config>
<aop:aspect id="asp" ref="aspetbean">
<aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
</aop:aspect>
</aop:config>
拦截类:
public class MyInterceptor {
//private void anyMethod(){}//声明切入点
public void doAccessCheck(String start)
{
System.out.println("前置通知: "+start);
}
}
拦截目标类:
public interface PersonService {
。。。
}
public class PersonServiceBean implements PersonService {
。。。。
}
报错如下,请指教,谢谢!
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService' defined in class path resource [ApplicationContext.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:405)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at junit.text.SpringAOPTest.interceptorTest(SpringAOPTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
[解决办法]
XML配置不提供参数的支持,若切面里的方法有参数,会报错
去掉所有方法的参数即可,或者对XML的配置如下:
"execution(java.lang.String com.persia.service.impl.PersonServiceImpl.*(..))"
代表要拦截返回值为String类型的方法。
"execution(* com.persia.service.impl.PersonServiceImpl.*(java.lang.String,..))"
代表要拦截第一个参数为String类型的方法,其他参数不限制。
"execution(!void com.persia.service.impl.PersonServiceImpl.*(..))"
代表要拦截返回值不是void类型的方法。
"execution(!void com.persia.service..*.*(..))"
代表要拦截com.persia.servie这个包的所有子包.*下的类的所有方法.*
[解决办法]
引起这个问题的原因很多,你可以先删除一些代码再调试,没问题再加上,直到找到错误,也许你会发现是你自己某个地方配置出错
[解决办法]
1去掉前置通知的参数
2修改配置文件<aop:before method="doAccessCheck" arg-names="name" pointcut="execution(* cn.itcast.service..*.*(..)) and args(name)"/>
[解决办法]
程序貌似没什么错误,那就看看环境是否搭建错误吧,看看你的jar文件是否有下面这些!
asm-2.2.3.jar
asm-commons-2.2.3.jar
asm-util-2.2.3.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
commons-logging-1.0.4.jar
log4j-1.2.15.jar
spring.jar
[解决办法]
将前置通知的方法:
public void doAccessCheck(String start)的参数删掉就OK了。
------------------------
即:public void doAccessCheck();