spring configuration file
<?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> application class
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 , 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(); } } output:
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 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 : in spring hello world m in init 2 j 3 going setup student profile. after smooth execution returning:null student profile has been setup. going setup student profile. after smooth execution returning:null ==> returning null student profile has been setup. logging.java:
package springaop; public class logging { /** * * method execute * before selected * method execution. */ public void beforeadvice() { system.out.println("going setup student profile."); } /** * * method execute * after selected * method execution. */ public void afteradvice() { system.out.println("student profile has been setup."); } /** * * method execute * when method * returns. */ public void afterreturningadvice(string retval) { system.out.println("returning:" + retval); } /** * * method execute * if there * exception raised. */ public void afterthrowingadvice(illegalargumentexception ex) { system.out.println("there has been exception: " + ex.tostring()); } public void aroundadvice(){ system.out.println("after smooth execution " ); } } issue:
when getname , getage methods accessed, afterreturning not getting retvalue, value passed null. kindly me understand why value passed null
you can use proceedingjoinpoint of aspectj.
public void afterreturningadvice(proceedingjoinpoint joinpoint) { object[] args = joinpoint.getargs() object retval = null; if(args.length > 0) { retval = args[0]; } system.out.println("returning:" + retval); }
Comments
Post a Comment