有一个切面类里面有两个方法:
public class CreateAop {
public void doBeofore(){
System.out.println("--------权限验证");
}
public void doAfter(){
System.out.println("--------记录日志");
}
}
有个用户接口和实现类:
public interface UserDao {
public void insert(String name);
public void test();
}
public class UserDaoImpl implements UserDao {
@Override
public void insert(String name) {
System.out.println("你好,"+name);
}
@Override
public void test() {
System.out.println("test success......");
}
}这前面基本上应该是没问题的:在看看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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.spingframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="userdaoImpl" class="Imp.UserDaoImpl"></bean>
<bean id="CA" class="Aop.CreateAop"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切面 -->
<aop:aspect id="ap" ref="CA">
<!-- 配置切入点 :expression={返回任意类型或不返回 ,这个包下的任意类的任意方法和任意参数}-->
<aop:pointcut expression="execution(* Imp.*.*(..))" id="ep"/>
<!-- 配置通知 -->
<aop:after method="doAfter" pointcut-ref="ep"/>
<aop:before method="doBeofore" pointcut-ref="ep"/>
</aop:aspect>
</aop:config>
</beans>
再就是test了:
public static void main(String[] args) {
ClassPathResource cpr=new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory=new XmlBeanFactory(cpr);
UserDao userdao=(UserDao)factory.getBean("userdaoImpl");
userdao.insert("young");
}
最后的结果只打印了1语句话。按理应该是3句话的。
难道是配置文件写的有问题?这问题纠结了一天。不得不来像大牛们求解。
难道是导包错了??

