spring - Logging entry, exit and exceptions for methods in java using aspects -


i using below code log entry, exit , exceptions using aspects. way have define bean every class in application in applicationcontext , becomes cumbersome maintain such length of bean definitions , properties. can me simplify this? don't think appropriate design define bean everytime create class. appreciated, in advance.

<bean id="logentrybean" class="com.example.logging.logentry" /> <bean id="logexitbean" class="com.example.logging.logreturn" /> <bean id="logexceptionbean" class ="com.example.logging.exceptionlogger"/>  <bean id="simpleserviceproxy" class="org.springframework.aop.framework.proxyfactorybean">     <property name="target" ref="simpleservicebean" />     <property name="interceptornames">         <list>             <value>logentrybean</value>             <value>logexitbean</value>             <value>logexceptionbean</value>         </list>     </property> </bean>      <bean id="secondserviceproxy" class="org.springframework.aop.framework.proxyfactorybean">     <property name="target" ref="secondservicebean" />     <property name="interceptornames">         <list>             <value>logentrybean</value>             <value>logexitbean</value>             <value>logexceptionbean</value>         </list>     </property> </bean> 

==================== logentry class:

public class logentry implements methodbeforeadvice { private final static logger logger = logger.getlogger(logentry.class);  public void before(method method, object[] args, object target) throws throwable {      if(logger.isdebugenabled())     logger.info("logged entry method : " + method.getname()); } 

========================= logreturn class:

public class logreturn implements afterreturningadvice { private final static logger logger = logger.getlogger(logreturn.class);  public void afterreturning(object returnvalue, method method, object[] args, object target) throws throwable {     if(logger.isdebugenabled())     logger.info("logged exit method : " + method.getname()); } 

============================== exceptionlogger class

public void afterthrowing(exception r) throws throwable {      loadproperties();      // log exceptions     logger.error("exception  : " + r.getmessage());     logger.error(r);      if (logger.isdebugenabled()) {         // logging complete stacktrace in better format         stringwriter sw = new stringwriter();         printwriter pw = new printwriter(sw);         r.printstacktrace(pw);         logger.debug((sw.tostring()));     }     // sendmail();      if (r instanceof processingexception) {          throw new outputexception(prop.getproperty("er004"), r);     } else if (r instanceof systemexception) {         throw new outputexception(error.database.tostring(), r);     }  }  public void loadproperties() {      // use try resource     try {          // load properties file         input = getclass().getclassloader().getresourceasstream("errorcodes.properties");         prop.load(input);      } catch (ioexception ex) {         ex.printstacktrace();     } {         if (input != null) {             try {                 input.close();             } catch (ioexception e) {                 e.printstacktrace();             }         }     }  } 

======================= main method:

public class app { public static void main(string[] args) {     configurableapplicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");     simpleservice simpleservice = (simpleservice) context.getbean("simpleserviceproxy");     secondservice secondservice = (secondservice) context.getbean("secondserviceproxy");     // simple call demo entry , exit logging methods of     // class     simpleservice.simplecall();     secondservice.test2();     try {         // processing logic exception generated , handled         simpleservice.processingoperator();     } catch (exception e) {          system.out.println(e);         // todo         // handle exception     }      // test service on different bean     secondservice.test3();     context.close(); } 

}

you can use spring's component scan autowire beans.

simply add

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

to applicationcontext.xml. have replace "" package want scan beans.

also, have annotate beans with

@component 

to "hey, bean" spring. inject dependencies other beans, tag field with

@autowired 

and injected.

i hope :).

~fabian


edit: how use spring-aop , aspectj.

you have add

<aop:aspectj-autoproxy /> 

to applicationcontext.xml. then, have define class contain aspects. class basic spring bean, annotated with

@component 

then have define join-points want execute logging via pointcuts.

example:

@around("execution(* <base_package>..*.* (..))") public object logall(preecedingjoinpoint joinpoint) throws throwable {     object result = null;     throwable throwable = null;      final stopwatch stopwatch = new stopwatch();     stopwatch.start();     try {         result = joinpoint.proceed();     } catch (throwable t) {         throwable = t;     }     stopwatch.stop();      if (throwable == null) {         logger.debug("executed " + joinpoint.getsignature() + " in " + stopwatch.gettime() + "ms!");     } else {         logger.debug("executed " + joinpoint.getsignature() + " in " + stopwatch.gettime() + "ms! threw exception: " + throwable);         throw throwable;     }     return result; } 

you continue execution via joinpoint.proceed(). important return result! otherwise, every method return null! also, must throw thrown exception, otherwise suppressed.


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 -