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();访问页面也是 ...
随机推荐
- OpenGL下多个sampler在shader中的使用
在OpenGL中,sampler2D/Cube等做为uniform可以在fragment shader中使用.结合glActiveTexture和glUniform1i,实现texture与sampl ...
- iOS下FMDB的多线程操作(一)
iOS中一些时间比较长的操作都应该放在子线程中,以避免UI的卡顿.而sqlite 是非线程安全的,故在多线程中不能共用同一个数据库连接,否则会导致EXC_BAD_ACCESS.所以我们可以在子线程中创 ...
- Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!
Google官方网络框架Volley实战--QQ吉凶测试,南无阿弥陀佛! 这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单 无图无真相 直接撸代码了,详细解释都已经写在注释里了 ...
- Android网络请求框架之Retrofit实践
网络访问框架经过了从使用最原始的AsyncTask构建简单的网络访问框架(甚至不能称为框架),后来使用开源的android-async-http库,再到使用google发布的volley库,一直不懈的 ...
- disable table 失败的处理
相信每一个维护hbase集群的运维人员一定碰到过disable失败,陷入无穷的"Region has been PENDING_CLOSE for too long..."状态,此 ...
- How I Turned Down $300,000 from Microsoft to go Full-Time on GitHub
How I Turned Down $300,000 from Microsoft to go Full-Time on GitHub (我是如何拒绝微软30w的诱惑,专注于GitHub事业) 当我老 ...
- netsh自动配置网络
工作需要经常在多个网络中切换,每次都要配置ip等,写个脚本一键完成配置: netsh interface ip set address "本地连接" static "ip ...
- 学好js的步骤
第一步:打基础,建议看<JavaScript权威指南>,并作为参考书,供以后学习用. 第二步:进阶学习:建议看<JavaScript高级程序设计>,<精通JavaScri ...
- Web 前台优化
大型网站--前端性能优化和规范 2013-10-28 09:00 by 贤达, 2769 阅读, 10 评论, 收藏, 编辑 Web性能涉及的范围太广,但一般web开发者在程序上线以后很多都曾遇到 ...
- Django1.6版本的PG数据库定义手动升级
Django1.7以后添加了migration功能,数据库定义的升级完全实现自动化,之前是通过一个叫south的app来做的.这篇文章谈一下1.6下的手动更新升级. 1.table create和ta ...