利用代理工厂实现增强

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. [VS] - 手工打开 WCF 客户端调试工具

    操作步骤 1. 在开始菜单中找到 Visual Studio 命令行工具 2. 输入命令 wcftestclient 即可打开 WCF 客户端测试工具 参考资料http://www.cnblogs.c ...

  2. Hystrix【参数配置及缓存】

    1.常用参数说明 hystrix参数的详细配置可参照 https://github.com/Netflix/Hystrix/wiki/Configuration 下面是一些常用的配置: 配置项 默认值 ...

  3. 多线程(10) — Future模式

    Future模式是多线程开发中常用常见的一种设计模式,它的核心思想是异步调用.在调用一个函数方法时候,如果函数执行很慢,我们就要进行等待,但这时我们可能不着急要结果,因此我们可以让被调者立即返回,让它 ...

  4. java8 : 流

    package day02.com.offcn.test; import java.io.IOException; import java.nio.charset.Charset; import ja ...

  5. mybatis与Spring集成(Aop整合PagerAspect插件)

    目的: Mybatis与spring集成 Aop整合pagehelper插件 Mybatis与spring集成 导入pom依赖 <?xml version="1.0" enc ...

  6. ionic开发遇到的问题总结

    前言 ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.css和js的性能,构建高效的应用程序,而且还可以用于构建Sass和AngularJS的优化.ionic会是一个可 ...

  7. GoLand中同一个目录下的package无法调用

    代码结构: 三个代码的package 都是 pipefilter,执行split_filter_test.go 就会提示   undefined:xxxxxxx Golang实际都可以自己补全另一个文 ...

  8. Powershell学习笔记:(二)、基础知识

    从Window7以后,WIndows系统都自带了Windows PowerShell. 自带版本如下 WIndow7  2.0 WIndow8   3.0 Window8.1      4.0 Win ...

  9. jQuery 基础知识

    一.序言 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后的又一个优秀的JavaScript代码库(JavaScript框架).jQuery设计的宗旨是"W ...

  10. php中需要注意的函数(持续更新)

    explode 函数 $a = null; explode("#",$a); //不会报错会返回一个只包含空字符串的数组