spring增强
1.前置增强
接口:ISomeService
public interface ISomeService {
public void doSome();
}
类
public class MyBeforeAdvise implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log=========="); }
}
public class SomeService implements ISomeService{
//核心业务
public void doSome() {
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring04aop01.SomeService"></bean>
<!--02.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring04aop01.MyBeforeAdvise"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property>
<property name="proxyTargetClass" value="true"></property>
</bean> </beans>
单测
//前置增强
@Test
public void test05(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml");
SomeService service = (SomeService) ctx.getBean("proxyService");
service.doSome();
}
2.后置增强
接口:ISomeService
public interface ISomeService {
public void doSome();
}
类
public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("==========after=========="); }
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring05aop_postposition.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="afterReturning" class="cn.happy.spring05aop_postposition.MyAfterReturningAdvice"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="afterReturning"></property>
</bean> </beans>
单测
//后置增强
@Test
public void test06(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
3.环绕增强
接口:ISomeService
public interface ISomeService {
public void doSome();
}
类
public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("before");
methodInvocation.proceed();
System.out.println("after");
return null;
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring06aop_convolution.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="methodAdvice" class="cn.happy.spring06aop_convolution.MyMethodInterceptor"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="methodAdvice"></property>
</bean> </beans>
单测
//环绕增强
@Test
public void test07(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
4.异常增强
接口:ISomeService Mystoo
public interface ISomeService {
public void doSome();
}
public interface Mystoo {
public void run();
public void run(String style);
}
类
public class MystooImpl implements Mystoo {
public void run() { }
public void run(String style) { }
}
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
int age=6/0;
System.out.println("错误");
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} public void doSecont() { }
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring07aop_abnormal.SomeService"></bean> <!--02.增强 通知-->
<bean id="throwsAdvice" class="cn.happy.spring07aop_abnormal.MyThrowsAdvice"></bean> <!--03.aop -->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property>
<!--做怎么样的增强-->
<property name="interceptorNames" value="throwsAdvice"></property> </bean> </beans>
单测
//异常增强
@Test
public void test08(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
Spring增强
1.前置增强
接口:ISomeService
public interface ISomeService {
public void doSome();
}
类
public class MyBeforeAdvise implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==========log=========="); }
}
public class SomeService implements ISomeService{
//核心业务
public void doSome() {
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring04aop01.SomeService"></bean>
<!--02.前置增强(通知)-->
<bean id="beforeAdvice" class="cn.happy.spring04aop01.MyBeforeAdvise"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="beforeAdvice"></property>
<property name="proxyTargetClass" value="true"></property>
</bean> </beans>
单测
//前置增强
@Test
public void test05(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01_aop01.xml");
SomeService service = (SomeService) ctx.getBean("proxyService");
service.doSome();
}
2.后置增强
接口:ISomeService
public interface ISomeService {
public void doSome();
}
类
public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("==========after=========="); }
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring05aop_postposition.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="afterReturning" class="cn.happy.spring05aop_postposition.MyAfterReturningAdvice"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="afterReturning"></property>
</bean> </beans>
单测
//后置增强
@Test
public void test06(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext02_aop02.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
3.环绕增强
接口:ISomeService
public interface ISomeService {
public void doSome();
}
类
public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("before");
methodInvocation.proceed();
System.out.println("after");
return null;
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring06aop_convolution.SomeService"></bean>
<!--02.增强(通知)-->
<bean id="methodAdvice" class="cn.happy.spring06aop_convolution.MyMethodInterceptor"></bean>
<!--03.aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <property name="interceptorNames" value="methodAdvice"></property>
</bean> </beans>
单测
//环绕增强
@Test
public void test07(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext03_aop03.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
4.异常增强
接口:ISomeService Mystoo
public interface ISomeService {
public void doSome();
}
public interface Mystoo {
public void run();
public void run(String style);
}
类
public class MystooImpl implements Mystoo {
public void run() { }
public void run(String style) { }
}
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
int age=6/0;
System.out.println("错误");
}
}
public class SomeService implements ISomeService {
//核心业务
public void doSome() {
System.out.println("拜托别让他一番努力换来是奢求!");
} public void doSecont() { }
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--01.目标对象-->
<bean id="someService" class="cn.happy.spring07aop_abnormal.SomeService"></bean> <!--02.增强 通知-->
<bean id="throwsAdvice" class="cn.happy.spring07aop_abnormal.MyThrowsAdvice"></bean> <!--03.aop -->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property>
<!--做怎么样的增强-->
<property name="interceptorNames" value="throwsAdvice"></property> </bean> </beans>
单测
//异常增强
@Test
public void test08(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext04_aop04.xml");
ISomeService service = (ISomeService) ctx.getBean("proxyService");
service.doSome();
}
spring增强的更多相关文章
- Spring增强代理模式
1. 依赖注入;(掌握) 2. XML自动注入;(掌握) 3. 全注解配置;(掌握) 4. 代理模式;(掌握,难点) 依赖注入 构造参数注入 constructor-arg:构造器注入: index: ...
- 基于XML配置的spring aop增强配置和使用
在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...
- Spring横切面(advice),增强(advisor),切入点(PointCut)(转)
Spring横切面(advice),增强(advisor),切入点(PointCut)的一点理解: 1.Spring管理事务有2种,其中一种是HibernateTransactionManager管理 ...
- JavaEE Spring
1. Spring以一己之力撼动了Sun公司的JavaEE传统重量级框架(EJB),逐渐成为使用最多的JavaEE企业应用开发框架. 2. Spring是分层的JavaEE应用一站式的轻量级开源框 ...
- Spring第二天
Spring第二天 整体课程安排(3天+2天): 第一天:Spring框架入门.IoC控制反转的配置管理.Spring Web集成.Spring Junit集成. 第二天:Spring AOP面向切面 ...
- Spring AOP的实现及源码解析
在介绍AOP之前,想必很多人都听说AOP是基于动态代理和反射来实现的,那么在看AOP之前,你需要弄懂什么是动态代理和反射及它们又是如何实现的. 想了解JDK的动态代理及反射的实现和源码分析,请参见下面 ...
- spring框架笔记
Spring实现依赖注入的两种方式: 1.构造方法注入 2.set方法注入,p标签注入 Spring中事务的两种实现方式: 编程式事务管理 声明式事务管理(推荐) Spring增强类型: Before ...
- 什么。你还没有搞懂Spring事务增强器 ,一篇文章让你彻底搞懂Spring事务,虽然很长但是干货满满
上一篇文章主要讲解了事务的Advisor是如何注册进Spring容器的,也讲解了Spring是如何将有配置事务的类配置上事务的,也讲解了Advisor,pointcut验证流程:但是还未提到的那个Ad ...
- 阿里四面:你知道Spring AOP创建Proxy的过程吗?
Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
随机推荐
- event.keyCode 事件属性
转自:http://www.runoob.com/jsref/event-key-keycode.html <!DOCTYPE html> <html> <head> ...
- python 基础 操作文件和目录
获得当前目录路径 :os.getcwd() 返回指定目录下的所有文件和目录名:os.listdir() 删除一个文件:os.remove(filename) 删除多个空目录 :os.removefir ...
- centos7安装与配置ansible
此次测试总共有三台机,分别如下: ansible服务器:10.0.0.20 client01:10.0.0.21 client02:10.0.0.22 一.安装ansible 1. python版本需 ...
- NodeJS”热部署“代码,实现动态调试(hotnode,可以实现热更新)
NodeJS”热部署“代码,实现动态调试 开发中遇到的问题 如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你在开发 Node.js 实现的 HTTP 应用时会 ...
- 菜鸟大充电啦啦啦啦啦:eclipse SDK 是什么啊
为什么下载是,没有单独的ecipse呢,,总是eclipse-sdk呢 而且还很大几百兆 回复1: Eclipse有好多专用名称,例如Eclipse SDK等.先说一下SDK, Eclipse Pro ...
- linux 环境变量恢复默认值
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 在linux命令下如何访问一个ur ...
- Spring入门第三十课
基于XML的方式配置事务 直接看代码: package logan.study.spring.tx.xml; public interface BookShopDao { //根据书号获取书的单价 p ...
- Windows 7,无法访问internet,DNS无响应
我电脑网络连接显示有internet访问,但是网页打不开,QQ上不了,但可以PING通谷歌DNS 8.8.8.8,一PING域名就无法解析. 解决方法:开始-运行-输入"netsh wins ...
- 第一周作业-Linux基础入门
写在前面 实验楼中linux基础入门的内容很多,几乎涵盖了所有的常用命令.命令的记忆不是一朝一夕的,更不能死记硬背,在实践中多操作,熟悉后自然就记住了.我没有将对每个命令操作结果都截图记录下来(事实上 ...
- Apple Swift中文入门教程【转发】
1 简介 今天凌晨Apple刚刚发布了Swift编程语言,本文从其发布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS& ...