美文网首页
Spring接口代理实现

Spring接口代理实现

作者: 神豪VS勇士赢 | 来源:发表于2018-08-03 20:15 被阅读76次

AOP联盟通知类型
AOP联盟为通知Advice定义了org.aopalliance.aop.Advice

开发通知类:
Spring按照通知Advice在目标类方法的连接点位置,常用:

前置通知 org.springframework.aop.MethodBeforeAdvice:在目标方法执行前实施增强,可以拦截目标方法的执行
后置通知 org.springframework.aop.AfterReturningAdvice:在目标方法执行后实施增强 ,可以获取到方法的返回值
环绕通知 org.aopalliance.intercept.MethodInterceptor:在目标方法(切入点)执行前后实施增强

invocation.proceed();//必须手动执行目标方法

使用AOP联盟来编写通知类
通知概念:
在目标方法之前加入通知:log: 这个通知就叫做前置通知
在目标方法之后加入通知:log: 这个通知就叫做后置通知
环绕通知:包含(前置+后置)

开发流程:
第一步:创建通知类
我们在这里创建了很多通知类 前置 后置 以及环绕

前置 :
@Component("beforeLog")
public class LogBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
Log.info("我是打印输出通知 前置方法");
}
}
后置:
@Component("afterLog")
public class LogAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
Log.info("我是打印输出通知 后置方法");
}
}
环绕:
@Component("circleLog")
public class LogCircleAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Log.info("环绕前");
Object proceed = invocation.proceed();
Log.info("环绕后" );
return proceed;
}
}
带返回值的环绕:
@Component("returnInfoCircleLog")
public class ReturnInfoCircleAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Log.info("环绕前");
String s = (String) invocation.proceed();
Log.info("环绕后" + s);
return s;
}
}

application 配置文件如下所示:

<bean id="studentServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="studentService"></property>

<property name="interceptorNames">
<list>
<value>circleLog</value>
</list>
</property>

<property name="proxyInterfaces">
<array>
<value>com.zyh.service.StudentService</value>
</array>
</property>
</bean>

测试方法如下所示:
@Test
public void testStudentService(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) classPathXmlApplicationContext.getBean("studentServiceProxy");
studentService.addStudent();
}

结果如下所示:

image.png

当我们想要测试带返回值得环绕的时候 我们修改代码如下所示:

配置文件如下所示:
<bean id="returnInfoServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="studentService"></property>

<property name="interceptorNames">
<list>
<value>returnInfoCircleLog</value>
</list>
</property>

<property name="proxyInterfaces">
<array>
<value>com.zyh.service.StudentService</value>
</array>
</property>

</bean>

修改实现类如下所示:


image.png

测试方法如下所示 :
@Test
public void testStudentService(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

StudentService studentService1 = (StudentService) classPathXmlApplicationContext.getBean("returnInfoServiceProxy");
studentService1.printInfo();

}

输出结果如下所示:


image.png

ProxyFactoryBean
这个代理类返回的对象是谁?
是通过底层getObject()返回具体被代理的目标对象。不是自己工厂的对象。

在代理类,AOP编程有3小步:
1)整合目标类
2)整合通知类
3)关联接口
三大步三小步

相关文章

  • Spring Aop

    Spring AOP: 它基于动态代理来实现。默认地,如果使用接口的,用 JDK 提供的动态代理实现,如果没有接口...

  • Spring AOP(一)

    Spring AOP实现原理 动态代理: 利用核心类Proxy和接口InvocationHandler(基于代理模...

  • Spring之AOP之底层实现

    Spring之AOP之底层实现 静态代理实现 定义IStudnetService接口 定义StudentServi...

  • Spring接口代理实现

    AOP联盟通知类型AOP联盟为通知Advice定义了org.aopalliance.aop.Advice 开发通知...

  • 动态代理

    1.spring aopjdk动态代理:invocationHandler 和proxy,需要实现接口cglib动...

  • Spring的两种代理方式

    AOP是Spring的重要组成部分,而AOP正是通过代理实现的。如果代理对象实现了接口,则默认使用jdk动态代理,...

  • SpringAOP源码解析过程

    spring的AOP基于JDK的动态代理和cglib实现,默认代理对象是某个接口的实现就会使用JDK动态代理,否则...

  • JDK动态代理使Spring事务失效

    1.JDK 动态代理 简单的动态代理: 接口 接口实现类 代理类 测试 2.Spring事务 事务有四个特性:AC...

  • 代理模式

    代理模式分静态代理与动态代理,而动态代理又在Spring中与两个实现:1.基于JDK的动态代理(通过接口实现)2....

  • Java Spring中的动态代理cglib

    总结 JDK的动态代理和 Spring中的动态代理cglib区别 JDK 的动态代理 :针对实现了接口的类产生代理...

网友评论

      本文标题:Spring接口代理实现

      本文链接:https://www.haomeiwen.com/subject/vrdyvftx.html