利用代理工厂实现增强

com.Spring.proxyfactory中的IdoSomeService

package cn.spring.proxyfactory;

public interface IdoSomeService {
public void doSome();
}

IdoSomeServiceImpl类

package cn.spring.proxyfactory;

/**
* 原始对象
*/
public class IdoSomeServiceImpl implements IdoSomeService{
public void doSome(){
System.out.println("=========真实业务===========");
}
}

MyBeforeAdavice类

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==============前置增强=================");
}
}

applicationContext.xml

<!--注入业务Bean-->
<bean id="idoSomeService" class="com.Spring.proxyfactory.IdoSomeServiceImpl"></bean>
<!--增强:切面-->
<bean id="myBeforeAdvice" class="com.Spring.proxyfactory.MyBeforeAdvice"></bean>
<!--使用代理工厂实现增强-->
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeService"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myBeforeAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
<property name="proxyTargetClass" value="true"></property>
</bean>

ProxyFactoryTest测试类

package com.Spring;

import com.Spring.proxyfactory.IdoSomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Around {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂Bean
IdoSomeService idoSomeService = (IdoSomeService)ctx.getBean("proxyFactory");
idoSomeService.doSome();
}
}

环绕增强

com.Spring.around中的IdoSomeService接口

package cn.spring.around;

public interface IdoSomeService {
public void doSome();
}

IdoSomeServiceImpl类

package cn.spring.around;
/**
* 原始对象
*/
public class IdoSomeServiceImpl implements IdoSomeService {
public void doSome(){
System.out.println("=========真实业务===========");
}
}

MyAroundAdvice类

package cn.spring.around;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 环绕增强
*/
public class MyAroundAdvice implements MethodInterceptor{ @Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("============环绕前============="); //调用核心业务方法 也可以获取方法内的参数 也可以获取目标对象
Object proceed = invocation.proceed();
Object aThis = invocation.getThis();
System.out.println(aThis); System.out.println("============环绕后=============");
return proceed;
}
}

AroundTest测试类

package cn.spring;

import cn.spring.throwadvice.IdoSomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AroundTest {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂Bean
IdoSomeService idoSomeServiceProxy = (IdoSomeService) ctx.getBean("idoSomeService"); try {
idoSomeServiceProxy.doSome();
} catch (Exception e) {
e.printStackTrace();
} System.out.println("");
}
}

applicationContext.xml文件

<bean id="idoSomeServie" class="com.Spring.around.IdoSomeServiceImpl"></bean>
<bean id="myAroundAdvice" class="com.Spring.around.MyAroundAdvice"></bean>
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeServie"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myAroundAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
<property name="proxyTargetClass" value="true"></property>
</bean> <aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..around.*.*(..))"/>
<aop:aspect ref="myAroundAdvice">
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>

最终异常

com.Spring.throwadvice中的IdoSomeService接口

package cn.spring.throwadvice;

public interface IdoSomeService {
public void doSome() throws Exception;
}

IdoSomeServiceImpl类

package cn.spring.throwadvice;
/**
* 原始对象
*/
public class IdoSomeServiceImpl implements IdoSomeService {
public void doSome() throws Exception{
int result=/;
System.out.println("=========真实业务===========");
}
}

MyAdvice类

package cn.spring.throwadvice;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.ThrowsAdvice; import java.lang.reflect.Method; public class MyAdvice {
public void afterThrowing(Exception ex){
System.out.println("=====发生了异常,执行增强操作===============");
} public void afterAdvice(){
System.out.println("======执行最终异常===============");
}
}

applicationContext.xml文件

<aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
<aop:aspect ref="myAdvice">
<aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
<aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
</aop:config>

SpringAOP进阶的更多相关文章

  1. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

  2. Spring框架进阶3

    Spring框架进阶3 测试spring_jdbc和spring对事务的管理 先配置相应的pom <?xml version="1.0" encoding="UTF ...

  3. nodejs进阶(6)—连接MySQL数据库

    1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...

  4. nodejs进阶(4)—读取图片到页面

    我们先实现从指定路径读取图片然后输出到页面的功能. 先准备一张图片imgs/dog.jpg. file.js里面继续添加readImg方法,在这里注意读写的时候都需要声明'binary'.(file. ...

  5. JavaScript进阶之路(一)初学者的开始

    一:写在前面的问题和话 一个javascript初学者的进阶之路! 背景:3年后端(ASP.NET)工作经验,javascript水平一般般,前端水平一般般.学习资料:犀牛书. 如有误导,或者错误的地 ...

  6. nodejs进阶(3)—路由处理

    1. url.parse(url)解析 该方法将一个URL字符串转换成对象并返回. url.parse(urlStr, [parseQueryString], [slashesDenoteHost]) ...

  7. nodejs进阶(5)—接收请求参数

    1. get请求参数接收 我们简单举一个需要接收参数的例子 如果有个查找功能,查找关键词需要从url里接收,http://localhost:8000/search?keyword=地球.通过前面的进 ...

  8. nodejs进阶(1)—输出hello world

    下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等. 搭建服务器页面输出hello world var  http  =  require ...

  9. [C#] 进阶 - LINQ 标准查询操作概述

    LINQ 标准查询操作概述 序 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> ...

随机推荐

  1. 多线程(8) — ThreadLocal

    ThreadLocal是一个线程的局部变量,也就是只有当前线程可以访问,是线程安全的.为每一个线程分配不同的对象,需要在应用层面保证ThreadLocal只起到简单的容器作用. ThreadLocal ...

  2. (零)linux 学习 -- 从 shell 开始

    The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap02.html 文章目录 前言 什么是 she ...

  3. PAT甲级 链表题_C++题解

    链表处理 PAT (Advanced Level) Practice 链表题 目录 <算法笔记> 重点摘要:静态链表 1032 Sharing (25) 1052 Linked List ...

  4. Android Manifest 中 uses-feature 和 uses-permission的作用 关系和区别

    Manifest中的 <uses-permission android:name="android.permission.CAMERA" /> 和 <uses-f ...

  5. 百人研发团队的难题:研发管理、绩效考核、组织文化和OKR

    分享一个公司规模近200,研发占一半的创业公司 Worktile 在研发团队管理方面的玩法,仅供百人左右研发团队参考~ 什么是研发团队?简单的说,你熟悉的那帮穿格子衬衫,以程序员为核心组成的团队,就是 ...

  6. vue 写一个瀑布流插件

    效果如图所示: 采用了预先加载图片,再计算高度的办法..网络差的情况下,可能有点卡 新建 vue-water-easy.vue  组件文件 <template> <div class ...

  7. html中标签的英文含义!

    html中的标签缩写的英文是什么?  标签  英文全称  含义 ul unordered lists 无序列表 li list item 列表项目 ol ordered lists 有序列表 dl d ...

  8. Go 操作 Mysql(一)

    关于 Go 的标准库 database/sql 和 sqlx database/sql 是 Go 操作数据库的标准库之一,它提供了一系列接口方法,用于访问数据库(mysql,sqllite,oralc ...

  9. Go part 7 反射,反射类型对象,反射值对象

    反射 反射是指在程序运行期间对程序本身进行访问和修改的能力,(程序在编译时,变量被转换为内存地址,变量名不会被编译器写入到可执行部分,在运行程序时,程序无法获取自身的信息) 支持反射的语言可以在程序编 ...

  10. 【转载】Response对象的作用以及常用方法属性

    Response对象是Asp.Net应用程序中非常重要的一个内置对象,其作用为负责将服务器执行好的信息输出给客户端,即作用主要为响应客户端请求并将服务器的响应返回给用户,在页面的临时跳转中,也可使用R ...