SSM-Spring-11:Spring中使用代理工厂Bean实现aop的四种增强

简介:   ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------     说说那四种增强:前置增强,后置增强,环绕增强,异常增强 那什么是代理工厂bean呢?   org.

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

说说那四种增强:前置增强,后置增强,环绕增强,异常增强

那什么是代理工厂bean呢?

  org.springframework.aop.framework.ProxyFactoryBean

  就是这个东西,他可以实现对方法的增强

 

@No.1:前置增强:

  需要前置增强的类SomeServiceImpl

 

package cn.dawn.day11aop01;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
    }
}

 

  前置增强的内容的类,可以说实现前置增强接口的类LoggerBefore

 

package cn.dawn.day11aop01;


import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Dawn on 2018/3/5.
 */
/*前置增强*/
public class LoggerBefore implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day11aop01.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="before" class="cn.dawn.day11aop01.LoggerBefore"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="before"></property>
    </bean>

</beans>

 

  单测方法:

 

package cn.dawn.day11aop01;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180305 {
    @Test
    /*aop代理工厂bean前置增强*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day11aop01.xml");
        SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
        service.doSome();
    }
}

 

 

@No.2:后置增强:

  需要后置增强的类:SomeServiceImpl(此类和前置那个写在不同包下,一会的配置文件也不同,往后都是如此)

 

package cn.dawn.day12aop02;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
    }
}

 

  后置增强的内容的的类,他与前置增强的那个实现的接口不同:LoggerAfter

 

package cn.dawn.day12aop02;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
 * Created by Dawn on 2018/3/5.
 */
/*后置增强*/
public class LoggerAfter implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("===============after==================");
    }
}

 

  配置文件中:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day12aop02.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="afteradvice" class="cn.dawn.day12aop02.LoggerAfter"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="afteradvice"></property>
    </bean>

</beans>

 

  单测方法:

 

package cn.dawn.day12aop02;

        import cn.dawn.day12aop02.SomeServiceImpl;
        import org.junit.Test;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180305 {
    @Test
    /*aop代理工厂bean后置增强*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day12aop02.xml");
        SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
        service.doSome();
    }
}

 

 

@No.3:环绕增强:

  需要环绕增强的类:SomeServiceImpl

 

package cn.dawn.day13aop03;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
    }
}

 

  环绕增强内容的:

 

package cn.dawn.day13aop03;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Created by Dawn on 2018/3/8.
 */
/*环绕增强需要实现MethodInterceptor这个接口*/
public class MethodAdvice implements MethodInterceptor {
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前置增强");
        /*它可以使前置增强和后置增强分开,同时实现前置和后置*/
        methodInvocation.proceed();
        System.out.println("后置增强");
        return null;
    }
}

 

  配置文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="methodAdvice"></property>
    </bean>

</beans>

 

  单测方法:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day13aop03.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="methodAdvice" class="cn.dawn.day13aop03.MethodAdvice"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="methodAdvice"></property>
    </bean>

</beans>

 

 

@No.4:异常增强:

  异常增强是主要用在他出错的时候,进行记录用的,此处模拟起来简单的就是除0异常和空指针异常

  写一个类,它里面的方法存在异常,运行时异常

  SomeServiceImpl类

 

package cn.dawn.day14aop04;


/**
 * Created by Dawn on 2018/3/8.
 */
public class SomeServiceImpl {
    public void doSome() {
        System.out.println("do something");
        String abc=null;
        System.out.println(abc.toString());;
    }
}

 

  此处模拟的是空指针异常,当然异常种类很多,不一一模拟了

  实现ThrowsAdvice接口的类

 

package cn.dawn.day14aop04;

import org.springframework.aop.ThrowsAdvice;

/**
 * Created by Dawn on 2018/3/8.
 */
public class MyThrowsAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception ex){
        System.out.println("网络中断XXXXX-错误号05289");
    }
}

 

  他这个接口里面没有方法要重写,很是奇怪,那是随便写什么defgabc的都可以吗?不是的,翻他的源码,他在上面的注释里提供了模板,只有按照他模板写的才能读取到,此处我用了他的其中一个模板方法

  配置文件中:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--要增强的对象-->
    <bean id="service" class="cn.dawn.day14aop04.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="myThrowsAdvice" class="cn.dawn.day14aop04.MyThrowsAdvice"></bean>
    <!--代理工厂bean-->
    <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--要增强的对象-->
        <property name="target" ref="service"></property>
        <!--增强的内容-->
        <property name="interceptorNames" value="myThrowsAdvice"></property>
    </bean>

