Spring——5种增强方式
一、前置增强
二、后置增强
三、环绕增强
环绕增强相当于前置增强和后置增强的结合体,可以使用<aop:around>进行处理,这里我采用代理工厂的方式
1.接口及其实现类
public interface ProService {
public void doSome();
}
public class ProServiceImpl implements ProService {
@Override
public void doSome() {
System.out.println("123");
}
}
2.增强类
public class around implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("before");
Object proceed = methodInvocation.proceed();
System.out.println("after");
return proceed;
}
}
3.配置文件
<!--注入bean-->
<bean id="proService" class="cn.spring.around.ProServiceImpl"></bean>
<!--切面-->
<bean id="aroundAdvice" class="cn.spring.around.around"></bean>
<!--代理工厂实现增强-->
<bean id="ProFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="proService"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="aroundAdvice"></property>
<!--更换代理方式 默认值为false jdk动态代理,当没有接口时,自动改成cglib-->
<property name="proxyTargetClass" value="true"></property>
</bean>
或者使用aop:config
<!--将目标对象声明到Spring容器中-->
<bean id="doSomeService" class="com.cmy.service.impl.DoSomeServiceImpl"></bean>
<!-- 声明增强方法所在的Bean -->
<bean id="advice" class="com.cmy.around.AroundLogger"></bean>
<!-- 配置切面 -->
<aop:config>
<aop:aspect ref="advice">
<aop:around method="aroundLogger" pointcut="execution(* com.cmy.*.*.*(..))"/>
</aop:aspect>
</aop:config>
四、异常增强
异常增强处理,在目标方法抛出异常后织入;使用<aop:after-throwing>处理,这里我依旧采用代理工厂的方法
1.接口及其实现类
public interface IdoSomeService {
public void doSome() throws Exception;
}
/**
* 原始对象
*/
public class IdoSomeServiceImpl implements IdoSomeService {
public void doSome() throws Exception{
int result=5/0;
System.out.println("=========真实业务===========");
}
}
2.增强类
package cn.spring.throwadvice; import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.ThrowsAdvice; import java.lang.reflect.Method; public class MyAdvice {
public void afterThrowing(Exception ex){
System.out.println("=====发生了异常,执行增强操作===============");
} }
3.配置文件
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 将增强和业务织入到一起-->
<property name="target" ref="idoSomeService"></property>
<!--拦截增强类;-->
<property name="interceptorNames" value="myThrowAdvice"></property>
<!-- 更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib -->
<property name="proxyTargetClass" value="true"></property>
</bean>
或者采用aop:after-throwing
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
<aop:aspect ref="myAdvice">
<aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing> </aop:config>
五、最终增强
无论方法是否抛出异常,都会在目标方法后做织入的增强处理,即该增强一定会执行,有点类似try-catch-finally块中的finally,一般用于释放资源。 使用<aop:after>处理最终增强。这里依旧运用代理工厂实现
1.增强类
public void afterAdvice(){
System.out.println("======执行最终异常===============");
}
2.配置文件
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
<aop:aspect ref="myAdvice">
<aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
</aop:config>
Spring——5种增强方式的更多相关文章
- Spring常见的两种增强方式
一.编程式增强 不借助spring的配置,通过自己实例化对象来实现的增强方式 创建增强类,需要实现你需要的增强接口,(只有实现了该接口,这个类就是一个通知)) /** * 增强类 */ public ...
- 深刻剖析spring三种注入方式以及使用注解的原理
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- Spring 3种注入方式
spring的三种注入方式: 接口注入(不推荐) getter,setter方式注入(比较常用) 构造器注入(死的应用) 关于getter和setter方式的注入: autowire="de ...
- spring boot-- 三种启动方式
spring-boot的三种启动方式 1. 直接运行SpringbootApplication.java 2.在项目目录下运行mvn spring-boot:run 3.先编译项目mvn instal ...
- spring 2种下载方式 下载地址 download 地址
spring 在官网只提供 maven 的下载方式,把zip方式的不再提供,两种方法下载: 1.想找回以前版本的spring zip包,如果知道版本号,那么直接在google里输入 ” spring ...
- 一起学Spring之三种注入方式及集合类型注入
本文主要讲解Spring开发中三种不同的注入方式,以及集合数据类型的注入,仅供学习分享使用,如有不足之处,还请指正. 概述 Spring的注入方式一共有三种,如下所示: 通过set属性进行注入,即通过 ...
- Spring -- 三种配置方式
1.Explicit configuration in XML:显示的XML配置. 优点: 1)XML配置方式进一步降低了耦合,使得应用更加容易扩展,即使对配置文件进一步修改也不需要工程进行修改和重新 ...
- Spring两种注入方式
1.XML注入 2.标签注入
- spring三种实例化bean的方式
1构造函数实例化 2静态工厂方法实例化 3实例工厂方法实例化 service接口: package service; public interface PersonService { public v ...
随机推荐
- Spring实战(十三)Spring事务
1.什么是事务(Transaction)? 事务是指逻辑上的一组操作,要么全部成功,要么全部失败. 事务是指将一系列数据操作捆绑成为一个整体进行统一管理.如果某一事务执行成功,则该事务中进行的所有数据 ...
- STL-set 容器以及迭代器的简单理解
先说下set的基本操作和时间复杂度 begin() ,返回set容器的第一个元素 end() ,返回set容器的最后一个元素 clear() ,删除set容器中的所有的元素 em ...
- 清除SQL日志文件
1.清除errorlog文件 MSSQL在 C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG 目录下存放这一些日志文件,一共是7个,常常会 ...
- React+Redux+Dva学习
Redux提供一些api来管理数据,并且只能通过它提供的方式来修改.Redux包括三个部分:store, action,reducer. store:是一个规范的state,就像一个有条理的数据库,R ...
- SpringBoot-核心依赖说明
spring-boot-dependencies 一般用来放在父项目中,来声明依赖,子项目引入相关依赖而不需要指定版本号,好处就是解决依赖冲突,统一管理依赖版本号 利用pom的继承,一处声明,处处使用 ...
- Delphi 画刷与作图区域
樊伟胜
- 原创:(一)TCP/IP学习笔记之概述
端到端论点和命运共享其实不应该在底层,差错控制应该在应用程序附近来实现.这是因为考虑了连接,而不是传输的准确,因为差错可以根据某些算法(通信中的滤波等)来恢复,不过在大面积网络出现问题的时候有必要进行 ...
- AlertDialog 对话框 5种
MainActivity.class public class MainActivity extends AppCompatActivity implements View.OnClickListen ...
- 5.Nginx的session一致性(共享)问题配置方案1
1:Session共享 为什么要实现共享,如果你的网站是存放在一个机器上,那么会话数据就在这台机器,但是如果你使用了负载均衡把请求分发到不同的机器呢?这个时候会话 id在客户端是没有问题的,但是如果用 ...
- angular打印功能实现方式
目前主流的前端打印方式有两种:一种是使用浏览器打印功能直接打印页面,另一种是调用本地控件实现.浏览器打印功能单一,不适用于复杂的业务表单,而打印控件可以设计打印模板,实现复杂表单的打印,十分适合复杂的 ...