<project ……> …… <dependencies> …… <!-- [Spring 3] AOP. --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.8</version> </dependency> </dependencies> </project>
……
public class AspectDemoLogger
{
……
public void logBeforeTarget(JoinPoint jp)
{
……
}
……
public Object logAroundTargetProceed(ProceedingJoinPoint pjp)
throws Throwable
{
Object oTarget = pjp.getTarget();
String strTextDesc = getTextDesc(pjp);
System.out.println(TextToolE.concat("[",
DateToolE.simpleFormat(new Date(), "yyyy-MM-yy HH:mm:ss.SSS"),
"] -- Start target ", oTarget, strTextDesc, "."));
Object oRtn = pjp.proceed(pjp.getArgs());
System.out.println(TextToolE.concat("[",
DateToolE.simpleFormat(new Date(), "yyyy-MM-yy HH:mm:ss.SSS"),
"] -- After target ", oTarget, strTextDesc, " return."));
return oRtn;
}
……
public void logAfterTargetReturn(JoinPoint jp)
{
……
}
……
public void logAfterTargetThrow(JoinPoint jp, Throwable t) throws Throwable
{
Object oTarget = jp.getTarget();
System.out.println(TextToolE.concat("[",
DateToolE.simpleFormat(new Date(), "yyyy-MM-yy HH:mm:ss.SSS"),
"] After target ", oTarget, getTextDesc(jp), " throws ", t));
throw t;
}
……
private String getTextDesc(JoinPoint jp)
{
// String strMainDesc = jp.getSignature().toString();
String strMainDesc = jp.getSignature().getName();
Object[] o1dArgs = jp.getArgs();
String strTextDesc = TextToolE.concat(strMainDesc, "(",
ArrayToolE.arrayForConsole(o1dArgs), ")");
return strTextDesc;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
<bean id="aop.aspect.aspectLogger"
class="tfw.integration_demo._06_spring_aop.aspect.AspectDemoLogger"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <import resource="sub_files/06_spring_4_struts.xml"/> <import resource="sub_files/07_spring_aop.xml"/> </beans>
……
public class AspectLoggerTestCase
{
private String strClasspathConfFile =
"tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml";
@Test
public void testObjects() throws IOException
{
System.out.println(
"Spring Configuration File:\n\t" + strClasspathConfFile);
AbstractApplicationContext actxt =
new ClassPathXmlApplicationContext(strClasspathConfFile);
System.out.println("Application Context:\n\t" + actxt);
AspectDemoLogger logAspect = actxt.getBean("aop.aspect.aspectLogger",
AspectDemoLogger.class);
System.out.println("LogAspect:\n\t" + logAspect);
actxt.close();
System.out.println("Application Context Closed:\n\t" + actxt);
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans ……> <bean id="aop.aspect.aspectLogger" ……/> <aop:config> <aop:aspect id="aop.aspect.logAspect" ref="aop.aspect.aspectLogger"> <aop:pointcut id="pointcut_01" expression="execution(* tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.dao.I_UserDAO.*(..))"/> <aop:before pointcut-ref="pointcut_01" method="logBeforeTarget"/> <aop:around pointcut-ref="pointcut_01" method="logAroundTargetProceed"/> <aop:after pointcut-ref="pointcut_01" method="logAfterTargetReturn"/> <aop:after-throwing pointcut-ref="pointcut_01" method="logAfterTargetThrow" throwing="t"/> </aop:aspect> </aop:config> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans ……> <bean id="aop.aspect.aspectLogger" ……/> <aop:config> <aop:aspect id="aop.aspect.logAspect" ref="aop.aspect.aspectLogger"> <aop:pointcut id="pointcut_01" expression="execution(* tfw.integration_demo.._0*_DataSourceConfig*.*(..)) or execution(* tfw.integration_demo.._01_UserMgr*.*(..)) or execution(* tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.dao.I_UserDAO.*(..))"/> <aop:before pointcut-ref="pointcut_01" method="logBeforeTarget"/> …… </aop:aspect> </aop:config> </beans>