nullpointer exception in aspectJ example

2019-06-26 05:03发布

问题:

I am trying to implement one of the suggestion given by our stackoverflow member here Logging entry, exit and exceptions for methods in java using aspects . Since this is different question in itself, posting here again.

I have tried to search but looks like different versions have different ways of doing it and unable to figure out an example online. I have tried the following simple example since I am new to aspect oriented programming and couldn't figure out how to implement. This example is throwing NPE. Please help me understand where I am doing it wrong.

==== Exception

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

Exactly at SimpleService.simpleCall();

ApplicationContext:

<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" />

================== App.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 package 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());   
   }

}

回答1:

I am supporting my suggestion ;).

The usage of Spring AOP/AspectJ is problematic when using beans that are not injected by using interface (using the interface ate the injection point), as the interfaces are proxied by AspectJ.

There is a way to come around this by adding proxy-target-class="true" to

<aop:aspectj-autoproxy />

but that is not a nice way.

It is much more simpler and safer to use interfaces.


EDIT: Another error is that you are missing a bean that is implementing SimpleService. It would be easier to add

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

to your applicationContext.xml.

Then, you have to tag all beans with

@Component

in order to let Spring know that they are beans that Spring should instantiate.


EDIT: The aspect has to be annotated with both @Aspect and @Component in order to let Spring detect it.