1.需要在應用程序的classpath中引入兩個AspectJ庫:aspectjweaver.jar 和 aspectjrt.jar。
2.servlet.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> //自動掃描包 <context:component-scan base-package="test.web.controller" /> </beans>
3.java
@AfterReturning("execution(* test.service.*.save(..))")
public void afterSaveAdviceDo(JoinPoint joinPoint) {
try {
Log entity = new Log();
String content = "";
//獲取save的實體;
Object object = joinPoint.getArgs()[0];
content = _buildContentByReflex(object, Constants.SYS_ACTION_SAVE);
Administrator admin = AspectHelper.getCurrentAdministrator(object);
SystemInfo info = new SystemInfo();
info.setCreateDate(new Date());
info.setCreateUser(admin);
info.setLastModifyDate(new Date());
info.setLastModifyUser(admin);
entity.setSystemInfo(info);
entity.setContent(content);
logService.write(entity);
} catch (Exception e) {
if (log.isErrorEnabled())
log.error(e, e);
}
}