spring aop配置

Spring AOP 配置一般有两种
一、注解式的
1.在spring的配置文件中加入
<!-- Annotation aop -->
<!--需要的代理对象-- >
<bean id="logInfoAspect" class="com.cctv.aspect.LogInfoAspect"></bean>
<!--注解方式必要的配置-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
2.在代理对象LogInfoAspect类中
@Aspect
public class LogInfoAspect {
@Autowired
private HttpServletRequest request;
@Resource
private BspDalylogDao dao;
//日志内容模板
private final String CONTENT_TEMPLATE = "@@agentId@@@@date@@调用 @@object@@@@method@@方法";
@Pointcut("execution(* com.cctv.service..*.del*(..))||execution(* com.cctv.service..*.save*(..))||execution(* com.cctv.service..*.add*(..))||execution(* com.cctv.service..*.update*(..))")
private void myMethod(){};
@Pointcut("execution(* com.hwacreate.service..*.del*(..))||execution(* com.cctv.service..*.save*(..))||execution(* com.cctv.service..*.add*(..))||execution(* com.cctv.service..*.update*(..))")
private void throwMethod(){};
@After("myMethod()")
public void addLogInfo(JoinPoint joinPoint){
BspDailyLogState dialLog = dao.findDialLog();
String state = dialLog.getState();
if(state.equals("1")){
//Object[] args = joinPoint.getArgs();
Object target = joinPoint.getTarget();
String method = joinPoint.getSignature().getName();
String userName = "zyc";
String states = "2";
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String remark = CONTENT_TEMPLATE.replace("@@agentId@@", userName+"于")
.replace("@@date@@", date)
.replace("@@object@@", (target == null ? "" : target.getClass().getName()+"的"))
.replace("@@method@@", method);
this.insertlog(date, remark, userName, states);
}
}
/**
* 目标方法抛出异常后
* @param jp
* @param ex 异常信息
*/
@AfterThrowing(pointcut="throwMethod()", throwing="ex")
public void afterThrowing(JoinPoint jp,Exception ex) {
BspDailyLogState dialLog = dao.findDialLog();
String state = dialLog.getState();
if(state.equals("1")){
Object[] args = jp.getArgs();
Object target = jp.getTarget();
String method = jp.getSignature().getName();
String userName = "zyc";
String states="2";
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String remark = CONTENT_TEMPLATE.replace("@@agentId@@", userName + "于").replace("@@date@@", date)
.replace("@@object@@", (target == null ? "" : target.getClass().getName() + "的"))
.replace("@@method@@", method);
remark += ",错误:"+ex.getMessage();
this.insertlog(date, remark, userName, states);
}

}
//插入数据
public void insertlog(String date,String remark,String userName,String state){
BspDailylog dia = new BspDailylog();
dia.setCreateTime(date);
dia.setRemark(remark);
dia.setStatus(state);
dia.setUserName(userName);
dao.save(dia);
}
二、xml方式的配置,比较常用,也是在spring中配置
<!-- 日志监控AOP配置xml
<bean id="logInfoAspect" class="com.hwacreate.aspect.LogInfoAspect"></bean>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* com.cctv.service..*.del*(..))||execution(* com.cctv.service..*.save*(..))||execution(* com.cctv.service..*.add*(..))||execution(* com.cctv.service..*.update*(..))" id="logPointCut"/>
<aop:aspect id="logAspect" ref="logInfoAspect">
<aop:after method="addLogInfo" pointcut-ref="logPointCut" />
</aop:aspect>
</aop:config>-->
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值