Spring学习(2)一个简单的AOP实例

来源:互联网 发布:防沉迷解除软件2016 编辑:程序博客网 时间:2024/05/19 05:03

AOP的概念

AOP 全称AspectOriented Programming 即面向切面编程或者面向方面编程

其主要作用是,在不修改源代码的情况下给某个或者一组操作添加额外的功能。像日志记录,事务处理,权限控制等功能,都可以用AOP来“优雅”地实现,使这些额外功能和真正的业务逻辑分离开来,软件的结构将更加清晰。AOP是OOP的一个强有力的补充。

Spring AOP是基于代理的,是运行时绑定的。合理的运用AOP,将使软件的开发更加便捷,清晰。


AOP基本术语

  • Join Point(连接点): 所谓的连接点就是被拦截到的点,spring中,这些点指的就是方法(通俗来讲就是起作用的那个方法)。spring中只支持方法类型的连接点,事实上join point还可以是field或类构造器。

注:如果一个类中有2个方法,那么这两个方法都是连接点。

  • Pointcut(切入点):用来指定join point(通俗来讲就是描述(定义)的一组符合某个条件的join point)。通常使用pointcut表达式来限定joint point,

Spring默认使用AspectJ pointcutexpression language。Pointcut通过pointcutexpression来描述,有若干种限定词。由于Pointcut的定义在Spring文档7.2.3 Declaringa pointcut中写得比较详细,所以在此不再赘述。

eg: execution(* com.tech.service.impl..*.*(..)) 含义:执行([任何]返回类型包名[..代表所有子包][*.*所有类的所有方法](..[任何参数]))

  • Advice(通知):是指拦截到join point在特定的时刻执行的操作,Advice有以下几种不同类型:(通俗地来讲就是起作用的内容和时间点)

1.       Before advice: 执行在join point之前的advice,但是它不能阻止joint point的执行流程,除非抛出了一个异常(exception)。

2.       After returning advice: 执行在join point这个方法返回之后的advice。

3.       After throwing advice: 执行在join point抛出异常之后的advice。

4.       After(finally) advice: 执行在join point返回之后或者抛出异常之后的advice,通常用来释放所使用的资源。

5.       Around advice: 执行在join point这个方法执行之前与之后的advice。

  • Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期动态的给对象增加方法或者属性。
  • Target object(目标对象): Advice起作用的那个对象,代理的对象。
  • AOP proxy(AOP代理):为实现AOP所生成的代理。在Spring中有两种方式生成代理:JDK代理和CGLIB代理。
  • Aspect: 组合了Pointcut与Advice,在Spring中有时候也称为Advisor。某些资料说Advisor是一种特殊的Aspect,其区别是Advisor只能包含一对pointcut和advice,但是aspect可以包含多对。AOP中的aspect可以类比于OOP中的class。
Weaving:将Advice织入join point的这个过程。 


一个简单的AOP实例

项目添加Spring所需的jar包

UserDao.java

package com.yw.xu.dao;public class UserDao {void add(){System.out.println("added!");}void register(){System.out.println("reg");}}

UserBeforeAdvice.java

package com.yw.xu.dao;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class UserBeforeAdvice  implements MethodBeforeAdvice{@Overridepublic void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {System.out.println( " 这是BeforeAdvice类的before方法. " );}}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- 具体逻辑对象 --><bean name="userDao" class="com.yw.xu.dao.UserDao"></bean><!-- 增强对象 --><bean name="userBeforeAdvice" class="com.yw.xu.dao.UserBeforeAdvice"></bean><!-- 配置代理对象 --><bean name="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="userDao"></property><property name="interceptorNames"><list><value>userBeforeAdvice</value></list></property><property name="optimize" value="true"></property></bean></beans>

Test.java

package com.yw.xu.dao;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");UserDao dao = (UserDao) ctx.getBean("proxyFactoryBean");dao.add();}}

测试结果:

log4j:WARN No appenders could be foundfor logger(org.springframework.context.support.ClassPathXmlApplicationContext).

log4j:WARN Please initialize the log4jsystem properly.

 这是BeforeAdvice类的before方法.

added!




0 0
原创粉丝点击