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

标签 java spring aop

我设计了一个数据库应用程序,但现在在使用 spring aop 连接到数据库时处理异常时遇到问题。涉及的类如下所示:

登录接口(interface).java

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

登录.java

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

LoginProfiler.java

@Aspect
public class 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 

最佳答案

试试这个。

@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。您正在尝试从一些示例代码中剪切并粘贴。

关于java spring aop:java.lang.IllegalArgumentException:错误在::0找不到引用的切入点LoginMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8573954/

相关文章:

java - Hibernate集合在持久化后为NULL

java - Spring 是否要求所有 bean 都具有默认构造函数?

android - Android : pointcut call(* Activity. onCreate(..)) 中的 AspectJ 未挑选出 Activity.onCreate() 调用

c# - Unity IOC、AOP & 接口(interface)拦截

java - libgdx 在矩形内缩放/拖动

Java行星轨道模拟: centering planets

java - 在 Spring Boot 中按 @onetomany 集合大小排序的最佳方法?

c# - 为什么Unity拦截抓不到Exception?

java - 当用户正确插入凭据时,为什么登录对话框不会消失?(JSF 2.0。)

java - 面板无法正确地在 Java 中重新绘制图像?