Java框架spring 學習筆記(十二):aop實例操做

使用aop須要在網上下載兩個jar包:java

  1. aopalliance.jar
  2. aspectjweaver.jar

爲idea添加jar包,快捷鍵ctrl+shift+alt+s,打開添加jar包的對話框,將剛纔下載好的jar添加進去spring

 

前置加強實例express

編寫TimeHandler.javaide

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

1 package com.example.spring;
2 
3 public class HelloWorld {
4     public void printHello(){
5         System.out.println("Hello Aop.");
6     }
7 }

 編寫配置文件spa

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

 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 }

運行輸出xml

前置加強:CurrentTime = 1510134673408
Hello Aop.

能夠看到,打印Hello Aop.以前會先打印當前的時間CurrentTime = 1510132664923。對象

 

後置加強實例blog

修改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
相關文章
相關標籤/搜索