• Spring 增强类型


    spring 增强类型支持5种:

      前置增强
                 org.springframework.aop.BeforeAdvice  代表前置增强,因为spring只支持方法级的增强,所以MethodBeforeAdvice 是目前可用前置增强,表示在目标方法执行前实施增强。


      后置增强
                 org.springframework.aop.AfterAdvice 代表后增强,表示目标方法在执行后实施增强 


      环绕增强
                  org.springframework.aop.MethodInterceptor 代表环绕增强,表示目标方法执行前后实施增强


      异常抛出增强
                  org.springframework.aop.ThrowsAdvice 代表抛出异常增强,表示目标方法抛出异常后实施增强

       引介增强

           org.springframework.aop.IntroductionInterceptor代表引介增强,表示在目标类中添加一些新的方法和属性

    一.前置增强

      首先创建一个接口: 

    package com.cjlb.dao;
    
    public interface IdoService {
        public void doServ();
    }

      接口实现类:

    package com.cjlb.dao;
    
    public class IdoServiceImpl implements IdoService {
        public void doServ() {
            System.out.println("+++++++业务逻辑+++++++");
        }
    }

    application_after.xml配置文件(使用代理工厂):

        <!--前置增强-->
        <!--注入业务 Bean-->
        <bean id="idoService" class="com.cjlb.dao.around.IdoServiceImpl"></bean>
        <!--增强   切面-->
        <bean id="before" class="com.cjlb.dao.Before"></bean>
        <!--使用代理工厂实现增强-->
        <bean id="proxyFactry" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--将增强类和业务织入到一起-->
            <property name="target" ref="idoService"></property>
            <!--拦截增强类-->
            <property name="interceptorNames" value="before"></property>
            <!--更换代理方式  proxyTargetClass默认为false  默认jdk代理 当目标对象没有实现和接口是 自动改为(默认为)cjlb代理-->
            <property name="proxyTargetClass" value="true"></property>
    
        </bean>

      增强类:

    package com.cjlb.dao;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class Before implements MethodBeforeAdvice {
        public void before(Method method, Object[] args, Object target) throws Throwable {
            System.out.println("------前置------");
        }
    }

      测试类:

    package com.cjlb;
    
    import com.cjlb.dao.around.IdoService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class test {
        public static void main(String[] args) {
            ApplicationContext apct=new ClassPathXmlApplicationContext("application_after.xml");
           IdoService  idos=(IdoService)apct.getBean("proxyFactry");
           idos.doServ();
        }
    }

      后置增强和前置增强基本相同(把增强类的implements MethodBeforeAdvice改为 implements AfterReturningAdvice)

    二.环绕增强

      继续使用上边的接口可实现类为例:

      创建环绕增强类:

      

    package com.cjlb.dao.around;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    public class Around implements MethodInterceptor {
        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;
        }
    }

      application_after.xml配置文件如下:

    更改如图红框框的位置即可。

      AroundTest测试类:

    package com.cjlb;
    
    import com.cjlb.dao.around.IdoService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AroundTest {
        public static void main(String[] args) {
            ApplicationContext apts=new ClassPathXmlApplicationContext("application_after.xml");
            IdoService idos=(IdoService)apts.getBean("proxyFactry");
            idos.doServ();
        }
    
    }

      执行结果如下:

      

      

  • 相关阅读:
    UML--->用例图梳理
    UML--->活动图梳理
    论懂产品对程序员的重要性
    markdown时序图语法
    bootstrap 设置表格固定宽度 内容换行
    gitlab的本地搭建和部署使用
    layer.load的使用
    git fatal: remote origin already exists. 报错解决
    导出csv xls文件数字会自动变科学计数法的解决方式
    git常用命令
  • 原文地址:https://www.cnblogs.com/wangdayexinyue/p/11759541.html
Copyright © 2020-2023  润新知