KPax
asked on
Error in @AspectJ Based AOP with Spring
Here are my classes:
MainApp.java
Student.java
Logging.java
And I get this error:
When I debug Student object is properly initialized with both properties ('name' and 'age' ) but exception is raised on:
System.out.println("Return ing:" + retVal.toString());
in
Because retVal is null. Any help with this?
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
student.getName();
student.getAge();
// student.printThrowException();
}
}
Student.java
package com.tutorialspoint;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age);
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name);
return name;
}
public void printThrowException() {
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
Logging.java
package com.tutorialspoint;
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 {
/**
* Following is the definition for a pointcut to select all the methods
* available. So advice will be called for all the methods.
*/
@Pointcut("execution(* com.tutorialspoint.*.*(..))")
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());
}
}
Beans.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: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 ">
<aop:aspectj-autoproxy />
<!-- Definition for student bean -->
<bean id="student" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11" />
</bean>
<!-- Definition for logging aspect -->
<bean id="logging" class="com.tutorialspoint.Logging" />
</beans>
And I get this error:
Going to setup student profile.
Jan 20, 2017 9:47:40 PM org.springframework.context.support. ClassPathX mlApplicat ionContext prepareRefresh
INFO: Refreshing org.springframework.context.support. ClassPathX mlApplicat ionContext @6e8cf4c6: startup date [Fri Jan 20 21:47:40 CET 2017]; root of context hierarchy
Jan 20, 2017 9:47:40 PM org.springframework.beans.factory.xm l.XmlBeanD efinitionR eader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Going to setup student profile.
Student profile has been setup.
Going to setup student profile.
Student profile has been setup.
Jan 20, 2017 9:47:40 PM org.springframework.context.support. ClassPathX mlApplicat ionContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.Be anCreation Exception: Error creating bean with name 'student' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans. PropertyBa tchUpdateE xception; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvo cationExce ption: Property 'name' threw exception; nested exception is java.lang.NullPointerExcep tion
PropertyAccessException 2: org.springframework.beans.MethodInvo cationExce ption: Property 'age' threw exception; nested exception is java.lang.NullPointerExcep tion
Student profile has been setup.
Exception in thread "main" org.springframework.beans.factory.Be anCreation Exception: Error creating bean with name 'student' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans. PropertyBa tchUpdateE xception; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvo cationExce ption: Property 'name' threw exception; nested exception is java.lang.NullPointerExcep tion
PropertyAccessException 2: org.springframework.beans.MethodInvo cationExce ption: Property 'age' threw exception; nested exception is java.lang.NullPointerExcep tion
at org.springframework.beans.factory.su pport.Abst ractAutowi reCapableB eanFactory .applyProp ertyValues (AbstractA utowireCap ableBeanFa ctory.java :1529)
at org.springframework.beans.factory.su pport.Abst ractAutowi reCapableB eanFactory .populateB ean(Abstra ctAutowire CapableBea nFactory.j ava:1237)
at org.springframework.beans.factory.su pport.Abst ractAutowi reCapableB eanFactory .doCreateB ean(Abstra ctAutowire CapableBea nFactory.j ava:552)
at org.springframework.beans.factory.su pport.Abst ractAutowi reCapableB eanFactory .createBea n(Abstract AutowireCa pableBeanF actory.jav a:483)
at org.springframework.beans.factory.su pport.Abst ractBeanFa ctory$1.ge tObject(Ab stractBean Factory.ja va:306)
at org.springframework.beans.factory.su pport.Defa ultSinglet onBeanRegi stry.getSi ngleton(De faultSingl etonBeanRe gistry.jav a:230)
at org.springframework.beans.factory.su pport.Abst ractBeanFa ctory.doGe tBean(Abst ractBeanFa ctory.java :302)
at org.springframework.beans.factory.su pport.Abst ractBeanFa ctory.getB ean(Abstra ctBeanFact ory.java:1 97)
at org.springframework.beans.factory.su pport.Defa ultListabl eBeanFacto ry.preInst antiateSin gletons(De faultLista bleBeanFac tory.java: 759)
at org.springframework.context.support. AbstractAp plicationC ontext.fin ishBeanFac toryInitia lization(A bstractApp licationCo ntext.java :866)
at org.springframework.context.support. AbstractAp plicationC ontext.ref resh(Abstr actApplica tionContex t.java:542 )
at org.springframework.context.support. ClassPathX mlApplicat ionContext .<init>(Cl assPathXml Applicatio nContext.j ava:139)
at org.springframework.context.support. ClassPathX mlApplicat ionContext .<init>(Cl assPathXml Applicatio nContext.j ava:83)
at com.tutorialspoint.MainApp.main(Main App.java:9 )
Caused by: org.springframework.beans.PropertyBa tchUpdateE xception; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvo cationExce ption: Property 'name' threw exception; nested exception is java.lang.NullPointerExcep tion
PropertyAccessException 2: org.springframework.beans.MethodInvo cationExce ption: Property 'age' threw exception; nested exception is java.lang.NullPointerExcep tion
at org.springframework.beans.AbstractPr opertyAcce ssor.setPr opertyValu es(Abstrac tPropertyA ccessor.ja va:121)
at org.springframework.beans.AbstractPr opertyAcce ssor.setPr opertyValu es(Abstrac tPropertyA ccessor.ja va:75)
at org.springframework.beans.factory.su pport.Abst ractAutowi reCapableB eanFactory .applyProp ertyValues (AbstractA utowireCap ableBeanFa ctory.java :1525)
... 13 more
When I debug Student object is properly initialized with both properties ('name' and 'age' ) but exception is raised on:
System.out.println("Return
in
@AfterReturning(pointcut = "selectAll()", returning = "retVal")
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString());
}
Because retVal is null. Any help with this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER