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其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
随机推荐
- 关于redis,学会这8点就够了(转)
1.redis是什么 redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串.哈希.列表.队 ...
- 如何设置 Windows 默认命令行窗口大小和缓冲区大小
关键字: 命令行不能全屏 命令行最大化只有一半屏幕 命令行 字体 背景 颜色 解决方案:http://unmi.cc/save-windows-command-size/ 简要说明: win+r,输入 ...
- 基于C语言的ssdb笔记 ----hashmap的简单实例
ssdb支持 zset, map/hash, list, kv 数据结构,同redis差不多.下面是关于ssdb hsahmap的使用笔记 1.ssdb hashmap的命令 1.hset name ...
- JavaScript学习系列6 -- JavaScript中的垃圾回收(内存释放)
程序开发中,涉及到的内存生命周期基本是一样的,分为以下三步 1. 分配需要的内存 2. 使用分配到的内存 3. 释放其内存 ----什么时候释放内存,以及需要释放哪些变量的内存, 就是垃圾回收机 ...
- 【转】Subversion快速入门教程-动画演示
如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关心的问题,与CVS相比,Subversion有更多的选择,也更加的容易,几个命令就可以建立一套服务器环境,可以使用起来,这里配 ...
- SqlServer2012-创建表、删除表 增加字段 删除字段操作
新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default \'默认值\' nu ...
- java线程基础知识----java daemon线程
java线程是一个运用很广泛的重点知识,我们很有必要了解java的daemon线程. 1.首先我们必须清楚的认识到java的线程分为两类: 用户线程和daemon线程 A. 用户线程: 用户线程可以简 ...
- centos运行netcore error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
Error: Another program is already listening on a port that one of our HTTP servers is configured to ...
- SCUT - 337 - 岩殿居蟹 - 线段树 - 树状数组
https://scut.online/p/337 这个东西是个阶梯状的.那么可以考虑存两棵树,一棵树是阶梯的,另一棵树的平的,随便一减就是需要的阶梯. 优化之后貌似速度比树状数组还惊人. #incl ...
- ZOJ - 3777(状压dp)
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...



