AOP-afterReturning抛出NullPointerException

时间:2015-07-17 16:33:08

标签: java spring-aop

Spring配置文件

<?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:aop="http://www.springframework.org/schema/aop"
    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" >


<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy />

<bean id="studService" class="springAOP.Student">
    <property name="age" value="20"></property>
    <property name="name" value="Sangeetha"></property>
</bean>

<bean id="logging" class="springAOP.Logging"/>

<aop:config>

    <aop:aspect id="log" ref="logging" >
        <aop:pointcut id="student" expression="execution(* springAOP.Student.*(..))" />

        <!--  before advice definition -->
        <aop:before pointcut-ref="student" method="beforeAdvice"/>
        <!-- after advice definition -->
        <aop:after pointcut-ref="student" method="afterAdvice"/>
        <!--  after-returning advice -->
        <aop:after-returning pointcut-ref="student" method="afterReturningAdvice" returning="retVal"/>
        <!--  after throwing advice -->
        <aop:after-throwing pointcut-ref="student" method="afterThrowingAdvice" throwing="ex"/>
        <!--  around advice  -->
        <aop:around pointcut-ref="student" method="aroundAdvice"/>

    </aop:aspect>
</aop:config>

申请类

package spring;

import springAOP.Student;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SpringApp {
    private  AbstractApplicationContext context;    
    HelloWorld obj;
    Student student;

    public SpringApp()
    {
        context = new ClassPathXmlApplicationContext("spring-beans.xml");
    }

    public void initializeBeans(){
        obj = (HelloWorld)context.getBean("sayHello");          
        student = (Student)context.getBean("studService");      
    }


    public static void main(String args[]){
        SpringApp app = new SpringApp();
        app.initializeBeans();
        System.out.println(" getting name and age");

        Student stud = (Student)app.context.getBean("studService");
        stud.getName(); 
        stud.getAge();
    }

EntityClass

package springAOP;

public class Student {
    private Integer age;
    private String name;

    public void setAge(Integer age) {
        this.age = age;
        System.out.println("Setting Age : " + age);
    }

    public Integer getAge() {
        System.out.println("Age : " + age);
        return age;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Setting Name : " + name);
    }

    public String getName() {
        System.out.println("Name : " + name);
        return name;
    }

    public void printThrowException() {
        System.out.println("Exception raised");
        throw new IllegalArgumentException();
    }
}

输出:

Jul 17, 2015 9:51:50 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ad9418: startup date [Fri Jul 17 21:51:50 IST 2015]; root of context hierarchy
Jul 17, 2015 9:51:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beans.xml]
Jul 17, 2015 9:51:52 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e2abd: defining beans [org.springframework.aop.config.internalAutoProxyCreator,studService,logging,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,student,employee,employeeService,dataSource,sayHello,spellCheck]; root of factory hierarchy
Setting Age : 20
Setting Name : Sangeetha
Jul 17, 2015 9:51:53 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
 Setting Message is : you are in spring Hello World
 i m in init
 i 2 j 3
Going to setup student profile.
After smooth execution 
Returning:null
Student profile has been setup.
Going to setup student profile.
After smooth execution 
Returning:null   ==> returning NULL
Student profile has been setup.

Logging.java:

package springAOP;

public class Logging {

    /**
     * * This is the method which I would like to execute * before a selected
     * method execution.
     */

    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.
     */
    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.
     */
    public void afterReturningAdvice(String retVal) {
        System.out.println("Returning:" + retVal);
    }

    /**
     * * This is the method which I would like to execute * if there is an
     * exception raised.
     */
    public void afterThrowingAdvice(IllegalArgumentException ex) {
        System.out.println("There has been an exception: " + ex.toString());
    }

    public void aroundAdvice(){
        System.out.println("After smooth execution " );
    }
}

问题:

当访问getNamegetAge方法时,afterReturning未获得retValue,值将传递为NULL。请帮助我理解为什么价值被传递为NULL

1 个答案:

答案 0 :(得分:0)

您可以使用aspectJ的ProceedingJoinPoint

public void afterReturningAdvice(ProceedingJoinPoint joinpoint) {
    Object[] args = joinpoint.getArgs()
    Object retVal = null;
    if(args.length > 0) {
        retVal = args[0];
    }
    System.out.println("Returning:" + retVal);
}