当前位置: 移动技术网 > IT编程>开发语言>Java > SpringAOP(5)

SpringAOP(5)

2019年03月09日  | 移动技术网IT编程  | 我要评论

2019-03-08/14:22:58

演示:登陆核心业务类与日志周边功能实现aop面向切面思想

jar包:https://share.weiyun.com/5gofoup

学习资料:

1.aop

  spring(1)中有讲过什么是aop,这里就放一个原理图看一下

  1. 功能分两大类,辅助功能和核心业务功能
  2. 辅助功能和核心业务功能彼此独立进行开发
  3. 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行
  4. 如果有需要,就把"日志输出" 功能和 "登陆" 功能 编织在一起,这样登陆的时候,就可以看到日志输出了
  5. 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程

 

2.准备业务类productservice

 1 package service;
 2  
 3 public class productservice {
 4      
 5     public void dosomeservice(){
 6          
 7         system.out.println("dosomeservice");
 8          
 9     }
10      
11 }
productservice.java

 

3.引入切面之前调用业务类

 1 package test;
 2  
 3 import org.springframework.context.applicationcontext;
 4 import org.springframework.context.support.classpathxmlapplicationcontext;
 5  
 6 import com.how2java.service.productservice;
 7  
 8 public class testspring {
 9  
10     public static void main(string[] args) {
11         applicationcontext context = new classpathxmlapplicationcontext(new string[] { "applicationcontext.xml" });
12         productservice s = (productservice) context.getbean("s");
13         s.dosomeservice();
14     }
15 }
testspring.java

 运行之前需要在配置文件中加一个扫描

 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     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemalocation="
 8    http://www.springframework.org/schema/beans
 9    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10    http://www.springframework.org/schema/aop
11    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12    http://www.springframework.org/schema/tx
13    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14    http://www.springframework.org/schema/context     
15    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16   
17     <bean name="s" class="service.productservice">   //声明业务对象
18     </bean>   

运行结果

 

4.准备日志切面 loggeraspect

该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。

下面是切面要执行的函数 log,log函数有一个形参 joinpoint 这个可以理解为断点

第8行代码可以理解为就是将来与某个核心功能编织之后,用于执行核心功能的代码

getsignature 获取切点的签名

proceed() 执行切点本来的业务

 1 package aspect;
 2 import org.aspectj.lang.proceedingjoinpoint;
 3  
 4 public class loggeraspect {
 5  
 6     public object log(proceedingjoinpoint joinpoint) throws throwable {
 7         system.out.println("start log:" + joinpoint.getsignature().getname());    //开始
 8         object object = joinpoint.proceed();   //监视 
 9         system.out.println("end log:" + joinpoint.getsignature().getname());  //执行切面,结束
10         return object;
11     }
12 }

 

5.aop配置文件详解

17-20行:详情见注释

 

23-25行:   指定右边的核心业务功能    看原理图的实现

这一句是声明切入点,切入点的 id 叫 loggercutpoint ,用来标记这个切入点,这个expression表示:满足expression中的方法调用之后,就会去进行切面操作,类似于触发了切面

第一个 * 代表返回任意类型       service.productservice.*     表示包名以 service.productservice 开头的类的任意方法(第二个*表示任意方法,通配符的意思)(..) 表示方法的参数是任意数量和类型

简单说就是,只要service这个包中的productservice类的任意一个函数被调用,不管返回值是什么,都会触发开关,我就会去执行切面,也就是辅助功能,但是辅助功能是什么呢,就是下面两句:

 

27-29行:   指定左边的辅助功能     看原理图的实现

这两句是定义了一个切面,上面说只要触发开关,就会去执行切面,就是指的这里的切面,所谓切面,就是一个类中的方法...

id代表这个切面的名字,ref就是指的方法所在的类,method代表的就是方法的名字,

pointcut-ref="loggercutpoint" 这个就是表示这个切面是和上面的切点关联起来的(一个切点是可以关联多个切面的,一个切面只能关联一个方法),只要上面的切点被触发,就会到这里来执行一些辅助功能

after表示在切入点触发之后来执行我这个中断,当然也有before,一共有五个before,after,after-returning ,after-throwing,around。具体的aop术语单独用一篇博客细讲一下

在 method 参数后面,还可以加上参数列表。

<aop:config/>:把业务对象与辅助功能编织在一起。
 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     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemalocation="
 8    http://www.springframework.org/schema/beans
 9    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10    http://www.springframework.org/schema/aop
11    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12    http://www.springframework.org/schema/tx
13    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14    http://www.springframework.org/schema/context     
15    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16    
17     <bean name="s" class="service.productservice">
18     </bean>    //声明业务类
19      
20     <bean id="loggeraspect" class="aspect.loggeraspect"/>   //声明日志切面
21      
22     <aop:config>
23         <aop:pointcut id="loggercutpoint"
24             expression=
25             "execution(* service.productservice.*(..)) "/>
26              
27         <aop:aspect id="logaspect" ref="loggeraspect">
28             <aop:around pointcut-ref="loggercutpoint" method="log"/>
29         </aop:aspect>
30     </aop:config>    
31   
32 </beans>

 

6.测试代码

 1 package test;
 2   
 3 import org.springframework.context.applicationcontext;
 4 import org.springframework.context.support.classpathxmlapplicationcontext;
 5  
 6 import service.productservice;
 7   
 8 public class testspring {
 9   
10     public static void main(string[] args) {
11         applicationcontext context = new classpathxmlapplicationcontext(
12                 new string[] { "applicationcontext.xml" });
13         productservice s = (productservice) context.getbean("s");
14         s.dosomeservice();
15     }
16 }
testspring.java
 
testspring 代码没有发生任何变化,通过配置的方式,把切面和核心业务类编制在了一起。
运行测试,可以发现在编织之后,业务方法运行之前和之后分别会打印日志

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网