java - nullpointer exception in aspectJ example -


i trying implement 1 of suggestion given our stackoverflow member here logging entry, exit , exceptions methods in java using aspects . since different question in itself, posting here again.

i have tried search looks different versions have different ways of doing , unable figure out example online. have tried following simple example since new aspect oriented programming , couldn't figure out how implement. example throwing npe. please me understand doing wrong.

==== exception

exception in thread "main" java.lang.nullpointerexception @ aoplogging.simplecall.call(simplecall.java:13) @ aoplogging.app.main(app.java:18) 

exactly @ 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(){}     /**      * method execute     * before selected method execution.     */    @before("selectall()")    public void beforeadvice(){       system.out.println("going setup student profile.");    }     /**      * method execute     * after selected method execution.     */    @after("selectall()")    public void afteradvice(){       system.out.println("student profile has been setup.");    }     /**      * method execute     * when method returns.     */    @afterreturning(pointcut = "selectall()", returning="retval")    public void afterreturningadvice(object retval){       system.out.println("returning:" + retval.tostring() );    }     /**     * method execute     * if there exception raised method.     */    @afterthrowing(pointcut = "selectall()", throwing = "ex")    public void afterthrowingadvice(illegalargumentexception ex){       system.out.println("there has been exception: " + ex.tostring());       } 

}

i supporting suggestion ;).

the usage of spring aop/aspectj problematic when using beans not injected using interface (using interface ate injection point), interfaces proxied aspectj.

there way come around adding proxy-target-class="true" to

<aop:aspectj-autoproxy /> 

but not nice way.

it more simpler , safer use interfaces.


edit: error missing bean implementing simpleservice. easier add

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

to applicationcontext.xml.

then, have tag beans with

@component 

in order let spring know beans spring should instantiate.


edit: aspect has annotated both @aspect , @component in order let spring detect it.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -