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

 

目录
相关文章
|
3月前
|
存储 缓存 Java
Spring高手之路23——AOP触发机制与代理逻辑的执行
本篇文章深入解析了Spring AOP代理的触发机制和执行流程,从源码角度详细讲解了Bean如何被AOP代理,包括代理对象的创建、配置与执行逻辑,帮助读者全面掌握Spring AOP的核心技术。
62 3
Spring高手之路23——AOP触发机制与代理逻辑的执行
|
4月前
|
设计模式 Java 测试技术
spring复习04,静态代理动态代理,AOP
这篇文章讲解了Java代理模式的相关知识,包括静态代理和动态代理(JDK动态代理和CGLIB),以及AOP(面向切面编程)的概念和在Spring框架中的应用。文章还提供了详细的示例代码,演示了如何使用Spring AOP进行方法增强和代理对象的创建。
spring复习04,静态代理动态代理,AOP
|
3月前
|
Java Spring
获取spring工厂中bean对象的两种方式
获取spring工厂中bean对象的两种方式
62 1
|
4月前
|
设计模式 Java Spring
spring源码设计模式分析-代理设计模式(二)
spring源码设计模式分析-代理设计模式(二)
|
6月前
|
缓存 安全 Java
Spring高手之路21——深入剖析Spring AOP代理对象的创建
本文详细介绍了Spring AOP代理对象的创建过程,分为三个核心步骤:判断是否增强、匹配增强器和创建代理对象。通过源码分析和时序图展示,深入剖析了Spring AOP的工作原理,帮助读者全面理解Spring AOP代理对象的生成机制及其实现细节。
64 0
Spring高手之路21——深入剖析Spring AOP代理对象的创建
|
5月前
|
缓存 安全 Java
Spring AOP 中两种代理类型的限制
【8月更文挑战第22天】
46 0
|
5月前
|
Java Spring
|
5月前
|
安全 Java 开发者
|
5月前
|
安全 Java 测试技术
Spring Security 中的委托过滤器代理
【8月更文挑战第21天】
56 0
|
7月前
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
90 2