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>-->

猜你喜欢

转载自zhouyunchao.iteye.com/blog/2169440