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();访问页面也是 ...
随机推荐
- akamai:与看视频广告等待相比,用户更不能忍受缓冲等待
根据akamai的研究,网络视频广告的位置对于完成率的影响最大,间插广告的完成率比前贴片广告要高18.1%,而前贴片广告比后贴片广告的完成率要高14.3%. 网站的重复访客的视频广告观看完成率比一时兴 ...
- 网站开发进阶(十三)window.onload用法详解
window.onload用法详解 网页中的javaScript脚本代码往往需要在文档加载完成后才能够去执行,否则可能导致无法获取对象的情况,为了避免这种情况的发生,可以使用以下两种方式: 一.将脚本 ...
- mt6577驱动开发 笔记版
3 Preloader & Uboot 3.1 Preloader 3.1.1Preloader结构 Preloader的主题结构在文件:"alps\mediatek\platfor ...
- 【Android 应用开发】BluetoothClass详解
一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该 ...
- ZooKeeper的快速搭建
本文是ZooKeeper的快速搭建,旨在帮助大家以最快的速度完成一个ZK集群的搭建,以便开展其它工作.本方不包含多余说明及任何调优方面的高级配置.如果要进行更深一层次的配置,请移步<ZooKee ...
- SharePoint WebService 之更新审批状态
SharePoint列表使用WebService操作,可以进行增删改查,但是操作开启审批功能列表的时候,会遇到列表项审批的问题,只要进行修改,该项目就会变成待定状态,然后想要修改审批状态,就使用Upd ...
- nasm预处理器(4)
nasm定义了一套标准宏,当开始处理源文件时,这些宏都已经被定义了,如果希望程序在执行前没有预定义的宏存在,可以使用%clear清空预处理器的一切宏. __NASM_MAJOR__ 主版本号 __NA ...
- Python 3.7 将引入 dataclass 装饰器
简评:Python 3.7 将于今年夏天发布,Python 3.7 中将会有许多新东西,最激动人心的新功能之一是 dataclass 装饰器. 什么是 Data Class 大多数 Python 开发 ...
- java 垃圾回收总结(1)
java 垃圾回收总结(1) 以前看过很多次关于垃圾回收相关的文章,都只是看过就忘记了,没有好好的整理一下,发现写文章可以强化自己的记忆. java与C,c++有很大的不同就是java语言开发者不 ...
- 全面解读Java NIO工作原理(2)
全面解读Java NIO工作原理(2) 2011-12-14 10:31 Rollen Holt Rollen Holt的博客 我要评论(0) 字号:T | T JDK 1.4 中引入的新输入输出 ( ...