AOP是什么
●AOP: (Aspect Oriented Programming) 面向切面编程。是目前软件开发中的一个热点,也是spring框架中容。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。
实例:
service包中有SellService和SaveService两个类(业务类)
public class SaveService {
public void stockit(){
System.out.println("这是进货的业务处理");
}
}
public class SellService {
public void sellit(){
System.out.println("商品销售");
}
}
common包中是两个通用方法LogCommon和RoleCommon(切面类):
public class LogCommon {
public void afterexec(){
System.out.println("执行完毕之后的日志记录");
}
}
public class RoleCommon {
public void beforeexec(){
System.out.println("方法执行前的权限检查");
}
}
applicationContext.xml配置:
<?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/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- 将bean对象添加到容器里 -->
<!-- 配置业务类的bean -->
<bean id="saveService" class="com.jljy.aop.service.SaveService"></bean>
<bean id="sellService" class="com.jljy.aop.service.SellService"></bean>
<!-- 配置切面类 -->
<bean id="logCommon" class="com.jljy.aop.common.LogCommon"></bean>
<bean id="roleCommon" class="com.jljy.aop.common.RoleCommon"></bean>
<!-- 对于AOP的配置 -->
<aop:config>
<!-- 配置权限的切面 -->
<aop:aspect id="roleaspect" ref="roleCommon">
<!-- 配置连接点的集合 -->
<!-- 注意:当你的鼠标放到expression上时,会出现一个框,上面会显示这样的东西 Attribute : expression The pointcut expression. For example : 'execution(* com.xyz.myapp.service.*.*(..))' Data Type : string 把红色加粗的文字copy下来放到expression里,就可以很轻松的指定你想要切入事物的类了 -->
<aop:pointcut expression="execution(* com..*.*Service.*(..))" id="mypointcut"/>
<!-- 业务方法执行之前的配置增强 -->
<aop:before method="beforeexec" pointcut-ref="mypointcut"/>
</aop:aspect>
<!-- 配置一个日志记录的切面 -->
<aop:aspect id="logaspect" ref="logCommon">
<aop:pointcut expression="execution(* com..*.*Service.*(..))" id="mypointcut2"/>
<!-- 业务方法执行之后的配置增强 -->
<aop:after method="afterexec" pointcut-ref="mypointcut2"/>
</aop:aspect>
</aop:config>
</beans>
补充切入点表达式:
任意公共方法的执行:
execution(public * *(..))
任何一个名字以“set”开始的方法的执行:
execution(* set*(..))
AccountService接口定义的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
在service包中定义的任意方法的执行:
execution(* com.xyz.service..(..))
在service包或其子包中定义的任意方法的执行:
execution(* com.xyz.service...(..))
aop切入点表达式
1、切入点表达式:对指定的方法进行拦截,并且生成代理表达式。
2、拦截所有public方法
<aop:pointcut expression="execution(public * *(..))" id="pt"/>
3、拦截所有save开头的方法
<aop:pointcut expression="execution(* save*(..))" id="pt"/>
4、拦截指定类的指定方法
<aop:pointcut expression="execution(public * 包名.类名.方法名(..))" id="pt"/>
5、拦截指定类的所有方法
<aop:pointcut expression="execution(* 包名.类名.*(..))" id="pt"/>
6、拦截指定包,以及其自包下所有类的所有方法
<aop:pointcut expression="execution(* cn..*.*(..))" id="pt"/>
7、多个表达式
<aop:pointcut expression="execution(* 包名.类名.方法名()) || execution(* 包名.类名(不同的类).方法名())" id="pt"/>
<aop:pointcut expression="execution(* 包名.类名.方法名()) or execution(* 包名.类名(不同的类).方法名())" id="pt"/>
8、取非值
<aop:pointcut expression="!execution(* 包名.类名.方法名())" id="pt"/>
<aop:pointcut expression=" not execution(* 包名.类名.方法名())" id="pt"/>