applicationContextAop.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<bean id="customerDeviceService" class="com.hdst.service.impl.CustomerDeviceVService_Impl"></bean>
<bean id="alarmInfoTService" class="com.hdst.service.impl.AlarmInfoTService_Impl"></bean>
<bean id="alarmSettingInfoTService" class="com.hdst.service.impl.AlarmSettingInfoTService_Impl"></bean>
<bean id="scriptInfoTService" class="com.hdst.service.impl.ScriptInfoTService_Impl"></bean>
<bean id="areaManageService" class="com.hdst.service.impl.AreaManageService_Impl"></bean>
<bean id="customerInfoManageService" class="com.hdst.service.impl.CustomerInfoManageService_Impl"></bean>
<bean id="deviceManageService" class="com.hdst.service.impl.DeviceManageService_Impl"></bean>
<bean id="measurePointRelationService" class="com.hdst.service.impl.MeasurePointRelationService_Impl"></bean>
<bean id="logAdvice" class="com.hdst.aop.SystemLogAdvice"/>
<aop:config>
<aop:aspect id="logAspect" ref="logAdvice">
<aop:pointcut id="anyMethod" expression="@annotation(com.hdst.annotation.MethodDescriptionAnnotation)" />
<aop:before method="generateLog" pointcut-ref="anyMethod" />
</aop:aspect>
</aop:config>
</beans>
SystemLogAdvice.java
public class SystemLogAdvice {
private void generateLog(JoinPoint joinPoint){
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
Method method=ms.getMethod();
Object[] args = joinPoint.getArgs();
String operationArgs = "";
for(int i=0;i<args.length;i++){
operationArgs+=args[0].toString()+";";
}
String adminUserName = Constant.adminUser.getUsername();
String operationDescription = method.getAnnotation(MethodDescriptionAnnotation.class).methodDescription();
String sql = "insert into device_log_info_t_"+((new Date()).getYear()+1900)+((new Date()).getMonth()+1)+" values("+
"null,'"+adminUserName+"',now()"+",'"+operationDescription+"','"+operationArgs+"')";
HibernateUtil.executeSQLUpdate(sql, null);
System.out.println(sql);
}
}
MethodDescriptionAnnotation.java
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MethodDescriptionAnnotation {
String methodDescription();
}
这里使用的是jdk代理,拦截在service上
public interface DeviceManageService_Intf {
......
@MethodDescriptionAnnotation(methodDescription = "新增设备")
int insertDevice(String areaId,DeviceInfoT deviceInfoT,String openVolume,String type,String oldId);
@MethodDescriptionAnnotation(methodDescription = "修改设备信息")
int updateDevice(MeasurementPointDeviceInfoV measurementPointDeviceInfoV);
int getCount(String adminId,String search_text);
}