java spring aop:java.lang.IllegalArgumentException::: 0处的错误找不到引用的切入点LoginMethod

时间:2011-12-20 10:22:38

标签: java spring aop

我设计了一个db应用程序,但需要处理使用spring aop连接到db的异常,我已经在下面显示了类

LoginInterface.java

LoginInterface(){
   ApplicationContext  context = new ClassPathXmlApplicationContext("LoginApp.xml");
   Login login = (Login) context.getBean("Login");
   login.loginMethod(username,password);
 }

Login.java
{
    loginMethod(String username, char[] pwd) throws ClassNOtFoundException, SQLException{
     ...
    }
 }

LoginProfiler.java

package dbapp;

import java.sql.SQLException;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.JoinPoint;

@Aspect 公共类LoginProfiler {

  @Pointcut("execution(* dbapp.Login.loginMethod(String, char[])throws java.lang.ClassNotFoundException, java.sql.SQLException)")
   public void loginMethod(){}

   @Around("loginMethod()")
    public void handleException(final ProceedingJoinPoint pJoinPoint )throws Throwable{

       try{

           pJoinPoint.proceed();

      }catch(Exception e) {


         if((e.getCause().toString()).contains("UnknownHostException") ){

              System.out.println("Unknown Host ");

         }else if((e.getCause().toString()).contains("ConnectException")){        
              System.out.println("Connection Problem ");

        }   
     }
   }

}

LoginApp.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"
        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"
        default-destroy-method="destroy"
        default-init-method="afterPropertiesSet"
        default-autowire="byName">



        <!-- Enable the @AspectJ support -->
        <aop:aspectj-autoproxy />

        <bean id="LoginProfiler" class="dbapp.LoginProfiler" />
        <bean id="Login" class="dbapp.Login" />
</beans>

我有以下例外

Erg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Login' defined in class path resource [LoginApp.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut LoginMethod
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    at 
..
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut LoginMethod
    at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:315)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:206)
    at 

1 个答案:

答案 0 :(得分:3)

试试这个。

@Aspect
public class LoginProfiler {

       @Pointcut("execution(* dbapp.Login.loginMethod(String, char[])throws java.lang.ClassNotFoundException, java.sql.SQLException)")
       public void loginMethod(){}

       @AfterThrowing("loginMethod()")
        public void handleException(final JoinPoint joinPoint){ 
               System.out.println("Am able to Handle");
        }
}

@Aspect
public class LoginProfiler {

       @AfterThrowing("execution(* dbapp.Login.loginMethod(String, char[])throws java.lang.ClassNotFoundException, java.sql.SQLException)")
        public void handleException(final JoinPoint joinPoint){ 
               System.out.println("Am able to Handle");
        }
}

如果你花一些时间学习spring-aop会更好。从你的问题看起来你真的不懂AOP。您正尝试从一些示例代码中剪切和粘贴。