Java框架spring 学习笔记(十二):aop实例操作
2017-11-08 17:30
716 查看
使用aop需要在网上下载两个jar包:
aopalliance.jar
aspectjweaver.jar
为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将刚才下载好的jar添加进去
前置增强实例
编写TimeHandler.java
编写HelloWorld.java
编写配置文件
编写Application.java
运行输出
可以看到,打印Hello Aop.之前会先打印当前的时间CurrentTime = 1510132664923。
后置增强实例
修改TimeHandler.java
修改配置文件aop.xml
运行输出
环绕增强实例
修改TimeHandler.java
修改aop.xml
运行输出
之后就不用修改源程序,只需通过调整配置文件,就可以调整程序的逻辑。
修改配置文件
运行输出
aopalliance.jar
aspectjweaver.jar
为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将刚才下载好的jar添加进去
前置增强实例
编写TimeHandler.java
1 package com.example.spring; 2 3 public class TimeHandler { 4 public void beforTime() 5 { 6 System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis()); 7 } 8 }
编写HelloWorld.java
1 package com.example.spring; 2 3 public class HelloWorld { 4 public void printHello(){ 5 System.out.println("Hello Aop."); 6 } 7 }
编写配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 9 10 <!-- bean definition & AOP specific configuration --> 11 <!-- 1 配置对象--> 12 <bean id="helloWorld" class="com.example.spring.HelloWorld"/> 13 <bean id="timeHandler" class="com.example.spring.TimeHandler"/> 14 15 <!-- 2 配置aop操作--> 16 <aop:config> 17 <!-- 2.1 配置切入点--> 18 <!-- 设置切入点id为pointcut1 --> 19 <aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> 20 21 <!-- 2.2 配置切面--> 22 <aop:aspect ref="timeHandler"> 23 <!-- 配置前置增强类型 ,method:增强类()里面的方法(beforTime())作为前置--> 24 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 25 <aop:before method="beforTime" pointcut-ref="pointcut1"/> 26 </aop:aspect> 27 </aop:config> 28 </beans>
编写Application.java
1 package com.example.spring; 2 3 import org.springframework.context.support.AbstractApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class Application { 7 public static void main(String[] args) { 8 //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml 9 //使用AbstractApplicationContext容器 10 AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\aop.xml"); 11 //得到配置创建的对象 12 HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld"); 13 helloWorld.printHello(); 14 } 15 }
运行输出
前置增强:CurrentTime = 1510134673408 Hello Aop.
可以看到,打印Hello Aop.之前会先打印当前的时间CurrentTime = 1510132664923。
后置增强实例
修改TimeHandler.java
1 package com.example.spring; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 5 public class TimeHandler { 6 public void beforTime() 7 { 8 System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis()); 9 } 10 11 public void afterTime() 12 { 13 System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis()); 14 } 15 }
修改配置文件aop.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 9 10 <!-- bean definition & AOP specific configuration --> 11 <!-- 1 配置对象--> 12 <bean id="helloWorld" class="com.example.spring.HelloWorld"/> 13 <bean id="timeHandler" class="com.example.spring.TimeHandler"/> 14 15 <!-- 2 配置aop操作--> 16 <aop:config> 17 <!-- 2.1 配置切入点--> 18 <!-- 设置切入点id为pointcut1 --> 19 <aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> 20 21 <!-- 2.2 配置切面--> 22 <aop:aspect ref="timeHandler"> 23 <!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置--> 24 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 25 <!--<aop:before method="beforTime" pointcut-ref="pointcut1"/>--> 26 27 <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置--> 28 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 29 <aop:after method="afterTime" pointcut-ref="pointcut1"/> 30 </aop:aspect> 31 </aop:config> 32 </beans>
运行输出
Hello Aop. 后置增强:CurrentTime = 1510134850754
环绕增强实例
修改TimeHandler.java
1 package com.example.spring; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 5 public class TimeHandler { 6 public void beforTime() 7 { 8 System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis()); 9 } 10 11 public void afterTime() 12 { 13 System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis()); 14 } 15 16 public void aroundTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 17 //方法之前 18 System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis()); 19 20 //执行被增强的方法 21 proceedingJoinPoint.proceed(); 22 23 //方法之后 24 System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis()); 25 } 26 27 }
修改aop.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 9 10 <!-- bean definition & AOP specific configuration --> 11 <!-- 1 配置对象--> 12 <bean id="helloWorld" class="com.example.spring.HelloWorld"/> 13 <bean id="timeHandler" class="com.example.spring.TimeHandler"/> 14 15 <!-- 2 配置aop操作--> 16 <aop:config> 17 <!-- 2.1 配置切入点--> 18 <!-- 设置切入点id为pointcut1 --> 19 <aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> 20 21 <!-- 2.2 配置切面--> 22 <aop:aspect ref="timeHandler"> 23 <!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置通知--> 24 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 25 <!--<aop:before method="beforTime" pointcut-ref="pointcut1"/>--> 26 27 <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置通知--> 28 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 29 <!--<aop:after method="afterTime" pointcut-ref="pointcut1"/>--> 30 31 <!-- 配置环绕增强类型 method:增强类()里面的方法(aroundTime())作为环绕通知--> 32 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 33 <aop:around method="aroundTime" pointcut-ref="pointcut1"/> 34 </aop:aspect> 35 </aop:config> 36 </beans>
运行输出
环绕增强:CurrentTime = 1510135559066 Hello Aop. 环绕增强:CurrentTime = 1510135559074
之后就不用修改源程序,只需通过调整配置文件,就可以调整程序的逻辑。
修改配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 9 10 <!-- bean definition & AOP specific configuration --> 11 <!-- 1 配置对象--> 12 <bean id="helloWorld" class="com.example.spring.HelloWorld"/> 13 <bean id="timeHandler" class="com.example.spring.TimeHandler"/> 14 15 <!-- 2 配置aop操作--> 16 <aop:config> 17 <!-- 2.1 配置切入点--> 18 <!-- 设置切入点id为pointcut1 --> 19 <aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> 20 21 <!-- 2.2 配置切面--> 22 <aop:aspect ref="timeHandler"> 23 <!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置通知--> 24 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 25 <aop:before method="beforTime" pointcut-ref="pointcut1"/> 26 27 <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置通知--> 28 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 29 <aop:after method="afterTime" pointcut-ref="pointcut1"/> 30 31 <!-- 配置环绕增强类型 method:增强类()里面的方法(aroundTime())作为环绕通知--> 32 <!-- pointcut-ref设置为切入点的id:pointcut1 --> 33 <aop:around method="aroundTime" pointcut-ref="pointcut1"/> 34 </aop:aspect> 35 </aop:config> 36 </beans>
运行输出
前置增强:CurrentTime = 1510190036105 环绕增强:CurrentTime = 1510190036105 Hello Aop. 环绕增强:CurrentTime = 1510190036122 后置增强:CurrentTime = 1510190036122
相关文章推荐
- Java框架spring 学习笔记(十四):注解aop操作
- Java框架spring 学习笔记(十五):操作MySQL数据库
- Spring AOP框架学习笔记(2):AOP拦截器调用的实现
- spring学习笔记(7)AOP前夕[2]CGLib动态代理实例解析
- <Head First Java>学习笔记--第四章:方法操作实例变量 对象的行为
- Java框架spring 学习笔记(三):Bean 的生命周期
- Spring学习笔记IOC与AOP实例
- Java框架spring 学习笔记(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource注解)
- 1. 笔记JAVA框架学习——Spring入门环境搭建及helloworld
- Spring学习笔记(四) JavaConfig创建Bean与AOP编程相结合的Demo
- Spring学习笔记---AOP的简单实例
- Java框架spring 学习笔记(四):BeanPostProcessor接口
- Java框架学习笔记——初识Spring之简述
- Java框架spring 学习笔记(七):Spring相关概念
- Java框架spring 学习笔记(一):SpringBean、ApplicationContext 容器、BeanFactory容器
- [Spring学习笔记 4 ] AOP 概念原理以及java动态代理
- spring.net 学习笔记之 AOP (异常记录实例)转
- java学习笔记10 - 以@AspectJ方式在Spring中实现AOP
- Java代理学习-实现类似spring的可配置的AOP框架
- Java学习笔记-----Java操作数据库:可以运行的实例