</beans>

 

  单测方法:

 

package cn.dawn.day14aop04;

import cn.dawn.day14aop04.SomeServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180305 {
    @Test
    /*aop代理工厂bean异常增强*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day14aop04.xml");
        SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
        try{
            service.doSome();
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}

 

    看到这儿的try-catch了吧,他的作用是让单测能通过单元测试,能变成对勾,能执行到结束,不会因为模拟的异常而中断   

--------------------------End-------------------------

基于代理工厂Bean实现aop的四种增强总结完毕

 

目录
相关文章
|
4月前
|
XML 安全 Java
使用 Spring 的 @Aspect 和 @Pointcut 注解简化面向方面的编程 (AOP)
面向方面编程(AOP)通过分离横切关注点,如日志、安全和事务,提升代码模块化与可维护性。Spring 提供了对 AOP 的强大支持,核心注解 `@Aspect` 和 `@Pointcut` 使得定义切面与切入点变得简洁直观。`@Aspect` 标记切面类,集中处理通用逻辑;`@Pointcut` 则通过表达式定义通知的应用位置,提高代码可读性与复用性。二者结合,使开发者能清晰划分业务逻辑与辅助功能,简化维护并提升系统灵活性。Spring AOP 借助代理机制实现运行时织入,与 Spring 容器无缝集成,支持依赖注入与声明式配置,是构建清晰、高内聚应用的理想选择。
555 0
|
3月前
|
XML Java 数据格式
《深入理解Spring》:AOP面向切面编程深度解析
Spring AOP通过代理模式实现面向切面编程,将日志、事务等横切关注点与业务逻辑分离。支持注解、XML和编程式配置,提供五种通知类型及丰富切点表达式,助力构建高内聚、低耦合的可维护系统。
|
8月前
|
监控 安全 Java
Spring AOP实现原理
本内容主要介绍了Spring AOP的核心概念、实现机制及代理生成流程。涵盖切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)等关键概念,解析了JDK动态代理与CGLIB代理的原理及对比,并深入探讨了通知执行链路和责任链模式的应用。同时,详细分析了AspectJ注解驱动的AOP解析过程,包括切面识别、切点表达式匹配及通知适配为Advice的机制,帮助理解Spring AOP的工作原理与实现细节。
1294 13
|
5月前
|
人工智能 监控 安全
如何快速上手【Spring AOP】?核心应用实战(上篇)
哈喽大家好吖~欢迎来到Spring AOP系列教程的上篇 - 应用篇。在本篇,我们将专注于Spring AOP的实际应用,通过具体的代码示例和场景分析,帮助大家掌握AOP的使用方法和技巧。而在后续的下篇中,我们将深入探讨Spring AOP的实现原理和底层机制。 AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的核心特性之一,它能够帮助我们解决横切关注点(如日志记录、性能统计、安全控制、事务管理等)的问题,提高代码的模块化程度和复用性。
|
5月前
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
|
10月前
|
前端开发 Java 数据库连接
Spring MVC 扩展和SSM框架整合
通过以上步骤,我们可以将Spring MVC扩展并整合到SSM框架中。这个过程包括配置Spring MVC和Spring的核心配置文件,创建控制器、服务层和MyBatis的Mapper接口及映射文件。在实际开发中,可以根据具体业务需求进行进一步的扩展和优化,以构建更加灵活和高效的企业级应用程序。
276 5
|
11月前
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
846 25
|
10月前
|
Java API 微服务
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——Spring Boot 中的 AOP 处理
本文详细讲解了Spring Boot中的AOP(面向切面编程)处理方法。首先介绍如何引入AOP依赖,通过添加`spring-boot-starter-aop`实现。接着阐述了如何定义和实现AOP切面,包括常用注解如`@Aspect`、`@Pointcut`、`@Before`、`@After`、`@AfterReturning`和`@AfterThrowing`的使用场景与示例代码。通过这些注解,可以分别在方法执行前、后、返回时或抛出异常时插入自定义逻辑,从而实现功能增强或日志记录等操作。最后总结了AOP在实际项目中的重要作用,并提供了课程源码下载链接供进一步学习。
1367 0

热门文章

最新文章