一、使用代理工厂完成声明式增强

1.创建业务接口

public interface IdoSomeService {
    public void doSomething();
}

2.创建接口实现类

public class IdoSomeServiceImpl implements  IdoSomeService{
    @Override
    public void doSomething() {
        System.out.println("真实业务");
    }
}

3.创建切面类

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

4.编写applicationContext.xml配置文件

<!--注入业务Bean-->
    <bean id="idoSomeService" class="cn.spring.proxyfactory.IdoSomeServiceImpl"></bean>
    <!--增强:切面-->
    <bean id="myBeforeAdvice" class="cn.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>
     </bean>

5.创建测试类

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService  idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
        idoSomeService.doSomething();
    }
}

二、使用代理工厂完成环绕增强

1.创建业务接口

public interface IdoSomeService {
    public void doSomething();
}

2.创建业务接口实现类

public class IdoSomeServiceImpl implements  IdoSomeService{
    @Override
    public void doSomething() {
        System.out.println("真实业务");
    }
}

3.创建切面类

public class MyAroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕前");

        //调用核心业务方法也可以获取方法内的参数 也可以获取目标对象
        Object proceed = methodInvocation.proceed();
        Object aThis = methodInvocation.getThis();
        System.out.println(aThis);

        System.out.println("环绕后");
        return proceed;
    }
}

4.编写applicationContext.xml配置文件

    环绕增强
    <bean id="idoSomeService"  class="cn.spring.around.IdoSomeServiceImpl"></bean>
    <bean id="myAroundAdvice"  class="cn.spring.around.MyAroundAdvice"></bean>
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="idoSomeService"></property>
        <property name="interceptorNames" value="myAroundAdvice"></property>
        <!--更换代理方式    proxyTargetClass默认值为false   默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
        <property name="proxyTargetClass" value="true"></property>
     </bean>

4.创建测试类

public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService idoSomeService=(IdoSomeService)context.getBean("proxyFactory");
        idoSomeService.doSomething();
    }

三、使用工厂代理工厂完成异常增强

1.创建业务接口

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

2.创建业务接口实现类

public class IdoSomeServiceImpl implements IdoSomeService {
    @Override
    public void doSomething() throws Exception{
        int result=5/0;
        System.out.println("真实业务");
    }
}

3.创建切面类

public class MyThrowAdvice{
   public void afterThrowing(Exception ex){
       System.out.println("=====发生了异常,执行增强操作===============");
   }
}

4.编写applicationContext.xml配置文件

<!--环绕增强-->
    <bean id="idoSomeService"  class="cn.spring.around.IdoSomeServiceImpl"></bean>
    <bean id="myAroundAdvice"  class="cn.spring.around.MyAroundAdvice"></bean>
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="idoSomeService"></property>
        <property name="interceptorNames" value="myAroundAdvice"></property>
        <!--更换代理方式    proxyTargetClass默认值为false   默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
        <property name="proxyTargetClass" value="true"></property>
     </bean>

5.创建测试类

 public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
        try{
            idoSomeService.doSomething();

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("2222222222222");
    }

四、使用代理工厂实现最终增强

1.创建业务接口

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

2.创建业务接口实现类

public class IdoSomeServiceImpl implements IdoSomeService {
    @Override
    public void doSomething() throws Exception{
        int result=5/0;
        System.out.println("真实业务");
    }
}

3.创建切面类

public class MyThrowAdvice{
    public void afterAdvice(){
        System.out.println("======执行最终异常===============");
    }
}

4.编写applicationContext.xml配置文件

<bean id="idoSomeService" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
    <bean id="myAdvice" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
    <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>

5.创建测试类

public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取代理工厂
        IdoSomeService idoSomeService=(IdoSomeService)context.getBean("idoSomeService");
        try{
            idoSomeService.doSomething();

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("2222222222222");
    }

