【问题标题】:nullpointer exception in aspectJ exampleaspectJ 示例中的空指针异常
【发布时间】:2015-10-31 00:33:00
【问题描述】:

我正在尝试实施我们的 stackoverflow 成员在此处 Logging entry, exit and exceptions for methods in java using aspects 给出的建议之一。由于这本身就是一个不同的问题,所以再次在这里发布。

我尝试过搜索,但看起来不同的版本有不同的方法,无法在网上找到一个例子。我尝试了以下简单示例,因为我是面向方面编程的新手并且不知道如何实现。这个例子是抛出 NPE。请帮助我了解我在哪里做错了。

==== 异常

Exception in thread "main" java.lang.NullPointerException
at aoplogging.SimpleCall.call(SimpleCall.java:13)
at aoplogging.App.main(App.java:18)

就在SimpleService.simpleCall();

应用程序上下文:

<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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">


<aop:aspectj-autoproxy />
<bean id="simpleCall" class="aoplogging.SimpleCall" />

================== 应用程序.java

package aoplogging;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {


public static void main(String[] args) {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    SimpleCall call =(SimpleCall) context.getBean("simpleCall");
    call.call();
    context.close();
}

============ SimpleCall.java 打包 aoplogging;

import org.springframework.beans.factory.annotation.Autowired;

public class SimpleCall {

@Autowired
private SimpleService SimpleService;

public void call(){


    SimpleService.simpleCall();
    try {
        SimpleService.processingOperator();
    } catch (SMSProcessingException | SMSSystemException e) {
        e.printStackTrace();
    }
}

}

=====Logging.java

package aoplogging;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Logging {
@Pointcut("execution(* aoplogging.*.*(..))")
   private void selectAll(){}

   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("selectAll()")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }

   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   @After("selectAll()")
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }

   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   @AfterReturning(pointcut = "selectAll()", returning="retVal")
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }

   /**
    * This is the method which I would like to execute
    * if there is an exception raised by any method.
    */
   @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }

}

【问题讨论】:

    标签: java spring aspectj spring-aop pointcut


    【解决方案1】:

    我支持我的建议;)。

    Spring AOP/AspectJ 的使用在使用不是通过 using 接口注入的 bean 时存在问题(使用接口吃了注入点),因为接口是由 AspectJ 代理的。

    有一种方法可以通过将proxy-target-class="true" 添加到

    <aop:aspectj-autoproxy />
    

    但这不是一个好方法。

    使用接口更加简单和安全。


    编辑:另一个错误是您缺少实现 SimpleService 的 bean。添加会更容易

    <context:component-scan base-package="aoplogging" />
    

    到您的 applicationContext.xml。

    然后,你必须标记所有的豆子

    @Component
    

    为了让 Spring 知道它们是 Spring 应该实例化的 bean。


    编辑:方面必须同时使用 @Aspect@Component 进行注释,以便让 Spring 检测到它。

    【讨论】:

    • 你指的接口是什么? SimpleCall call =(SimpleCall) context.getBean("simpleCall");
    • 这里最好引入一个接口。但我发现了错误:您没有任何实现 SimpleService 的 bean,因此 Spring 无法自动装配它(通常您会得到一个异常......)。将 添加到 applicationContext.xml 会更容易。
    • 没问题。一切都按预期记录了吗?
    • 没有。 Logging.java中定义的Pointcut没有被调用。
    • 尝试用@Component 标记切面。
    猜你喜欢
    • 2014-07-06
    • 2017-08-28
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多