spring之AOP基本概念和AOP的三种实现方式

来源:互联网 发布:nginx集群负载均衡配置 编辑:程序博客网 时间:2024/06/05 07:40

AOP基本概念


 关于AOP的一些概念性词语的解释:

切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。在Spring AOP中,切面可以使用基于模式)或者基于Aspect注解方式来实现。通俗点说就是我们加入的切面类(比如log类),可以这么理解。

连接点(Joinpoint:在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。在Spring AOP中,一个连接点总是表示一个方法的执行。通俗的说就是加入切点的那个点

通知(Advice:在切面的某个特定的连接点上执行的动作。其中包括了“around”“before”“after”等不同类型的通知(通知的类型将在后面部分进行讨论)。许多AOP框架(包括Spring)都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。

切入点(Pointcut:匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例如,当执行某个特定名称的方法时)。切入点表达式如何和连接点匹配是AOP的核心:Spring缺省使用AspectJ切入点语法。

引入(Introduction:用来给一个类型声明额外的方法或属性(也被称为连接类型声明(inter-type declaration))。Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。例如,你可以使用引入来使一个bean实现IsModified接口,以便简化缓存机制。

目标对象(Target Object: 被一个或者多个切面所通知的对象。也被称做被通知(advised)对象。 既然Spring AOP是通过运行时代理实现的,这个对象永远是一个被代理(proxied)对象。

AOP代理(AOP ProxyAOP框架创建的对象,用来实现切面契约(例如通知方法执行等等)。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。

织入(Weaving:把切面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。

AOP的三种实现方式

<!-- 1、配置形式:最容易理解2、注解形式:代码量最少3、正则形式:功能最强 -->

例子:


一、AOP注解实现

@Aspect
public class LoggerAdvice {
@Pointcut("execution(* com.javalxj.spring.*.service..*mpl.*(String,*))")
public void method(){

}
@After("method()")
public void after(){
System.out.println("AOP:After");

}---

---------改进------

public class LoggerAdvice {
private static final String EXPRESSION="execution(* com.javalxj.spring.*.service..*mpl.*(String,*))";
@After(EXPRESSION)
public void after(){
System.out.println("AOP:After");
}

------注解实现五种通知方式----

public class LoggerAdvice {
private static final String EXPRESSION="execution(* com.javalxj.spring.*.service..*mpl.*(String,*))";
@After(EXPRESSION)
public void after(){
System.out.println("AOP:After");
}
@Before(EXPRESSION)
public void before(){
System.out.println("AOP:Before");
}
@AfterReturning(EXPRESSION)
public void afterReturning(){
System.out.println("AOP:afterReturning");
}
@AfterThrowing(EXPRESSION)
public void afterThrowing(){
System.out.println("AOP:afterThrowing");
}
/**
* around总是最先执行
* 配置文件报错找错轻松注解方式的文件找错不容易
* @param pjp

*/
@Around(EXPRESSION)
public void around(ProceedingJoinPoint pjp){
System.out.println("AOP:around");
try {
pjp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

--------------------------------------------带参数的注解-----------------------------------

@Pointcut("execution(* com.javalxj.spring.*.service..*mpl.*(String,*))")
public void method(){

}
/**带参数的拦截:注释参数名称必须与方法中的参数名称相同
* 问题提出:参数多个 ,并且类型不同怎么办
* 提示:参数个数相同的时候可以用OBject 参数类,然后进方法内转化执行判断强转:
*/
@After("method() and args(name,age)")
public void after(String name,int age){

System.out.println("AOP:After----"+name);
}

----------------------------------配置形式-------------------------

<?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-2.5.xsd            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>    <bean id="registerDaoImpl" class="com.zxf.dao.RegisterDaoImpl"/>    <bean id="registerService" class="com.zxf.service.RegisterServiceImpl">        <property name=" registerDaoImpl " ref=" RegisterDaoImpl "/>    </bean>    <!-- 日志切面类 -->    <bean id="logAspectBean" class="com.zxf.aspect.LogAspect"/>    <!-- 第1步: AOP的配置 -->    <aop:config>        <!-- 第2步:配置一个切面 -->        <aop:aspect id="logAspect" ref="logAspectBean">            <!-- 第3步:定义切入点,指定切入点表达式 -->            <aop:pointcut id="allMethod"                 expression="execution(* com.zxf.service.*.*(..))"/>             <!-- 第4步:应用前置通知 -->            <aop:before method="before" pointcut-ref="allMethod" />            <!-- 第4步:应用后置通知 -->            <aop:after-returning method="afterReturn" pointcut-ref="allMethod"/>            <!-- 第4步:应用最终通知 -->            <aop:after method="after" pointcut-ref="allMethod"/>            <!-- 第4步:应用抛出异常后通知 -->            <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>            <!-- 第4步:应用环绕通知 -->            <!--             <aop:around method="doAround" pointcut-ref="allMethod" />             -->        </aop:aspect>    </aop:config></beans>


---------------------------------------正则形式-----------------------------

<?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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"><!-- 1、配置形式:最容易理解2、注解形式:代码量最少3、正则形式:功能最强 --><!-- 逻辑层实现类 --><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.LogAdvice"></bean><!-- 基于正则的配置表达式 1、定义一个切入点注入.(所有类中的)*create//以create结尾的方法==为了拦截符合要求的方法--><bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"><property name="pattern" value=".*create"></property></bean><!-- 基于正则的配置表达式 2、定义一个通知管理器--><bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"><property name="advice" ref="loggerAdvice"></property><property name="pointcut" ref="pointcut"></property></bean><!-- 基于正则的配置表达式 3、定义代理实现对象target目标对象               有拦截对象的interceptorNames     拦截器proxyInterfaces      接口--><bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="aopService"/> <property name="interceptorNames" value="advisor" /><property name="proxyInterfaces"  value="com.javalxj.spring.aop.service.AopService"></property></bean><!-- AOP代理 配置<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></aop:config>--><!--启用AOP注解支持<aop:aspectj-autoproxy></aop:aspectj-autoproxy>--></beans>


原创粉丝点击