Spring(七)Spring中的四种增强和顾问

摘要:
Spring中的四种增强有那四种?˃@Testpublicvoidt1(){ApplicationContextcontext=newClassPathXmlApplicationContext;ISomeServiceproxyService=context.getBean;proxyService.doSome();}运行的结果是2.后置增强和前置增强一样,只是改一改配置文件里的名称就可以˂?

Spring中的四种增强有那四种?

前置增强 后置增强 环绕增强 异常增强

先编写接口和实体类 ISomeService和SomeServiceImpl

package demo10;

/**
 * Created by mycom on 2018/3/8.
 */
public interfaceISomeService {
    public voiddoSome();
}
package demo10;

/**
 * Created by mycom on 2018/3/8.
 */
public classSomeServiceImpl implements ISomeService {
    public voiddoSome() {
        System.out.println("=================");
    }
}

先来说第一个前置增强,直接用例子来说明

package demo10;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by mycom on 2018/3/8.
 */
public classBeforeAdvice implements MethodBeforeAdvice {
    public voidbefore(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=========before");
    }
}

在配置文件中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="service" class="demo10.SomeServiceImpl"></bean>
    <bean id="beforeAdvice" class="demo10.BeforeAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="beforeAdvice"></property>
    </bean>


</beans>
@Test
    public voidt1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextBefore.xml");
        ISomeService proxyService =(ISomeService) context.getBean("proxyService");
        proxyService.doSome();
    }

运行的结果是

Spring(七)Spring中的四种增强和顾问第1张

2.后置增强和前置增强一样,只是改一改配置文件里的名称就可以

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="service" class="demo11.SomeServiceImpl"></bean>
    <bean id="afterAdvice" class="demo11.AfterAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="afterAdvice"></property>
    </bean>


</beans>

3.环绕增强

直接饮用上面的接口和实现类了

在创建另一个类MethodAdvice

package demo12;


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

/**
 * Created by mycom on 2018/3/8.
 */
public classMethodAdvice implements MethodInterceptor {


    publicObject invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("前置增强");
        Object result =methodInvocation.proceed();
        System.out.println("后置增强");
        returnresult;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="service" class="demo12.SomeServiceImpl"></bean>
    <bean id="methodAdvice" class="demo12.MethodAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="methodAdvice"></property>
    </bean>


</beans>

4.异常增强

package demo13;


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ThrowsAdvice;

/**
 * Created by mycom on 2018/3/8.
 */
public classMyThroesAdvice implements ThrowsAdvice {
    public voidafterThrowing(Exception ex){
        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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="service" class="demo13.SomeServiceImpl"></bean>
    <bean id="throwsAdvice" class="demo13.MyThroesAdvice"></bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="throwsAdvice"></property>
    </bean>


</beans>
@Test
   public voidt2(){
       ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextThrows.xml");
       ISomeService proxyService =(ISomeService) context.getBean("proxyService");
       try{
           proxyService.doSome();
       }catch(Exception ex){
           ex.printStackTrace();
       }

5.advisor 是顾问的意思 正对某一个方法增强

还有一个词是通知 advice 我自己的理解是 通知视同只所有人,顾问是针对某个人顾问,这样方便记忆,顾问包括通知

通知的方法有两种名称匹配方法切入点顾问和正则方法切入点顾问

先看第一种:

现在在ISomeService接口中在添加一个方法doAny(),在实现类中重写这个方法

那么在配置文件中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="service" class="demo14.SomeServiceImpl"></bean>
    <bean id="beforeAdvice" class="demo14.BeforeAdvice"></bean>
    
    <!--与其他不一样的地方-->
    <bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="mappedNames" value="do*"></property>
    </bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="advisor"></property>
    </bean>


</beans>

这个配置文件可以对比着之前写的配置文件看看有什么不同之处

@Test
   public voidt2(){
       ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextAdvisor.xml");
       ISomeService proxyService =(ISomeService) context.getBean("proxyService");
       proxyService.doSome();
       proxyService.doAny();
   }

第二种方法:正则方法切入点顾问

同样是饮用上面的接口和实现类,在配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="service" class="demo16.SomeServiceImpl"></bean>
    <bean id="beforeAdvice" class="demo16.BeforeAdvice"></bean>

    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="pattern" value=".*do.*"></property>
    </bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="advisor"></property>
    </bean>


</beans>
<property name="pattern" value=".*do.*"></property>

这个节点中name还有一个值是:patterns,其value值是:

<property name="patterns" value=".*Some,.*Any"></property>

这种方法用的是正则的符号

Spring(七)Spring中的四种增强和顾问第2张

免责声明:文章转载自《Spring(七)Spring中的四种增强和顾问》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Sublime text 3 汉化教程RobotFramework做自动化中,能定位到iFrame里面,怎么在iFrame里面输入文字?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

spring-装配bean

1.spring配置的可选方案 spring提供了三种装配bean的机制: 在xml中显式配置 在java中进行显式配置 隐式的bean发现机制和自动转配 三种方式在适当的场合使用,当然你可以选择自己喜欢的方式转配bean。 2.自动化装配bean spring从两个角度实现自动化装配 组件扫描(component scanning)spring会自...

SpringBoot启动时让方法自动执行的几种实现方式

在springBoot中我们有时候需要让项目在启动时提前加载相应的数据或者执行某个方法,那么实现提前加载的方式有哪些呢?接下来我带领大家逐个解答 1.实现ServletContextAware接口并重写其setServletContext方法 实现ServletContextAware 注意:该方法会在填充完普通Bean的属性,但是还没有进行Bean的...

日常编码规范(Java版)

规范: 命名: 接口命名。接口必须是名词,并且接口是能准确的描述要做的事情,命名能清晰的看出输入输出,可以是抽象的行为描述。接口必须以一个动作的名词形式结尾,比如reader,handler等。接口的命名,必须是抽象的,除非接口本身和具体实现紧密相关,否则不应该在接口中包含任何和具体实现相关的名词。接口命名根据行为分为以下几种: 读取某个数据,命名: {...

Spring 框架的事务管理

1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接口: 事务定义信息(事务的隔离级别,传播行为,超时,只读等); TransactionStatus 接口: 事务的状态; 平台事务管理器真正管理事务对...

使用java配置定时任务的几种配置方式及示例

Spring定时器,主要有两种实现方式,包括Java Timer定时和Quartz定时器! 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 package com.land; import java.util.Date;import java.util.TimerTask; public class Timer...

Java各种反射性能对比

对各种方法实现get方法的性能进行了一个测试。 总共有5个测试,,每个测试都是执行1亿次 1. 直接通过Java的get方法 2.通过高性能的ReflectAsm库进行测试 3.通过Java Class类自带的反射获得Method测试 4.使用Java自带的Property类获取Method测试 5.BeanUtils的getProperty测试 1 测试...