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的四种增强总结完毕

 

目录
打赏
0
0
0
0
1030
分享
相关文章
Spring Boot中的AOP实现
Spring AOP(面向切面编程)允许开发者在不修改原有业务逻辑的情况下增强功能,基于代理模式拦截和增强方法调用。Spring Boot通过集成Spring AOP和AspectJ简化了AOP的使用,只需添加依赖并定义切面类。关键概念包括切面、通知和切点。切面类使用`@Aspect`和`@Component`注解标注,通知定义切面行为,切点定义应用位置。Spring Boot自动检测并创建代理对象,支持JDK动态代理和CGLIB代理。通过源码分析可深入了解其实现细节,优化应用功能。
119 6
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
97 25
|
1月前
|
Spring AOP—深入动态代理 万字详解(通俗易懂)
Spring 第四节 AOP——动态代理 万字详解!
79 24
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
93 8
Spring Aop该如何使用
本文介绍了AOP(面向切面编程)的基本概念和术语,并通过具体业务场景演示了如何在Spring框架中使用Spring AOP。文章详细解释了切面、连接点、通知、切点等关键术语,并提供了完整的示例代码,帮助读者轻松理解和应用Spring AOP。
112 2
Spring Aop该如何使用
什么是AOP?如何与Spring Boot一起使用?
什么是AOP?如何与Spring Boot一起使用?
116 5
深入解析:Spring AOP的底层实现机制
在现代软件开发中,Spring框架的AOP(面向切面编程)功能因其能够有效分离横切关注点(如日志记录、事务管理等)而备受青睐。本文将深入探讨Spring AOP的底层原理,揭示其如何通过动态代理技术实现方法的增强。
117 8
Spring AOP 底层原理技术分享
Spring AOP(面向切面编程)是Spring框架中一个强大的功能,它允许开发者在不修改业务逻辑代码的情况下,增加额外的功能,如日志记录、事务管理等。本文将深入探讨Spring AOP的底层原理,包括其核心概念、实现方式以及如何与Spring框架协同工作。
|
4月前
|
深入调查研究Spring AOP
【11月更文挑战第15天】
66 5
Spring AOP深度解析:探秘动态代理与增强逻辑
Spring框架中的AOP(Aspect-Oriented Programming,面向切面编程)功能为开发者提供了一种强大的工具,用以将横切关注点(如日志、事务管理等)与业务逻辑分离。本文将深入探讨Spring AOP的底层原理,包括动态代理机制和增强逻辑的实现。
78 4
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等