aop

aop:面向切面编程,它可以解决重复代码。

aop有两种方式:

一、.xml方式

1、在springmvc-servlet.xml中配置aop,应用bean文件;

<!--aop配置-->
<aop:config>
<aop:aspect id="log" ref="loging">
<!--第一个星号:返回值
第二个星号:类
第三个星号:方法
小括号:方法入参-->
<aop:pointcut id="print" expression="execution(* com.dait.controller.*.*(..))"/>
<!-- <aop:before pointcut-ref="print" method="doBefore"/>
<aop:after pointcut-ref="print" method="doAfter"/>-->
<aop:around pointcut-ref="print" method="doAround"/>
<aop:after-throwing pointcut-ref="print" method="doThrowing" throwing="ex"/>
</aop:aspect>
</aop:config>

2、在bean文件的类上加@Component


@Component
public class Loging { /**
* 目标方法执行之前调用
* @param
*/
/* public void doBefore(JoinPoint jp) {
System.out.println(">>>>>doBefore>>>>>>>log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}*/ public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(">>>>>doAround>>>>>>>log Begining method: " + pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName()); Object retVal = pjp.proceed();//执行目标方法 System.out.println(">>>>>doAround>>>>>>>log Ending method: " + pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName()); return retVal;
}
}

二、采用注解的方式

1、在springmvc-servlet.xml文件中添加注解配置

<!--启用注解代理-->
<aop:aspectj-autoproxy/>

2、在bean文件上添加@Component和@Aspect,缺一不可

@Component
@Aspect
public class LogingAnnotation { public void doBefore(JoinPoint jp) {
System.out.println(">>>>>doBefore>>>>>>>log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
} @Around("execution(* com.dait.controller.*.*(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
System.out.println(">>>>>doAround>>>>>>>log Begining method: " + pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName());
Object retVal = pjp.proceed();//执行目标方法
System.out.println(">>>>>doAround>>>>>>>log Ending method: " + pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName()); time = System.currentTimeMillis() - time;
//pjp.getTarget().getClass().getName() 目前类包+类名
//pjp.getSignature().getName() 目标方法 return retVal;
} public void doAfter(JoinPoint jp) {
System.out.println(">>anno>>>doAfter>>>>>>>log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
} public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println(">>anno>>>doThrowing>>>>>>>method " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw exception");
System.out.println(ex.getMessage());
}
}

学习JavaWeb aop两种配置方式的更多相关文章

  1. spring aop两种配置方式

    基于注解的Spring AOP开发 简单案例快速入门 定义目标类接口和实现类 /** * Created by zejian on 2017/2/19.*/ //接口类 public interfac ...

  2. spring aop两种配置方式(1)

    第一种:注解配置AOP注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before ...

  3. java框架篇---spring aop两种配置方式

    第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Befor ...

  4. hibernate 一对一 one to one的两种配置方式

    hibernate中one-to-one两种配置方式 标签: hibernateHibernateone-to-one 2013-02-19 17:44 11445人阅读 评论(1) 收藏 举报  分 ...

  5. Hibernate中双向多对多的两种配置方式

    Hibernate中双向多对多的两种配置方式 1.建立多对多双向关联关系 package cn.happy.entitys; import java.util.HashSet; import java ...

  6. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  7. Spring AOP两种实现方式

    一. AOP 概念: Spring AOP 即Aspect Oriented Programming(面向切面编程), 实现方式分为两种: 1. 注解(Annotation) 2. 配置(Config ...

  8. IOC容器在web容器中初始化——(一)两种配置方式

    参考文章http://blog.csdn.net/liuganggao/article/details/44083817,http://blog.csdn.net/u013185616/article ...

  9. 第四节:框架前期准备篇之进程外Session的两种配置方式

    一. 基本介绍 1. 背景:Asp.Net默认的Session机制是进程内,存储在服务器端内存中,有这么几个缺点: ①:既然存在内存中,空间有限,不能存储大数据量信息,数据量多的话Session会被挤 ...

随机推荐

  1. Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)

    这个问题在之前做项目时碰到过一次,当时按照网上的做法,去掉JSON.parse()这一层转换后就没有这个报错了,数据也能正常使用,就没多想,也没深究是什么原因.可是这次又碰到了,所以这次我必须要弄明白 ...

  2. WPF 手机验证码 发送按钮倒计时 代码

    private async void SendButton_Click(object sender, RoutedEventArgs e) { var button = sender as Butto ...

  3. Visualizing LSTM Layer with t-sne in Neural Networks

    LSTM 可视化 Visualizing Layer Representations in Neural Networks Visualizing and interpreting represent ...

  4. dev gridview指定单元格cell获取坐标

    DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo Info2 = gvQueryResult.GetViewInfo() as DevExpre ...

  5. use ROW_NUMBER() for pagination in Oracle and SQLServer

    ------------------------------------------------------------------------Oracle---------------------- ...

  6. c 字符串替换字符

    使用完释放记得内存free(str),防止内存泄露 char * replace (const char *str, const char *src, const char *dst){ const ...

  7. LeetCode题解之Find the Difference

    1.题目描述 2.题目分析 比较两个字符串中加入的一个字符,由于可能在字符串中加入一个已经存在的字符,因此使用hash table 去统计字符个数最好. 3.代码 char findTheDiffer ...

  8. 转:Eclipse+webservice开发实例

    原文地址:http://blog.csdn.net/xw13106209/article/details/7049614 1.参考文献: 1.利用Java编写简单的WebService实例  http ...

  9. 转:eclipse的快捷键

    22 21 Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到 ...

  10. Django的model中创建表

    类中的class Meta字段的作用: 第一个作用可以给这个类起名字 在后台的admin中显示这个类名字 class CourseCategory(models.Model): "" ...