解决方案 »

  1.   

    先这个:pointcut-ref  在写:method.不知道正确与否!
      

  2.   

    试用下这个表达式execution(* ins*(..))看下
      

  3.   

    main中的内容改为BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserDao userdao=(UserDao)factory.getBean("userdaoImpl");
    userdao.insert("young");
      

  4.   

    你的包名还是大写的啊。你按住ctrl 看能点进去这两个类吗?<bean id="userdaoImpl" class="Imp.UserDaoImpl"></bean>
        <bean id="CA" class="Aop.CreateAop"></bean>
      

  5.   

    我自己做了测试的,没任何问题
    你那边把main中的内容先全注释,再加入我给你的代码
      

  6.   

    你spring的aop jar包有没有导入啊
      

  7.   

    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userdaoImpl' 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: warning no match for this type name: imp [Xlint:invalidAbsoluteTypeName]
    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:222)
    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:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at test.Client.main(Client.java:20)
    Caused by: 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: warning no match for this type name: imp [Xlint:invalidAbsoluteTypeName]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:254)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    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:222)
    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.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:87)
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:98)
    at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:105)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:281)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:791)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:762)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:399)
    ... 13 more
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: imp [Xlint:invalidAbsoluteTypeName]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:115)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:248)
    ... 31 more
    Caused by: java.lang.IllegalArgumentException: warning no match for this type name: imp [Xlint:invalidAbsoluteTypeName]
    at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:206)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:193)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getMethodMatcher(AspectJExpressionPointcut.java:179)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.buildSafePointcut(AbstractAspectJAdvice.java:189)
    at org.springframework.aop.aspectj.AspectJPointcutAdvisor.<init>(AspectJPointcutAdvisor.java:51)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
    ... 33 more
      

  8.   


    重写了一遍就抛上面这个异常了。jar包也导了。spring.jar
    comming-logging.jar
    log4j.jar
    aspectjrt.jar
    aspectjweaver.jar
    就导了这几个包。。还有吗?
      

  9.   

    spring3.0 core
    spring3.0 aop
    你是用的myeclipse吗
      

  10.   

    Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: imp你到底包名要用大写还是小写啊???
      

  11.   

    你在配置文件里面按住ctrl 能点进去?<bean id="userdaoImpl" class="Imp.UserDaoImpl"></bean>
      <bean id="CA" class="Aop.CreateAop"></bean> 大写小写是有区别的。
      

  12.   


    我这是重新建了一个项目。然后再copy了一份。顺便把包名也改了。当初建的时候没太在意。
    楼上有位大哥既然指出来了。那我就把把他改成小写了。在applicationContext.xml文件中也改了。而且我也测试了下。如果把配置AOP的代码去了。完全可以创建成功。
      

  13.   

    对。用的是myeclipse8.6..有问题吗?
      

  14.   


    大哥。能进去。。如果把配置AOP的代码去了。这两个对象都可以创建成功。
      

  15.   

    那你可以直接就用myeclipse给你提供的spring的jar包,把aop的jar包也要加进去
      

  16.   

    嗯。用Add Spring Capabilities..吗??我上一个项目就是这么做的。。好。听你的。在试试。。
      

  17.   

    是,我先去买包烟,你的代码就按照你最开始那样的去,然后只是main方法中的内容就用我给你的那段
      

  18.   

    你就差那么一点点了,肯定是哪个小细节的问题,因为我用你最开始的配置和代码,只改动下main方法就成功得出了结论,加油啊
      

  19.   


    嗯。谢谢。你用的spring哪个版本的啊?2.5的还是3.0的。。
      

  20.   

    和你一样3.0的,你要注意下myeclipse8.6自带spring3.0不,我myeclipse10集成了3.0的,我看你配置文件是用的3.0的,如果你用的是2.5的,那要改
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.spingframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
      

  21.   

    问题已经解决了。。首先非常感谢各位大牛的指点和解疑。。真的是发自内心的感谢。。谢谢你们。。主要原因:虽然已经导入了jar包。但是没有效果。所以可以借组myEclipse8.6的Add Spring Capabilities的功能,在导入核心库的时候,再把AOP的库也导进来。。就没问题了。也就不用导那些jar包了。还有一个地方就是测试的地方:某位大牛指点出来的非常准确。我的测试类
     ClassPathResource cpr=new ClassPathResource("applicationContext.xml");
            XmlBeanFactory factory=new XmlBeanFactory(cpr);
            UserDao userdao=(UserDao)factory.getBean("userdaoImpl");
            userdao.insert("young");
    这样还是只能打印一句话。因为:XmlBeanFactory不支持AOP所以还要把测试类改了。
    BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserDao userdao=(UserDao)factory.getBean("userdaoImp");
    userdao.insert("young");
    就可以了。最后还是非常感谢各位大牛的指点。小弟会继续努力的。。谢谢你们
      

  22.   

    在学到配置通知的时候又有点小问题:配置前后通知,环绕通知都没有问题。但是配置的异常通知的时候就不行了。。我是在一个切面类写的所有的通知。如果把其他的通知都去除的话。就可以执行通知异常这个方法。求解??关键源码如下:
    public class UserDaoImpl implements UserDao {
    @Override
    public void insert(String name) {
    System.out.println("你好,"+name);
    }
    @Override
    public void test() throws Exception {
    System.out.println("测试成功.");
    throw new RuntimeException("出错啦");
    }
    }
    这是切面类;
    public class CreateAop implements ThrowsAdvice {
    //在方法之前执行的动作
    public void doBeofore(JoinPoint jointPoint){
    System.out.println("--------权限验证");

    //获取执行之前的类的信息
    System.out.println("方法执行的类:"+jointPoint.getTarget().getClass().getName());
    System.out.println("执行的方法:"+jointPoint.getSignature().getName());
    System.out.print("执行的参数有:");
    Object obj[]=jointPoint.getArgs();
    for(int i=0;i<obj.length;i++){
    System.out.print(" "+obj[i]+" ");
    }
    }
    //在方法之后执行的动作
    public void doAfter(JoinPoint jointPoint1){
    System.out.println("--------记录日志");
    //获取执行之前的类的信息
    StringBuffer sb=new StringBuffer();
    sb.append(jointPoint1.getTarget().getClass().getName());
    sb.append(".");
    sb.append(jointPoint1.getSignature().getName());
    sb.append("(");

    Object obj[]=jointPoint1.getArgs();
    if(obj.length>0){
    for(int i=0;i<obj.length-1;i++){
    sb.append(obj[i]);
    sb.append(",");
    }
    sb.append(obj[obj.length-1]);
    }
    sb.append(")");
    System.out.println(sb.toString());
    }
    //环绕通知
    public void doAround(ProceedingJoinPoint pjp){
    long time=System.currentTimeMillis();
    try {
    pjp.proceed();
    } catch (Throwable e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    long time1=System.currentTimeMillis();
    System.out.println("方法执行了"+(time1-time)+"毫秒");
    }
    //抛出异常通知
    public void doException(JoinPoint jp,Throwable ex){
    System.out.println("method " + jp.getTarget().getClass().getName()
    + "." + jp.getSignature().getName() + " throw exception");
    System.out.println(ex.getMessage());
    this.sendMsg(ex.getMessage());
    }
    public void sendMsg(String ex){
    if(ex!=null){
    System.out.println("老板,抛异常了");
    }
    }
    }
    还有是不是可以不用实现这个接口啊?我没有实现这个接口也能执行进到这个处理异常的方法里。
    而且处理异常的时候都会把详细的异常打印出来。这是在没有其他的通知的情况下打印的结果。
    ************************
    测试成功.
    method imp.UserDaoImpl.test throw exception
    出错啦
    老板,抛异常了
    java.lang.RuntimeException: 出错啦
    at imp.UserDaoImpl.test(UserDaoImpl.java:26)
    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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:54)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy0.test(Unknown Source)
    at test.Client.main(Client.java:27)
    如果有其他的通知和异常处理一并存在:就不会执行这个异常通知方法:
    ************************
    --------权限验证
    方法执行的类:imp.UserDaoImpl
    执行的方法:test
    执行的参数有:测试成功.
    --------记录日志
    imp.UserDaoImpl.test()
    java.lang.RuntimeException: 出错啦
    at imp.UserDaoImpl.test(UserDaoImpl.java:26)
    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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:77)
    at aop.CreateAop.doAround(CreateAop.java:52)
    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.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:64)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:54)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy0.test(Unknown Source)
    at test.Client.main(Client.java:27)
    方法执行了2毫秒求解??
      

  23.   

    这是applicationContext.xml文件信息
    <beans
    xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
    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
         http://www.spingframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="userdaoImp" class="imp.UserDaoImpl"></bean>
        <bean id="aop" class="aop.CreateAop"></bean>
        <aop:config>
            <!-- 配置切面 -->
            <aop:aspect id="ap" ref="aop">
                <!-- 配置切入点 :expression={返回任意类型或不返回 ,这个包下的任意类的任意方法和任意参数}-->
                <!-- <aop:pointcut expression="execution(* imp.*.*(String))" id="ep"/> -->
                 <aop:pointcut expression="execution(* imp.*.*(..))" id="ep"/>
                <!-- 配置通知 -->
                <aop:after method="doAfter" pointcut-ref="ep"/>    
                <aop:before method="doBeofore" pointcut-ref="ep"/>
                <aop:around method="doAround" pointcut-ref="ep"/>
                <aop:after-throwing method="doException" pointcut-ref="ep" throwing="ex" />
            </aop:aspect>
        </aop:config>
    </beans>