Spring AOP的八个概念、五个通知类型、AOP的第一种实现方式

来源:互联网 发布:ual研究生 留学 知乎 编辑:程序博客网 时间:2024/06/08 18:52

CRUD:增删改查

平安校园的系统开发记录

设备管理、平安校园、会员注册三个模块

问题提出:如何统计统计模块的使用频率?

如何统计各个模块中的各个功能的使频率

问题提出方案:日志统计分析--- ---------------------人员ID IP  操作时间模块 功能

实现原理与实现过程:代码的侵入式、Filter:过滤器加入一个拦截器:Interceptor(类似过滤器)设置拦截规则,然后加日志AOP在OOP的基础上的建立的。

AOP是OOP的延续,是(Aspect Oriented Programming)的缩写,意思是面向切面编程。
1、主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理、权限登录、手机端的新闻客户端推荐新闻等等。
2、主要的意图是:将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立的裴指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。

3、相对于OOP的编程思想的优点:程序非侵入式\避免代码冗余量大

实现步骤:

<?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.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

<!-- 逻辑层实现类 -->
<bean id="aopService" class="com.javalxj.spring.aop.service.impl.AopServiceImpl">
<property name="aopDao" ref="aopDao"></property>
</bean>
<!-- 数据层实现类 -->
<bean id="aopDao" class="com.javalxj.spring.aop.dao.impl.AopDaoImpl">
</bean>
<!-- 日志通知实现类 -->
<bean id="loggerAdvice" class="com.javalxj.spring.aop.advice.LoggerAdvice">
</bean>
<!-- AOP代理 配置-->
<aop:config>
<aop:aspect ref="loggerAdvice">
<aop:pointcut expression="execution(*//*方法的返回类型   com.javalxj.spring.aop.service.impl.AopServiceImpl.*//任意方法(String,String))" id="method"/>
<aop:after pointcut-ref="method" method="addLog"/>
</aop:aspect>
<!--Caused by: java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor导入架包  -->
</aop:config>
</beans>

两个引用的XML文件配置:<aop:config>
<aop:aspect ref="loggerAdvice">

<aop:pointcut expression="execution(* com.javalxj.spring.*.service.impl.*ServiceImpl.*(String,String))" id="method"/>
<aop:after pointcut-ref="method" method="addLog"/>
</aop:aspect>
</aop:config>

参数不同

<aop:aspect ref="loggerAdvice">
<aop:pointcut expression="execution(* com.javalxj.spring.*.service.impl.*mpl.*(String,*))" id="method"/>
<aop:after pointcut-ref="method" method="addLog"/>
</aop:aspect>

中间路径层次不同可以用../任意的层级路径代替但是在项目中很少这样写,因为效率低

4、Spring AOP的八个概念:

  1. 连接点:在程序执行中特定的方法eg:需要绑定日志:create
  2. 切入点--站在应用程序角度思考:由连接点组成的集合:    eg:多个模块的所有的create方法
  3. 关注点--站在开发者角度思考:其实就是连接点:程序员所关心的共性的功能:方法执行的过程中是否能够绑定日志
  4. 切面:由多个切入点所构成的集合aop:aspect
  5. 目标对象:aopservice/iocservice:包含连接点的对象
  6. 通       知:关注点的实现
  7. AOP代理:声明动态的把通知所实现的功能回帖(create and addLog)到目标对象上
  8. 织        入:实现AOP代理所声明的功能
5、Spring AOP的五个通知类型:----决定何时触发通知


  1. after:后置通知----不论拦截的方法是否有异常
  2. before:前置通知
  3. around:环绕通知---需要放行操作
  4. after-returning:后置成功通知---------拦截的方法没油抛出异常就会执行--拦截到的方法必须正确返回;
  5. after-throwing:后置异常通知----拦截的方法如果抛出异常则会执行----有异常执行它没油异常不执行
AOP的提出者: jboss公司:around

多数监控异常通知----同时绑定邮箱


around:实现其他四种功能

<aop:config>
<aop:aspect ref="loggerAdvice">
<aop:pointcut expression="execution(* com.javalxj.spring.*.service..*mpl.*(String,*))" id="method"/>
<aop:around pointcut-ref="method" method="around"/>
</aop:aspect>
<!--Caused by: java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor导入架包  -->
</aop:config>

public class LoggerAdvice {


public void around(ProceedingJoinPoint pjp){
System.out.println("around前置通知");
try{
System.out.println("around后置成功通知");
}
catch(Throwable e){
System.out.println("around后置异常通知");
}finally{
System.out.println("around后置通知");
}
}
public void addLog(){
System.out.println("日志实现拦截到该方法日志实现");
}
}



原创粉丝点击