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其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
随机推荐
- html锚链接
锚点(anchor):其实就是超链接的一种,一种特殊的超链接 普通的超链接,<a href="路径"></a> 是跳转到不同的页面 而锚点,<a hr ...
- 关于 sklearn.decomposition.KernelPCA的简单介绍
from sklearn import decomposition import numpy as np A1_mean = [1, 1] A1_cov = [[2, .99], [1, 1]] A1 ...
- Tiny4412学习杂记
1.Android 挂载NFS 使用 busybox mount 来替代mount命令 2.修改Uboot中fastboot最大buff 使用U-boot烧写Android5.0的时候出现 remo ...
- BIOS MCSDK 2.0 User Guide - Acronyms and Definitions
BIOS MCSDK 2.0 User Guide - Texas Instruments Wiki Acronyms and Definitions The following acronyms a ...
- Java探索之旅(4)——方法和Random&Math类
1.基本知识点 ❶方法在C++里面称为函数.调用方法时,应该类型兼容--即不需显式类型转换即可将形参传递给实参. ❷形参的改变不影响实参的值. ❸Java注重模块化设计和自顶向下的设 ...
- Activity--弹出底部窗口
第一步 : 退出时候的布局文件exit_dialog_from_settings.xml <?xml version="1.0" encoding="UTF-8&q ...
- codeforces educational round25
A #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ; string s; c ...
- geneid/genesymbol/ensemblid等之间的转换
在基因注释时,难免碰到各种GENE在不同数据库之间的ID转换(例如,Ensembl ID 转Entrez ID,或者Entrez ID与GENE Symbol之间的转换).这里介绍一下常用的三个在线网 ...
- cookie 、Session 和自定义分页
cookie cookie的由来 大家都知道Http协议是无状态的. 无状态的意思 是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系, 他不会受前面的请求响应情况直接影响, ...
- easyui学习笔记1-(datagrid+dialog)
jQuery EasyUI是一组基于jQuery的UI插件集合体.我的理解:jquery是js的插件,easyui是基于jquery的插件.用easyui可以很轻松的打造出功能丰富并且美观的UI界面. ...



