利用代理工厂实现增强

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. 《Mysql - 为什么只查一行的数据,也这么慢?》

    概念 - 在某些情况下,“查一行”,也会执行得特别慢. - 下面分析在什么情况下,会出现这个现象. - 基础工作(构建数据库环境) - 建立 t 表,并写入 10W 的数据. CREATE TABLE ...

  2. (一)Spring Security Demo 登陆与退出

    文章目录 配置springSecurityFilterChain过滤器 配置身份验证 加载配置 登陆项目 退出 下面的代码需要spring环境的支持: 看这个系列博客之前,需要这个博客,大概了解下 s ...

  3. Visual Studio 2019 离线安装方法

    1. 网址 1.1 阅读官方离线安装教程 离线安装官网 仔细阅读离线安装官网,差不多就能学会如何下载. 1.2 工作负荷和组件 ID 进入这个网址,Visual Studio 工作负荷和组件 ID,单 ...

  4. Python02之continue,break语句

    Python中的break和continue用法基本一样 break和continue都是用在while和for循环中,而不是跳出if...elif..else的判断语句中,跳出是直接跳出语句所在的w ...

  5. sequence(线段树+单调栈) (2019牛客暑期多校训练营(第四场))

    示例: 输入: 31 -1 11 2 3 输出: 3 题意:求最大的(a区间最小值*b区间和) 线段树做法:用单调栈求出每个数两边比b数组大的左右边界,然后用线段树求出每段区间的和sum.最小前缀ls ...

  6. ajax提交异常解决

    一.遇到的问题 在项目中使用ajax提交表单失败,并且后台程序都没有执行,分析具体问题是由于post表单时contenttype的类型不一致. 二.解决方式 $.ajax({ type: 'post' ...

  7. iis安装ssl证书

    在证书控制台下载IIS版本证书,下载到本地的是一个压缩文件,解压后里面包含.pfx文件是证书文件,pfx_password.txt是证书文件的密码. 友情提示: 每次下载都会产生新密码,该密码仅匹配本 ...

  8. 编写Postgres扩展之三:调试

    原文:http://big-elephants.com/2015-10/writing-postgres-extensions-part-iii/ 编译:Tacey Wong 在上一篇关于编写Post ...

  9. 使用node建立本地服务器访问静态文件

    最终目录结构 demo │ node_modules └───public │ │ index.html │ │ index.css │ └───index.js └───server.js 一.使用 ...

  10. 安卓开发之生成cache目录和files目录

    package com.lidaochen.test; import android.os.Bundle; import android.support.v7.app.AppCompatActivit ...