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 ...
随机推荐
- vue响应原理
用Object.defineProperty添加属性的方法,给属性加get set方法.当我们操作属性的时候其实底层是在操作dom. <!DOCTYPE html> <html la ...
- [转载]Yacc基础
原文:https://www.ibm.com/developerworks/cn/linux/sdk/lex/index.html,摘录部分内容. Yacc的定义 Yacc 代表 Yet Anothe ...
- centos7---ansible批量部署
CentOS7系统 ansible自动化部署多台服务器部署 Ansible工作机制 从图中可以看出ansible分为以下几个部份: 1> Control Node:控制机器2> In ...
- vue学习(3)-增删改查
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PostgreSQL 初学常用实用命令
常用命令 psql -h ip -p port -U user -d dbName 数据库 \dt(当前数据库所有表) \l(所有数据库) \c(切换数据库) \d 表名(查看表结构) table \ ...
- vue-cli3.x创建项目vue create hello-world
在git中输入指令vue create hello-world,没反应,因为vue-cli的版本问题,必须3.x版本才能使用这个指令于是按照官网的提示升级vue vue-cli从2.x版本升级到3.x ...
- 单点登录系统SSO实现
前些天被问到单点登录了,而据我当时做的这个模块两年了,现在重新温习并记录下,方便以后快速回忆起来 一.什么是单点登录系统 SSO全称Single Sign On.SSO是用户只需要登录一次就可以访问所 ...
- 前端入门Js笔记
T 001 ____________--信息页面展示 需求分析: 有一个页面,在页面上有很多文字信息,且格式不一. 技术分析: html: 文字标签: 字体标签: 标题标签: 其他标签: 排版标签: ...
- GridView 二维排布
与ListView一维排布相对 public class MainActivity extends Activity implements AdapterView.OnItemClickListene ...
- 在values中添加colors.xml
如何在values中添加colors.xml文件?按钮上的文字,背景色的动态变化的xml放在res/drawable里,现在我就说静态的xml文件吧. res/values/colors.xml< ...