SSM-Spring-11:Spring中使用代理工厂Bean实现aop的四种增强
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
说说那四种增强:前置增强,后置增强,环绕增强,异常增强
那什么是代理工厂bean呢?
org.springframework.aop.framework.ProxyFactoryBean
就是这个东西,他可以实现对方法的增强
@No.1:前置增强:
需要前置增强的类SomeServiceImpl
package cn.dawn.day11aop01; /**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
}
}
前置增强的内容的类,可以说实现前置增强接口的类LoggerBefore
package cn.dawn.day11aop01; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /**
* Created by Dawn on 2018/3/5.
*/
/*前置增强*/
public class LoggerBefore implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("日志记录");
}
}
配置文件中:
<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day11aop01.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="before" class="cn.dawn.day11aop01.LoggerBefore"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="before"></property>
</bean> </beans>
单测方法:
package cn.dawn.day11aop01; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180305 {
@Test
/*aop代理工厂bean前置增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day11aop01.xml");
SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
service.doSome();
}
}
@No.2:后置增强:
需要后置增强的类:SomeServiceImpl(此类和前置那个写在不同包下,一会的配置文件也不同,往后都是如此)
package cn.dawn.day12aop02; /**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
}
}
后置增强的内容的的类,他与前置增强的那个实现的接口不同:LoggerAfter
package cn.dawn.day12aop02; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; /**
* Created by Dawn on 2018/3/5.
*/
/*后置增强*/
public class LoggerAfter implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("===============after==================");
}
}
配置文件中:
<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day12aop02.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="afteradvice" class="cn.dawn.day12aop02.LoggerAfter"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="afteradvice"></property>
</bean> </beans>
单测方法:
package cn.dawn.day12aop02;
import cn.dawn.day12aop02.SomeServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Dawn on 2018/3/3.
*/
public class test20180305 {
@Test
/*aop代理工厂bean后置增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day12aop02.xml");
SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
service.doSome();
}
}
@No.3:环绕增强:
需要环绕增强的类:SomeServiceImpl
package cn.dawn.day13aop03; /**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
}
}
环绕增强内容的:
package cn.dawn.day13aop03; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; /**
* Created by Dawn on 2018/3/8.
*/
/*环绕增强需要实现MethodInterceptor这个接口*/
public class MethodAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("前置增强");
/*它可以使前置增强和后置增强分开,同时实现前置和后置*/
methodInvocation.proceed();
System.out.println("后置增强");
return null;
}
}
配置文件:
<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="methodAdvice"></property>
</bean> </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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="methodAdvice"></property>
</bean> </beans>
@No.4:异常增强:
异常增强是主要用在他出错的时候,进行记录用的,此处模拟起来简单的就是除0异常和空指针异常
写一个类,它里面的方法存在异常,运行时异常
SomeServiceImpl类
package cn.dawn.day14aop04; /**
* Created by Dawn on 2018/3/8.
*/
public class SomeServiceImpl {
public void doSome() {
System.out.println("do something");
String abc=null;
System.out.println(abc.toString());;
}
}
此处模拟的是空指针异常,当然异常种类很多,不一一模拟了
实现ThrowsAdvice接口的类
package cn.dawn.day14aop04; import org.springframework.aop.ThrowsAdvice; /**
* Created by Dawn on 2018/3/8.
*/
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
System.out.println("网络中断XXXXX-错误号05289");
}
}
他这个接口里面没有方法要重写,很是奇怪,那是随便写什么defgabc的都可以吗?不是的,翻他的源码,他在上面的注释里提供了模板,只有按照他模板写的才能读取到,此处我用了他的其中一个模板方法
配置文件中:
<?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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--要增强的对象-->
<bean id="service" class="cn.dawn.day14aop04.SomeServiceImpl"></bean>
<!--增强的内容-->
<bean id="myThrowsAdvice" class="cn.dawn.day14aop04.MyThrowsAdvice"></bean>
<!--代理工厂bean-->
<bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--要增强的对象-->
<property name="target" ref="service"></property>
<!--增强的内容-->
<property name="interceptorNames" value="myThrowsAdvice"></property>
</bean> </beans>
单测方法:
package cn.dawn.day14aop04; import cn.dawn.day14aop04.SomeServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180305 {
@Test
/*aop代理工厂bean异常增强*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day14aop04.xml");
SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
try{
service.doSome();
}catch (Exception ex){
ex.printStackTrace();
} }
}
看到这儿的try-catch了吧,他的作用是让单测能通过单元测试,能变成对勾,能执行到结束,不会因为模拟的异常而中断
--------------------------End-------------------------
基于代理工厂Bean实现aop的四种增强总结完毕
SSM-Spring-11:Spring中使用代理工厂Bean实现aop的四种增强的更多相关文章
- sping练习,在Eclipse搭建的Spring开发环境中,使用工厂方式创建Bean对象,将创建的Bean对象输出到控制台。
相关 知识 >>> 相关 练习 >>> 实现要求: 在Eclipse搭建的Spring开发环境中,使用工厂方式创建Bean对象,将创建的Bean对象输出到控制台.要 ...
- spring AOP 代理机制、执行过程、四种实现方式及示例详解
1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...
- Python中xml、字典、json、类四种数据的转换
最近学python,觉得python很强很大很强大,写一个学习随笔,当作留念注:xml.字典.json.类四种数据的转换,从左到右依次转换,即xml要转换为类时,先将xml转换为字典,再将字典转换为j ...
- Webform中Repeater控件--绑定嵌入C#代码四种方式
网页里面嵌入C#代码用的是<% %>,嵌入php代码<?php ?> 绑定数据的四种方式: 1.直接绑定 <%#Eval("Code") %> ...
- JS中的this是什么,this的四种用法
在Javascript中,this这个关键字可以说是用的非常多,说他简单呢也很简单,说他难呢也很难,有的人开发两三年了,自己好像也说不清this到底是什么.下面我们来看看: 1.在一般函数方法中使用 ...
- 解决 spring boot 线程中使用@Autowired注入Bean的方法,报java.lang.NullPointerException异常
问题描述 在开发中,因某些业务逻辑执行时间太长,我们常使用线程来实现.常规服务实现类中,使用 @Autowired 来注入Bean,来调用其中的方法.但如果在线程类中使用@Autowired注入的Be ...
- spring和springmvc中,Configuration注解Bean重复加载
问题:bean重复加载1.如下代码所示,开启Configuration注解,实现Bean代码注入,发现bean重复加载 @Configuration public class EhCacheConfi ...
- pageContext中page、request、session、application四种范围变量的用法。
在PageContext中有很多作用域 第一种:PageContext.PAGE_SCOPE适用于当前页面的作用域,其接受数据的代码是pageContext.getAttribute();访问页面也是 ...
随机推荐
- Orientation Auto Rotation旋转屏幕crash问题(Unity3D开发之十四)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/44133127 ...
- Learning ROS forRobotics Programming Second Edition学习笔记(八)indigo rviz gazebo
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- "《算法导论》之‘排序’":线性时间排序
本文参考自一博文与<算法导论>. <算法导论>之前介绍了合并排序.堆排序和快速排序的特点及运行时间.合并排序和堆排序在最坏情况下达到O(nlgn),而快速排序最坏情况下达到O( ...
- Detours修改段属性漏洞
v:* { } o:* { } w:* { } .shape { }p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-botto ...
- Linux - 用make进行工程编译
首先建立好自己的工作目录 然后创建主函数main.cpp 接着写sinValue.h和cosValue.h函数文件 先按照传统方式进行编译运行 然后用make,先写makefile文件 将原来生成的文 ...
- ZeroC Ice IceGrid Node和IceGrid
IceGrid Node介绍 绝大多数分布式系统都有一个共同特点,即分布在各个主机上的节点进程并不是完全独立的,而是彼此之间有相互联系和通信的.集群对集群中的节点有一些控制指令,如部署.启停或者调整某 ...
- javascript语言扩展:可迭代对象(1)
在ECMAScript中我们知道可以通过for in语句进行对象属性的遍历,当然这些属性不包括继承而来的属性: var ary = [1,2,3,"aa",4]; for(i in ...
- JAVAEE——BOS物流项目12:角色、用户管理,使用ehcache缓存,系统菜单根据登录人展示
1 学习计划 1.角色管理 n 添加角色功能 n 角色分页查询 2.用户管理 n 添加用户功能 n 用户分页查询 3.修改Realm中授权方法(查询数据库) 4.使用ehcache缓存权限数据 n 添 ...
- access窗体主体居中
Private Sub Form_Load()DoCmd.Echo False Dim x, y As IntegerDoCmd.Maximizex = Me.WindowWidthy = Me.Wi ...
- 【转载】tomcat+nginx+redis实现均衡负载、session共享(二)
今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html. 1.red ...