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();
    }

}

 

  执行结果如下:

 

 

 

 

  

 

 

  

posted @ 2019-10-29 16:28  败给时光  阅读(384)  评论(0编辑  收藏  举报