多种方式实现AOP的更多相关文章

  1. 特性attribute,声明和使用attribute,应用attribute,AOP面向切面,多种方式实现AOP

    1 特性attribute,和注释有什么区别2 声明和使用attribute3 应用attribute4 AOP面向切面5 多种方式实现AOP ---------------------------- ...

  2. IOC和AOP使用扩展 多种方式实现依赖注入

    多种方式实现依赖注入 1.Spring 使用setter访问器实现对属性的赋值, 2.Spring 构造constructor方法赋值, 3.接口注入 4.Spring P命名空间注入直接量 sett ...

  3. Spring学习总结(一)——Spring实现IoC的多种方式

    控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法.没有IoC的程序中我们使用面向对象编程对象的创 ...

  4. idea打包jar的多种方式

    这里总结出用IDEA打包jar包的多种方式,以后的项目打包Jar包可以参考如下形式: 用IDEA自带的打包形式 用Maven插件maven-shade-plugin打包 用Maven插件maven-a ...

  5. Java中测试异常的多种方式

    使用JUnit来测试Java代码中的异常有很多种方式,你知道几种? 给定这样一个class. Person.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  6. PDO多种方式取得查询结果

    PDO多种方式取得查询结果 01 December 2009 1:26 Tuesday by Sjolzy PDO最大的特点之一是它的灵活性,本节将介绍如何取得查询结果,包括: 数组(数值或关联数组) ...

  7. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

  8. sql语句分页多种方式ROW_NUMBER()OVER

    sql语句分页多种方式ROW_NUMBER()OVER 摘自: http://www.cnblogs.com/CodingArt/articles/1692468.html 方式一 select to ...

  9. JavaScript中判断为整数的多种方式

    之前记录过JavaScript中判断为数字类型的多种方式,这篇看看如何判断为整数类型(Integer). JavaScript中不区分整数和浮点数,所有数字内部都采用64位浮点格式表示,和Java的d ...

随机推荐

  1. 记一次Burp Suite的使用实例

       下载完的Bur是这样的,双击jar即可 最右边的键一直按,傻瓜式         先将要抓包的网页打开,此次以上传图片为例     第一步当然是先下载Burp Suite之后打开,查看设置代理地 ...

  2. opencv边缘检测-拉普拉斯算子

    sobel算子一文说了,索贝尔算子是模拟一阶求导,导数越大的地方说明变换越剧烈,越有可能是边缘. 那如果继续对f'(t)求导呢? 可以发现"边缘处"的二阶导数=0. 我们可以利用这 ...

  3. Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)

    @EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...

  4. 设计模式的七大原则(Java)

    一.OOP三大基本特性 OOP 面向对象程序设计(Object Oriented Programming)作为一种新方法,其本质是以建立模型体现出来的抽象思维过程和面向对象的方法.模型是用来反映现实世 ...

  5. Docker 第一个HelloWorld镜像

    Docker 创建第一个HelloWorld镜像: 创建Dockerfile FROM alpine CMD "echo" "Hello World!" 通过D ...

  6. k阶斐波那契数列fibonacci第n项求值

    已知K阶斐波那契数列定义为:f0 = 0,  f1 = 0, … , fk-2 = 0, fk-1 = 1;fn = fn-1 + fn-2 + … + fn-k , n = k , k + 1, … ...

  7. Knative 实战:基于 Knative Serverless 技术实现天气服务-下篇

    上一期我们介绍了如何基于 Knative Serverless 技术实现天气服务-上篇,首先我们先来回顾一下上篇介绍的内容: 通过高德天气 API 接口,每隔 3 个小时定时发送定时事件,将国内城市未 ...

  8. B/S 工业互联网 地铁行业

    前言 近几年,互联网与交通运输的融合,改变了交易模式,影响着运输组织和经营方式,改变了运输主体的市场结构.模糊了运营与非营运的界限,也更好的实现了交通资源的集约共享,同时使得更多依靠外力和企业推动交通 ...

  9. 【TencentOS tiny】深度源码分析(2)——调度器

    温馨提示:本文不描述与浮点相关的寄存器的内容,如需了解自行查阅(毕竟我自己也不懂) 调度器的基本概念 TencentOS tiny中提供的任务调度器是基于优先级的全抢占式调度,在系统运行过程中,当有比 ...

  10. LeetCode初级算法--数组02:旋转数组

    LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...