多种方式实现AOP
一、使用代理工厂完成声明式增强
1.创建业务接口
public interface IdoSomeService { public void doSomething(); }
2.创建接口实现类
public class IdoSomeServiceImpl implements IdoSomeService{ @Override public void doSomething() { System.out.println("真实业务"); } }
3.创建切面类
/** * 切面 */ public class MyBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("前置增强"); } }
4.编写applicationContext.xml配置文件
<!--注入业务Bean--> <bean id="idoSomeService" class="cn.spring.proxyfactory.IdoSomeServiceImpl"></bean> <!--增强:切面--> <bean id="myBeforeAdvice" class="cn.spring.proxyfactory.MyBeforeAdvice"></bean> <!--使用代理工厂实现增强--> <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--将增强和业务织入 到一起 --> <property name="target" ref="idoSomeService"></property> <!--拦截增强类--> <property name="interceptorNames" value="myBeforeAdvice"></property> </bean>
5.创建测试类
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取代理工厂 IdoSomeService idoSomeService=(IdoSomeService)context.getBean("proxyFactory"); idoSomeService.doSomething(); } }
二、使用代理工厂完成环绕增强
1.创建业务接口
public interface IdoSomeService { public void doSomething(); }
2.创建业务接口实现类
public class IdoSomeServiceImpl implements IdoSomeService{ @Override public void doSomething() { System.out.println("真实业务"); } }
3.创建切面类
public class MyAroundAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("环绕前"); //调用核心业务方法也可以获取方法内的参数 也可以获取目标对象 Object proceed = methodInvocation.proceed(); Object aThis = methodInvocation.getThis(); System.out.println(aThis); System.out.println("环绕后"); return proceed; } }
4.编写applicationContext.xml配置文件
环绕增强 <bean id="idoSomeService" class="cn.spring.around.IdoSomeServiceImpl"></bean> <bean id="myAroundAdvice" class="cn.spring.around.MyAroundAdvice"></bean> <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--将增强和业务织入到一起--> <property name="target" ref="idoSomeService"></property> <property name="interceptorNames" value="myAroundAdvice"></property> <!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib--> <property name="proxyTargetClass" value="true"></property> </bean>
4.创建测试类
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取代理工厂 IdoSomeService idoSomeService=(IdoSomeService)context.getBean("proxyFactory"); idoSomeService.doSomething(); }
三、使用工厂代理工厂完成异常增强
1.创建业务接口
public interface IdoSomeService { public void doSomething() throws Exception; }
2.创建业务接口实现类
public class IdoSomeServiceImpl implements IdoSomeService { @Override public void doSomething() throws Exception{ int result=5/0; System.out.println("真实业务"); } }
3.创建切面类
public class MyThrowAdvice{ public void afterThrowing(Exception ex){ System.out.println("=====发生了异常,执行增强操作==============="); } }
4.编写applicationContext.xml配置文件
<!--环绕增强--> <bean id="idoSomeService" class="cn.spring.around.IdoSomeServiceImpl"></bean> <bean id="myAroundAdvice" class="cn.spring.around.MyAroundAdvice"></bean> <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--将增强和业务织入到一起--> <property name="target" ref="idoSomeService"></property> <property name="interceptorNames" value="myAroundAdvice"></property> <!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib--> <property name="proxyTargetClass" value="true"></property> </bean>
5.创建测试类
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取代理工厂 IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService"); try{ idoSomeService.doSomething(); }catch (Exception e){ e.printStackTrace(); } System.out.println("2222222222222"); }
四、使用代理工厂实现最终增强
1.创建业务接口
public interface IdoSomeService { public void doSomething() throws Exception; }
2.创建业务接口实现类
public class IdoSomeServiceImpl implements IdoSomeService { @Override public void doSomething() throws Exception{ int result=5/0; System.out.println("真实业务"); } }
3.创建切面类
public class MyThrowAdvice{ public void afterAdvice(){ System.out.println("======执行最终异常==============="); } }
4.编写applicationContext.xml配置文件
<bean id="idoSomeService" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean> <bean id="myAdvice" class="cn.spring.throwadvice.MyThrowAdvice"></bean> <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:after method="afterAdvice" pointcut-ref="pointcut"></aop:after> </aop:aspect> </aop:config>
5.创建测试类
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取代理工厂 IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService"); try{ idoSomeService.doSomething(); }catch (Exception e){ e.printStackTrace(); } System.out.println("2222222222222"); }
多种方式实现AOP的更多相关文章
- 特性attribute,声明和使用attribute,应用attribute,AOP面向切面,多种方式实现AOP
1 特性attribute,和注释有什么区别2 声明和使用attribute3 应用attribute4 AOP面向切面5 多种方式实现AOP ---------------------------- ...
- IOC和AOP使用扩展 多种方式实现依赖注入
多种方式实现依赖注入 1.Spring 使用setter访问器实现对属性的赋值, 2.Spring 构造constructor方法赋值, 3.接口注入 4.Spring P命名空间注入直接量 sett ...
- Spring学习总结(一)——Spring实现IoC的多种方式
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法.没有IoC的程序中我们使用面向对象编程对象的创 ...
- idea打包jar的多种方式
这里总结出用IDEA打包jar包的多种方式,以后的项目打包Jar包可以参考如下形式: 用IDEA自带的打包形式 用Maven插件maven-shade-plugin打包 用Maven插件maven-a ...
- Java中测试异常的多种方式
使用JUnit来测试Java代码中的异常有很多种方式,你知道几种? 给定这样一个class. Person.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- PDO多种方式取得查询结果
PDO多种方式取得查询结果 01 December 2009 1:26 Tuesday by Sjolzy PDO最大的特点之一是它的灵活性,本节将介绍如何取得查询结果,包括: 数组(数值或关联数组) ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- sql语句分页多种方式ROW_NUMBER()OVER
sql语句分页多种方式ROW_NUMBER()OVER 摘自: http://www.cnblogs.com/CodingArt/articles/1692468.html 方式一 select to ...
- JavaScript中判断为整数的多种方式
之前记录过JavaScript中判断为数字类型的多种方式,这篇看看如何判断为整数类型(Integer). JavaScript中不区分整数和浮点数,所有数字内部都采用64位浮点格式表示,和Java的d ...
随机推荐
- 集群某节点DataNode服务无法启动解决(报java.net.BindException:Address already in use错误)
现象: 在集群中某节点, 启动DataNode服务后马上又Shutdown, 在操作系统没看到有DataNode的日志(可能是服务启动失败, 自动删除了日志文件),幸好在界面上可以查看报错的日志: ...
- filebeat使用multiline丢失数据问题
最近部署filebeat采集日志. 发现配置multiline后,日志偶尔会丢失数据,而且采集到的数据长度都不相同,所以和日志长度没有关系. 查阅filebeat官网后,找到了问题.filebeat有 ...
- jquery的api以及用法总结-数据/操作/事件
数据 .data() 在匹配元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值 .data(obj) 一个用于更新数据的键/值对 .data()方法允许我们再dom元素上 ...
- 《图解HTTP》读后记
看了一遍又一遍…………不如记一下 用做笔记的方式来看,发现了好多之前没发现的知识点.....感觉前几次看了跟没看一样... 1.1HTTP通常被译为超文本传输协议,但这种译法并不严谨.严谨的译名应该为 ...
- Spring 梳理 - WebMvcConfigurerAdapter详解
参考:https://blog.csdn.net/weixin_43453386/article/details/83623242
- 注意!GetThreadPriority的返回值不是系统的优先级值
GetThreadPriority的返回值 Return code/value Description THREAD_PRIORITY_ABOVE_NORMAL 1 Priority 1 point ...
- mysql 5.7 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ...报错
使用mysql在执行一条插入语句时 , ', "hhh"); 报错:Expression #1 of ORDER BY clause is not in GROUP BY clau ...
- map转换成com.google.gson.JsonObject
String json =new Gson().toJson(map); JsonObject jsonObject =new JsonParser().parse(json).getAsJsonOb ...
- mysql数据库设计规则总结
MySQL数据库设计总结 规则1:一般情况可以选择MyISAM存储引擎,如果需要事务支持必须使用InnoDB存储引擎. 注意:MyISAM存储引擎 B-tree索引有一个很大的限制:参与一个索引的 ...
- vue平行组件传值
平行组件传值 通过平行组件传值可以实现任何情境下的传值,包括(父传子,子传父) 代码示例 <!DOCTYPE html> <html lang="en"> ...