簡體   English   中英

AOP-afterReturning引發NullPointerException

[英]AOP-afterReturning throws NullPointerException

彈簧配置文件

<?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傳遞

您可以使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM