使用aop需要在网上下载两个jar包:

  1. aopalliance.jar
  2. aspectjweaver.jar

为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将刚才下载好的jar添加进去

前置增强实例

编写TimeHandler.java

 package com.example.spring;

 public class TimeHandler {
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
}
}

编写HelloWorld.java

 package com.example.spring;

 public class HelloWorld {
public void printHello(){
System.out.println("Hello Aop.");
}
}

编写配置文件

 <?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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 ,method:增强类()里面的方法(beforTime())作为前置-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:before method="beforTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>

编写Application.java

 package com.example.spring;

 import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\aop.xml");
//得到配置创建的对象
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.printHello();
}
}

运行输出

前置增强:CurrentTime = 1510134673408
Hello Aop.

可以看到,打印Hello Aop.之前会先打印当前的时间CurrentTime = 1510132664923。

后置增强实例

修改TimeHandler.java

 package com.example.spring;

 import org.aspectj.lang.ProceedingJoinPoint;

 public class TimeHandler {
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
} public void afterTime()
{
System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis());
}
}

修改配置文件aop.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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<!--<aop:before method="beforTime" pointcut-ref="pointcut1"/>--> <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:after method="afterTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>

运行输出

Hello Aop.
后置增强:CurrentTime = 1510134850754

环绕增强实例

修改TimeHandler.java

 package com.example.spring;

 import org.aspectj.lang.ProceedingJoinPoint;

 public class TimeHandler {
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
} public void afterTime()
{
System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis());
} public void aroundTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//方法之前
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis()); //执行被增强的方法
proceedingJoinPoint.proceed(); //方法之后
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis());
} }

修改aop.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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<!--<aop:before method="beforTime" pointcut-ref="pointcut1"/>--> <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<!--<aop:after method="afterTime" pointcut-ref="pointcut1"/>--> <!-- 配置环绕增强类型 method:增强类()里面的方法(aroundTime())作为环绕通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:around method="aroundTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>

运行输出

环绕增强:CurrentTime = 1510135559066
Hello Aop.
环绕增强:CurrentTime = 1510135559074

之后就不用修改源程序,只需通过调整配置文件,就可以调整程序的逻辑。

修改配置文件

 <?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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:before method="beforTime" pointcut-ref="pointcut1"/> <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:after method="afterTime" pointcut-ref="pointcut1"/> <!-- 配置环绕增强类型 method:增强类()里面的方法(aroundTime())作为环绕通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:around method="aroundTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>

运行输出

前置增强:CurrentTime = 1510190036105
环绕增强:CurrentTime = 1510190036105
Hello Aop.
环绕增强:CurrentTime = 1510190036122
后置增强:CurrentTime = 1510190036122

Java框架spring 学习笔记(十二):aop实例操作的更多相关文章

  1. Java框架spring 学习笔记(二):Bean的作用域

    Spring 框架Bean支持以下五个作用域: 下面介绍两种作用域,singleton和protoype singleton作用域 singleton作用域为默认作用域,在同一个ioc容器内getBe ...

  2. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  3. Java框架spring 学习笔记(十四):注解aop操作

    回见Java框架spring Boot学习笔记(十三):aop实例操作,这里介绍注解aop操作 首先编写一个切入点HelloWorld.java package com.example.spring; ...

  4. Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)

    配置文件和注解混合使用 创建对象操作使用配置文件方式实现 注入属性的操作使用注解方式实现 编写BookDao.java和OrderDao.java文件 BookDao.java package com ...

  5. Spring学习记录(十二)---AOP理解和基于注解配置

    Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...

  6. Java框架spring 学习笔记(七):Spring相关概念

    Spring是开源.轻量级.一站式框架. Spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码实现. ioc:控制反转,把对象的创建交给Spring进行配置,比如一个类,在类里面有 ...

  7. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  8. Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate

    连接池可以大大提高数据库的性能和连接速度,将那些已连接的数据库连接存放在一个连接池里,以后别人要连接数据库的时候,将不会重新建立数据库连接,直接从连接池中取出可用的连接,用户使用完毕后,会释放连接重新 ...

  9. Java框架spring 学习笔记(十九):事务管理(注解管理)

    注解管理的方式要比xml配置方式要简单很多 只需在配置文件中添加事务注解 <?xml version="1.0" encoding="UTF-8"?> ...

随机推荐

  1. CPU的核、进程和线程

    转自https://www.cnblogs.com/-new/p/7234332.html 一.CPU与核心 物理核 物理核数量=cpu数(机子上装的cpu的数量)*每个cpu的核心数 虚拟核 所谓的 ...

  2. php7带来的性能升级

    前言本文是一篇讲座听后+后续研究的总结. 话说当年追时髦,php7一出就给电脑立马装上了,php5和php7共存,也是立马写了个超级耗时间的循环脚本测了一番,确实php7给力很多,然后也是注意了一些新 ...

  3. php中cookie和session的总结

    cookie: 设置cookie:  setcookie("name","zhang","time()+3600"); 参数一:属性名 参数 ...

  4. 20175311 《Java程序设计》第三周学习总结

    20175311 2018-2019-2 <Java程序设计>第3周学习总结 教材学习内容总结 在蓝墨云中的教程里学习了如何安装IDEA,并且尝试了自己破解IDEA 主要在看书时,对jav ...

  5. Linux7.2 UDEV

    1. 生成规则文件 touch /etc/udev/rules.d/99-oracle-asmdevices.rules 或者 touch /usr/lib/udev/rules.d/99-oracl ...

  6. js滚动到指定位置

    序言:在网络上百度,关键字:“js div滚动到指定位置”,结果基本上大同小异!各种大神都给我们总结出来了四种滚动到指定位置的办法,可惜再下愚钝,每个都不会用,所以写了一个超级简单的方法来使初学者一看 ...

  7. Android自定义万能Canvas画布

    一.需求: 1.在自定义的画布中实现可缩放手势,摇一摇可对控件进行整理排序: 2.画布中可以添加位置设定的控件,控件可以响应点击.长按.拖动事件: 3.控件A长按事件会隐藏画布中的控件除了A之外,显示 ...

  8. MyPubMedID

    MyPubMedID是北京同舟云信息技术公司全新开发的新一代生物医学文献检索与分析平台. 该平台对PubMed全部内容进行重新清洗.组织.挖掘和开发,在完全消除PubMed检索歧义的同时,能够保证检索 ...

  9. Android 开发 Camera类的拍照与录像

    前言 在开发Android应用的时候,如果需要调用摄像头拍照或者录像,除了通过Intent调用系统现有相机应用进行拍照录像之外,还可以通过直接调用Camera硬件去去获取摄像头进行拍照录像的操作.本篇 ...

  10. 解决uni-app props 传递数组修改后不能使用问题

    1.子组件页面结构 //NoticesMarquee 组件 <view v-for="(item, index) in tempList" :key="index& ...